| 1 | package cli |
| 2 | |
| 3 | import ( |
| 4 | "github.com/charmbracelet/bubbles/viewport" |
| 5 | tea "github.com/charmbracelet/bubbletea" |
| 6 | "github.com/charmbracelet/lipgloss" |
| 7 | "github.com/tomwright/dasel/v3/parsing" |
| 8 | ) |
| 9 | |
| 10 | type interactiveOutputModel struct { |
| 11 | sharedData *interactiveSharedData |
| 12 | hasUpdatedBefore bool |
| 13 | lastSeenSelector string |
| 14 | lastSeenFormatIn parsing.Format |
| 15 | lastSeenFormatOut parsing.Format |
| 16 | lastSeenInput string |
| 17 | root bool |
| 18 | run interactiveDaselExecutor |
| 19 | output string |
| 20 | outputViewport viewport.Model |
| 21 | outputViewportReady bool |
| 22 | } |
| 23 | |
| 24 | func newInteractiveOutputModel(sharedData *interactiveSharedData, root bool, run interactiveDaselExecutor) *interactiveOutputModel { |
| 25 | m := &interactiveOutputModel{ |
| 26 | sharedData: sharedData, |
| 27 | root: root, |
| 28 | run: run, |
| 29 | } |
| 30 | m.outputViewport = viewport.New(10, 10) |
| 31 | return m |
| 32 | } |
| 33 | |
| 34 | func (m *interactiveOutputModel) Init() tea.Cmd { |
| 35 | return nil |
| 36 | } |
| 37 | |
| 38 | func (m *interactiveOutputModel) setOutput(output string) { |
| 39 | m.output = output |
| 40 | if m.outputViewportReady { |
| 41 | m.outputViewport.SetContent(m.output) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | func (m *interactiveOutputModel) setSize(width int, height int) { |
| 46 | if !m.outputViewportReady { |
| 47 | m.outputViewportReady = true |
| 48 | } |
| 49 | |
| 50 | m.outputViewport.Width = width |
| 51 | m.outputViewport.Height = height |
| 52 | m.outputViewport.SetContent(m.output) |
| 53 | } |
| 54 | |
| 55 | func (m *interactiveOutputModel) setVerticalPosition(pos int) { |
| 56 | m.outputViewport.YPosition = pos |
| 57 | } |
| 58 | |
| 59 | func (m *interactiveOutputModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 60 | var cmds []tea.Cmd |
| 61 | var cmd tea.Cmd |
| 62 | |
| 63 | defer func() { |
| 64 | m.lastSeenSelector = m.sharedData.selector |
| 65 | m.lastSeenFormatIn = m.sharedData.formatIn |
| 66 | m.lastSeenFormatOut = m.sharedData.formatOut |
| 67 | m.lastSeenInput = m.sharedData.input |
| 68 | }() |
| 69 | firstUpdate := !m.hasUpdatedBefore |
| 70 | m.hasUpdatedBefore = true |
| 71 | |
| 72 | queryChanged := m.lastSeenSelector != m.sharedData.selector || |
| 73 | m.lastSeenFormatIn != m.sharedData.formatIn || |
| 74 | m.lastSeenFormatOut != m.sharedData.formatOut || |
| 75 | m.lastSeenInput != m.sharedData.input |
| 76 | |
| 77 | // Take care of dasel execution + output. |
| 78 | if firstUpdate || queryChanged { |
| 79 | m.setOutput("Executing...") |
| 80 | out, err := m.run(m.sharedData.selector, m.root, m.sharedData.formatIn, m.sharedData.formatOut, m.sharedData.input) |
| 81 | if err != nil { |
| 82 | m.setOutput(err.Error()) |
| 83 | } else { |
| 84 | m.setOutput(out) |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | m.outputViewport, cmd = m.outputViewport.Update(msg) |
| 89 | cmds = append(cmds, cmd) |
| 90 | |
| 91 | return m, tea.Batch(cmds...) |
| 92 | } |
| 93 | |
| 94 | func (m *interactiveOutputModel) View() string { |
| 95 | title := "Result" |
| 96 | if m.root { |
| 97 | title = "Root" |
| 98 | } |
| 99 | |
| 100 | content := "Initializing..." |
| 101 | if m.outputViewportReady { |
| 102 | content = m.outputViewport.View() |
| 103 | } |
| 104 | |
| 105 | return lipgloss.JoinVertical(lipgloss.Left, outputHeaderStyle.Render(title), outputContentStyle.Render(content)) |
| 106 | } |