z_mirror/pesticide
git clone https://git.raiguard.me/z_mirror/pesticide
388f8ba
trunk
1package ui 2 3import ( 4"log" 5 6"github.com/charmbracelet/bubbles/textinput" 7 tea"github.com/charmbracelet/bubbletea" 8"github.com/charmbracelet/lipgloss" 9"codeberg.org/raiguard/pesticide/command" 10) 11 12type prompt struct { 13commandHistory CommandHistory 14textinput textinput.Model 15} 16 17func (p * prompt )Init () { 18p .textinput = textinput .New () 19p .textinput .Prompt = lipgloss .NewStyle ().Foreground (lipgloss .Color ("12" )).Render ("(pesticide) " ) 20p .textinput .Focus () 21} 22 23func (p * prompt )Update (msg tea.Msg ,cmds * []tea.Cmd ) command.Command { 24var cmd tea.Cmd 25p .textinput ,cmd = p .textinput .Update (msg ) 26if cmd != nil { 27* cmds = append (* cmds ,cmd ) 28 } 29keyMsg ,ok := msg .(tea.KeyMsg ) 30if ! ok { 31return nil 32 } 33switch keyMsg .Type { 34case tea .KeyCtrlC : 35p .textinput .SetValue ("" ) 36case tea .KeyEnter : 37input := p .textinput .Value () 38* cmds = append (* cmds ,tea .Println (p .textinput .Prompt ,input )) 39if len (input )== 0 { 40p .commandHistory .Up () 41input = p .commandHistory .Get () 42p .commandHistory .Down () 43 }else { 44p .commandHistory .Append (input ) 45 } 46p .textinput .SetValue ("" ) 47cmd ,err := command .Parse (input ) 48if err != nil { 49* cmds = append (* cmds ,tea .Println (err )) 50break 51 } 52log .Printf ("Command: %s" ,input ) 53return cmd 54case tea .KeyUp ,tea .KeyCtrlP : 55p .commandHistory .Up () 56p .textinput .SetValue (p .commandHistory .Get ()) 57p .textinput .SetCursor (999 ) 58case tea .KeyDown ,tea .KeyCtrlN : 59p .commandHistory .Down () 60p .textinput .SetValue (p .commandHistory .Get ()) 61p .textinput .SetCursor (999 ) 62 } 63return nil 64} 65 66func (p * prompt )View ()string { 67return p .textinput .View () 68}