z_mirror/pesticide
git clone https://git.raiguard.me/z_mirror/pesticide
388f8ba
trunk
1package ui 2 3import ( 4"github.com/charmbracelet/bubbles/textinput" 5 tea"github.com/charmbracelet/bubbletea" 6"codeberg.org/raiguard/pesticide/adapter" 7"codeberg.org/raiguard/pesticide/config" 8) 9 10type Model struct { 11config config.Config 12adapters map [string ]* adapter.Adapter 13focusedAdapter * adapter.Adapter 14 15prompt prompt 16} 17 18func New (config config.Config )* tea.Program { 19return tea .NewProgram (& Model { 20config :config , 21adapters :map [string ]* adapter.Adapter {}, 22 }) 23} 24 25func (m * Model )Init () tea.Cmd { 26m .prompt .Init () 27return tea .Batch (textinput .Blink ,tea .Println ("Type a command and press <ret> to submit" )) 28} 29 30func (m * Model )Update (msg tea.Msg ) (tea.Model , tea.Cmd ) { 31var cmds []tea.Cmd 32switch msg := msg .(type ) { 33case tea.KeyMsg : 34switch msg .Type { 35case tea .KeyCtrlD : 36return m ,tea .Quit // Escape hatch for development 37 } 38case adapter.Msg : 39cmds = append (cmds ,m .handleAdapterMessage (msg )) 40 } 41cmd := m .prompt .Update (msg ,& cmds ) 42if cmd != nil { 43cmds = append (cmds ,m .handleCommand (cmd )) 44 } 45 46// Bubbletea does not validate sequence commands, so we must do it ourselves to avoid high CPU usage. 47var validCmds []tea.Cmd //nolint:prealloc 48for _ ,c := range cmds { 49if c == nil { 50continue 51 } 52validCmds = append (validCmds ,c ) 53 } 54switch len (validCmds ) { 55case 0 : 56return m ,nil 57case 1 : 58return m ,validCmds [0 ] 59default : 60return m ,tea .Sequence (cmds ... ) 61 } 62} 63 64func (m * Model )View ()string { 65return m .prompt .View () 66}