|
| 1 | +package components |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/gdamore/tcell/v2" |
| 11 | + "github.com/rivo/tview" |
| 12 | + |
| 13 | + "github.com/jorgerojas26/lazysql/app" |
| 14 | +) |
| 15 | + |
| 16 | +const defaultBatchSize = 10000 |
| 17 | + |
| 18 | +// CSVExportScope defines the scope of data to export |
| 19 | +type CSVExportScope int |
| 20 | + |
| 21 | +const ( |
| 22 | + ExportCurrentPage CSVExportScope = iota |
| 23 | + ExportAllRecords |
| 24 | +) |
| 25 | + |
| 26 | +// CSVExportOptions contains options for creating a CSV export modal. |
| 27 | +type CSVExportOptions struct { |
| 28 | + DatabaseName string // Database name for file naming |
| 29 | + TableName string // Table name for file naming |
| 30 | + HasPagination bool // Whether pagination exists (determines UI: 2 buttons vs 1) |
| 31 | + RowCount int // Current row count for display (excluding header) |
| 32 | +} |
| 33 | + |
| 34 | +// getDefaultExportDir returns the default directory for CSV export. |
| 35 | +// Uses ~/Downloads on all platforms (standard location), falls back to home directory. |
| 36 | +func getDefaultExportDir() string { |
| 37 | + homeDir, err := os.UserHomeDir() |
| 38 | + if err != nil { |
| 39 | + return "." |
| 40 | + } |
| 41 | + |
| 42 | + // ~/Downloads is the standard download location on macOS, Windows, and most Linux distros |
| 43 | + downloadDir := filepath.Join(homeDir, "Downloads") |
| 44 | + if info, err := os.Stat(downloadDir); err == nil && info.IsDir() { |
| 45 | + return downloadDir |
| 46 | + } |
| 47 | + |
| 48 | + return homeDir |
| 49 | +} |
| 50 | + |
| 51 | +// CSVExportModal is a modal for exporting data to CSV. |
| 52 | +type CSVExportModal struct { |
| 53 | + tview.Primitive |
| 54 | + form *tview.Form |
| 55 | + hasPagination bool |
| 56 | + onExport func(filePath string, scope CSVExportScope, batchSize int) |
| 57 | +} |
| 58 | + |
| 59 | +// NewCSVExportModal creates a new CSVExportModal. |
| 60 | +func NewCSVExportModal(opts CSVExportOptions, onExport func(filePath string, scope CSVExportScope, batchSize int)) *CSVExportModal { |
| 61 | + cem := &CSVExportModal{ |
| 62 | + hasPagination: opts.HasPagination, |
| 63 | + onExport: onExport, |
| 64 | + } |
| 65 | + |
| 66 | + // Default file path with timestamp to avoid conflicts |
| 67 | + timestamp := time.Now().Format("20060102_150405") |
| 68 | + fileName := fmt.Sprintf("%s_%s_%s.csv", opts.DatabaseName, opts.TableName, timestamp) |
| 69 | + defaultPath := filepath.Join(getDefaultExportDir(), fileName) |
| 70 | + |
| 71 | + cem.form = tview.NewForm(). |
| 72 | + AddInputField("File Path", defaultPath, 0, nil, nil) |
| 73 | + |
| 74 | + if opts.HasPagination { |
| 75 | + // Add batch size field for table view (used for Export All Records) |
| 76 | + cem.form.AddInputField("Batch Size", strconv.Itoa(defaultBatchSize), 0, nil, nil) |
| 77 | + |
| 78 | + // Table view: show both options |
| 79 | + cem.form.AddButton("Export Current Page", func() { |
| 80 | + cem.export(ExportCurrentPage) |
| 81 | + }) |
| 82 | + cem.form.AddButton("Export All Records", func() { |
| 83 | + cem.export(ExportAllRecords) |
| 84 | + }) |
| 85 | + } else { |
| 86 | + // Query result: show single export button with row count |
| 87 | + buttonLabel := fmt.Sprintf("Export (%d rows)", opts.RowCount) |
| 88 | + cem.form.AddButton(buttonLabel, func() { |
| 89 | + cem.export(ExportAllRecords) |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + cem.form.SetFieldStyle( |
| 94 | + tcell.StyleDefault. |
| 95 | + Background(app.Styles.SecondaryTextColor). |
| 96 | + Foreground(app.Styles.ContrastSecondaryTextColor), |
| 97 | + ).SetButtonActivatedStyle(tcell.StyleDefault. |
| 98 | + Background(app.Styles.SecondaryTextColor). |
| 99 | + Foreground(app.Styles.ContrastSecondaryTextColor), |
| 100 | + ).SetButtonStyle(tcell.StyleDefault. |
| 101 | + Background(app.Styles.InverseTextColor). |
| 102 | + Foreground(app.Styles.ContrastSecondaryTextColor), |
| 103 | + ) |
| 104 | + |
| 105 | + cem.form.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { |
| 106 | + if event.Key() == tcell.KeyEsc { |
| 107 | + cem.cancel() |
| 108 | + return nil |
| 109 | + } |
| 110 | + return event |
| 111 | + }) |
| 112 | + |
| 113 | + cem.form.SetBorder(false) |
| 114 | + |
| 115 | + hint := tview.NewTextView(). |
| 116 | + SetText("Esc to cancel"). |
| 117 | + SetTextAlign(tview.AlignCenter). |
| 118 | + SetTextColor(app.Styles.TertiaryTextColor) |
| 119 | + |
| 120 | + formWithHint := tview.NewFlex(). |
| 121 | + SetDirection(tview.FlexRow). |
| 122 | + AddItem(cem.form, 0, 1, true). |
| 123 | + AddItem(hint, 1, 0, false) |
| 124 | + formWithHint.SetBorder(true).SetTitle(" Export to CSV ").SetTitleAlign(tview.AlignLeft) |
| 125 | + |
| 126 | + grid := tview.NewGrid(). |
| 127 | + SetRows(0, 11, 0). |
| 128 | + SetColumns(0, 80, 0). |
| 129 | + AddItem(formWithHint, 1, 1, 1, 1, 0, 0, true) |
| 130 | + |
| 131 | + cem.Primitive = grid |
| 132 | + |
| 133 | + return cem |
| 134 | +} |
| 135 | + |
| 136 | +func (cem *CSVExportModal) export(scope CSVExportScope) { |
| 137 | + filePath := cem.form.GetFormItem(0).(*tview.InputField).GetText() |
| 138 | + if filePath == "" { |
| 139 | + cem.showErrorModal("File path cannot be empty") |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + batchSize := 0 |
| 144 | + if cem.hasPagination { |
| 145 | + batchSizeText := cem.form.GetFormItem(1).(*tview.InputField).GetText() |
| 146 | + var err error |
| 147 | + batchSize, err = strconv.Atoi(batchSizeText) |
| 148 | + if err != nil || batchSize <= 0 { |
| 149 | + cem.showErrorModal("Batch size must be a positive integer") |
| 150 | + return |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + mainPages.RemovePage(pageNameCSVExport) |
| 155 | + |
| 156 | + if cem.onExport != nil { |
| 157 | + cem.onExport(filePath, scope, batchSize) |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +func (cem *CSVExportModal) showErrorModal(message string) { |
| 162 | + modal := NewErrorModal(message) |
| 163 | + modal.SetDoneFunc(func(_ int, _ string) { |
| 164 | + mainPages.RemovePage(pageNameCSVExportError) |
| 165 | + }) |
| 166 | + |
| 167 | + mainPages.AddPage(pageNameCSVExportError, modal, true, true) |
| 168 | + App.SetFocus(modal) |
| 169 | +} |
| 170 | + |
| 171 | +func (cem *CSVExportModal) cancel() { |
| 172 | + mainPages.RemovePage(pageNameCSVExport) |
| 173 | +} |
0 commit comments