| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "slices" |
| 6 | "strings" |
| 7 | |
| 8 | tea "github.com/charmbracelet/bubbletea" |
| 9 | "github.com/charmbracelet/lipgloss" |
| 10 | "github.com/tomwright/dasel/v3/internal" |
| 11 | "github.com/tomwright/dasel/v3/parsing" |
| 12 | ) |
| 13 | |
| 14 | var ( |
| 15 | interactiveKeyQuit = tea.KeyCtrlC |
| 16 | interactiveKeyCycleRead = tea.KeyCtrlE |
| 17 | interactiveKeyCycleWrite = tea.KeyCtrlD |
| 18 | |
| 19 | headingStyle = func() lipgloss.Style { |
| 20 | return lipgloss.NewStyle().Padding(0, 1, 1, 1) |
| 21 | }() |
| 22 | shortcutStyle = func() lipgloss.Style { |
| 23 | return lipgloss.NewStyle().Padding(0, 1).Align(lipgloss.Left) |
| 24 | }() |
| 25 | headerStyle = func() lipgloss.Style { |
| 26 | return lipgloss.NewStyle().Padding(1).Align(lipgloss.Center) |
| 27 | }() |
| 28 | inputStyle = func() lipgloss.Style { |
| 29 | return lipgloss.NewStyle().Margin(0, 0, 1, 0) |
| 30 | }() |
| 31 | outputContentStyle = func() lipgloss.Style { |
| 32 | return lipgloss.NewStyle().Padding(0, 1).Border(lipgloss.RoundedBorder()) |
| 33 | }() |
| 34 | outputHeaderStyle = func() lipgloss.Style { |
| 35 | return lipgloss.NewStyle().Padding(0, 2).Margin(0, 0, 1, 0).Underline(true) |
| 36 | }() |
| 37 | ) |
| 38 | |
| 39 | type interactiveDaselExecutor func(selector string, root bool, formatIn parsing.Format, formatOut parsing.Format, in string) (res string, err error) |
| 40 | |
| 41 | func newInteractiveTeaProgram(initialInput string, initialSelector string, formatIn parsing.Format, formatOut parsing.Format, run interactiveDaselExecutor) (*tea.Program, func() string) { |
| 42 | m := newInteractiveRootModel(initialInput, initialSelector, formatIn, formatOut, run) |
| 43 | return tea.NewProgram(m, tea.WithAltScreen()), func() string { |
| 44 | return m.sharedData.selector |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | type interactiveSharedData struct { |
| 49 | formatIn parsing.Format |
| 50 | formatOut parsing.Format |
| 51 | selector string |
| 52 | input string |
| 53 | } |
| 54 | |
| 55 | type interactiveRootModel struct { |
| 56 | sharedData *interactiveSharedData |
| 57 | inputModel *interactiveInputModel |
| 58 | outputModels []*interactiveOutputModel |
| 59 | } |
| 60 | |
| 61 | func newInteractiveRootModel(initialInput string, initialSelector string, formatIn parsing.Format, formatOut parsing.Format, run interactiveDaselExecutor) *interactiveRootModel { |
| 62 | res := &interactiveRootModel{ |
| 63 | sharedData: &interactiveSharedData{ |
| 64 | formatIn: formatIn, |
| 65 | formatOut: formatOut, |
| 66 | selector: initialSelector, |
| 67 | input: initialInput, |
| 68 | }, |
| 69 | outputModels: make([]*interactiveOutputModel, 0), |
| 70 | } |
| 71 | |
| 72 | res.inputModel = newInteractiveInputModel(res.sharedData) |
| 73 | |
| 74 | outputRootModel := newInteractiveOutputModel(res.sharedData, true, run) |
| 75 | outputResultModel := newInteractiveOutputModel(res.sharedData, false, run) |
| 76 | |
| 77 | res.outputModels = append(res.outputModels, outputRootModel, outputResultModel) |
| 78 | |
| 79 | return res |
| 80 | } |
| 81 | |
| 82 | func (m *interactiveRootModel) Init() tea.Cmd { |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | func cycleFormats(all []parsing.Format, current parsing.Format) parsing.Format { |
| 87 | slices.SortFunc(all, func(i, j parsing.Format) int { |
| 88 | return strings.Compare(string(i), string(j)) |
| 89 | }) |
| 90 | cur := -1 |
| 91 | for i, format := range all { |
| 92 | if format == current { |
| 93 | cur = i |
| 94 | break |
| 95 | } |
| 96 | } |
| 97 | next := cur + 1 |
| 98 | if next > len(all)-1 { |
| 99 | next = 0 |
| 100 | } |
| 101 | return all[next] |
| 102 | } |
| 103 | |
| 104 | func (m *interactiveRootModel) cycleReader() { |
| 105 | m.sharedData.formatIn = cycleFormats(parsing.RegisteredReaders(), m.sharedData.formatIn) |
| 106 | } |
| 107 | |
| 108 | func (m *interactiveRootModel) cycleWriter() { |
| 109 | m.sharedData.formatOut = cycleFormats(parsing.RegisteredWriters(), m.sharedData.formatOut) |
| 110 | } |
| 111 | |
| 112 | func (m *interactiveRootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 113 | var cmds []tea.Cmd |
| 114 | var cmd tea.Cmd |
| 115 | |
| 116 | switch msg := msg.(type) { |
| 117 | case tea.KeyMsg: |
| 118 | switch msg.Type { |
| 119 | case interactiveKeyQuit: |
| 120 | return m, tea.Quit |
| 121 | case interactiveKeyCycleRead: |
| 122 | m.cycleReader() |
| 123 | case interactiveKeyCycleWrite: |
| 124 | m.cycleWriter() |
| 125 | default: |
| 126 | } |
| 127 | |
| 128 | case tea.WindowSizeMsg: |
| 129 | headerStyle = headerStyle.Width(msg.Width).MaxWidth(msg.Width) |
| 130 | |
| 131 | var headerHeight int |
| 132 | { |
| 133 | headerHeight += lipgloss.Height(m.headerView()) |
| 134 | headerHeight += lipgloss.Height(m.inputView()) |
| 135 | } |
| 136 | verticalMarginHeight := headerHeight |
| 137 | |
| 138 | numCols := len(m.outputModels) |
| 139 | |
| 140 | viewportHeight := msg.Height - verticalMarginHeight - (2 * numCols) |
| 141 | viewportWidth := (msg.Width / numCols) - (2 * numCols) |
| 142 | |
| 143 | for _, outputModel := range m.outputModels { |
| 144 | outputModel.setSize(viewportWidth, viewportHeight) |
| 145 | outputModel.setVerticalPosition(verticalMarginHeight) |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | { |
| 150 | var model tea.Model |
| 151 | model, cmd = m.inputModel.Update(msg) |
| 152 | m.inputModel = model.(*interactiveInputModel) |
| 153 | cmds = append(cmds, cmd) |
| 154 | } |
| 155 | |
| 156 | for i, outputModel := range m.outputModels { |
| 157 | var model tea.Model |
| 158 | model, cmd = outputModel.Update(msg) |
| 159 | m.outputModels[i] = model.(*interactiveOutputModel) |
| 160 | cmds = append(cmds, cmd) |
| 161 | } |
| 162 | |
| 163 | return m, tea.Batch(cmds...) |
| 164 | } |
| 165 | |
| 166 | func (m *interactiveRootModel) headerView() string { |
| 167 | header := headingStyle.Render("Dasel interactive mode - " + internal.Version) |
| 168 | |
| 169 | shortcuts := "\n" |
| 170 | shortcuts += fmt.Sprintf("%s: %s\n", interactiveKeyQuit, "Quit") |
| 171 | shortcuts += fmt.Sprintf("%s: %s\n", interactiveKeyCycleRead, "Cycle reader") |
| 172 | shortcuts += fmt.Sprintf("%s: %s\n", interactiveKeyCycleWrite, "Cycle writer") |
| 173 | |
| 174 | out := append([]string{header}, shortcutStyle.Render(shortcuts)) |
| 175 | |
| 176 | out = append(out, fmt.Sprintf("\nReader: %s | Writer: %s", m.sharedData.formatIn, m.sharedData.formatOut)) |
| 177 | |
| 178 | return headerStyle.Render(out...) |
| 179 | } |
| 180 | |
| 181 | func (m *interactiveRootModel) inputView() string { |
| 182 | return inputStyle.Render(m.inputModel.View()) |
| 183 | } |
| 184 | |
| 185 | func (m *interactiveRootModel) View() string { |
| 186 | rows := make([]string, 0) |
| 187 | |
| 188 | rows = append(rows, m.headerView()) |
| 189 | |
| 190 | rows = append(rows, m.inputView()) |
| 191 | |
| 192 | { |
| 193 | cols := make([]string, 0) |
| 194 | for _, outputModel := range m.outputModels { |
| 195 | cols = append(cols, outputModel.View()) |
| 196 | } |
| 197 | if len(cols) > 0 { |
| 198 | rows = append(rows, lipgloss.JoinHorizontal(lipgloss.Top, cols...)) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return lipgloss.JoinVertical(lipgloss.Left, rows...) |
| 203 | } |