mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-06 01:14:52 +01:00
reimplement agent,provider and add file history
This commit is contained in:
@@ -1,28 +1,58 @@
|
||||
package dialog
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/bubbles/key"
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/components/core"
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/layout"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/styles"
|
||||
"github.com/kujtimiihoxha/termai/internal/tui/util"
|
||||
|
||||
"github.com/charmbracelet/huh"
|
||||
)
|
||||
|
||||
const question = "Are you sure you want to quit?"
|
||||
|
||||
type CloseQuitMsg struct{}
|
||||
|
||||
type QuitDialog interface {
|
||||
tea.Model
|
||||
layout.Sizeable
|
||||
layout.Bindings
|
||||
}
|
||||
|
||||
type quitDialogCmp struct {
|
||||
form *huh.Form
|
||||
width int
|
||||
height int
|
||||
selectedNo bool
|
||||
}
|
||||
|
||||
type helpMapping struct {
|
||||
LeftRight key.Binding
|
||||
EnterSpace key.Binding
|
||||
Yes key.Binding
|
||||
No key.Binding
|
||||
Tab key.Binding
|
||||
}
|
||||
|
||||
var helpKeys = helpMapping{
|
||||
LeftRight: key.NewBinding(
|
||||
key.WithKeys("left", "right"),
|
||||
key.WithHelp("←/→", "switch options"),
|
||||
),
|
||||
EnterSpace: key.NewBinding(
|
||||
key.WithKeys("enter", " "),
|
||||
key.WithHelp("enter/space", "confirm"),
|
||||
),
|
||||
Yes: key.NewBinding(
|
||||
key.WithKeys("y", "Y"),
|
||||
key.WithHelp("y/Y", "yes"),
|
||||
),
|
||||
No: key.NewBinding(
|
||||
key.WithKeys("n", "N"),
|
||||
key.WithHelp("n/N", "no"),
|
||||
),
|
||||
Tab: key.NewBinding(
|
||||
key.WithKeys("tab"),
|
||||
key.WithHelp("tab", "switch options"),
|
||||
),
|
||||
}
|
||||
|
||||
func (q *quitDialogCmp) Init() tea.Cmd {
|
||||
@@ -30,77 +60,73 @@ func (q *quitDialogCmp) Init() tea.Cmd {
|
||||
}
|
||||
|
||||
func (q *quitDialogCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
var cmds []tea.Cmd
|
||||
form, cmd := q.form.Update(msg)
|
||||
if f, ok := form.(*huh.Form); ok {
|
||||
q.form = f
|
||||
cmds = append(cmds, cmd)
|
||||
}
|
||||
|
||||
if q.form.State == huh.StateCompleted {
|
||||
v := q.form.GetBool("quit")
|
||||
if v {
|
||||
switch msg := msg.(type) {
|
||||
case tea.KeyMsg:
|
||||
switch {
|
||||
case key.Matches(msg, helpKeys.LeftRight) || key.Matches(msg, helpKeys.Tab):
|
||||
q.selectedNo = !q.selectedNo
|
||||
return q, nil
|
||||
case key.Matches(msg, helpKeys.EnterSpace):
|
||||
if !q.selectedNo {
|
||||
return q, tea.Quit
|
||||
}
|
||||
return q, util.CmdHandler(CloseQuitMsg{})
|
||||
case key.Matches(msg, helpKeys.Yes):
|
||||
return q, tea.Quit
|
||||
case key.Matches(msg, helpKeys.No):
|
||||
return q, util.CmdHandler(CloseQuitMsg{})
|
||||
}
|
||||
cmds = append(cmds, util.CmdHandler(core.DialogCloseMsg{}))
|
||||
}
|
||||
|
||||
return q, tea.Batch(cmds...)
|
||||
return q, nil
|
||||
}
|
||||
|
||||
func (q *quitDialogCmp) View() string {
|
||||
return q.form.View()
|
||||
}
|
||||
yesStyle := styles.BaseStyle
|
||||
noStyle := styles.BaseStyle
|
||||
spacerStyle := styles.BaseStyle.Background(styles.Background)
|
||||
|
||||
func (q *quitDialogCmp) GetSize() (int, int) {
|
||||
return q.width, q.height
|
||||
}
|
||||
if q.selectedNo {
|
||||
noStyle = noStyle.Background(styles.PrimaryColor).Foreground(styles.Background)
|
||||
yesStyle = yesStyle.Background(styles.Background).Foreground(styles.PrimaryColor)
|
||||
} else {
|
||||
yesStyle = yesStyle.Background(styles.PrimaryColor).Foreground(styles.Background)
|
||||
noStyle = noStyle.Background(styles.Background).Foreground(styles.PrimaryColor)
|
||||
}
|
||||
|
||||
func (q *quitDialogCmp) SetSize(width int, height int) {
|
||||
q.width = width
|
||||
q.height = height
|
||||
q.form = q.form.WithWidth(width).WithHeight(height)
|
||||
yesButton := yesStyle.Padding(0, 1).Render("Yes")
|
||||
noButton := noStyle.Padding(0, 1).Render("No")
|
||||
|
||||
buttons := lipgloss.JoinHorizontal(lipgloss.Left, yesButton, spacerStyle.Render(" "), noButton)
|
||||
|
||||
width := lipgloss.Width(question)
|
||||
remainingWidth := width - lipgloss.Width(buttons)
|
||||
if remainingWidth > 0 {
|
||||
buttons = spacerStyle.Render(strings.Repeat(" ", remainingWidth)) + buttons
|
||||
}
|
||||
|
||||
content := styles.BaseStyle.Render(
|
||||
lipgloss.JoinVertical(
|
||||
lipgloss.Center,
|
||||
question,
|
||||
"",
|
||||
buttons,
|
||||
),
|
||||
)
|
||||
|
||||
return styles.BaseStyle.Padding(1, 2).
|
||||
Border(lipgloss.RoundedBorder()).
|
||||
BorderBackground(styles.Background).
|
||||
BorderForeground(styles.ForgroundDim).
|
||||
Width(lipgloss.Width(content) + 4).
|
||||
Render(content)
|
||||
}
|
||||
|
||||
func (q *quitDialogCmp) BindingKeys() []key.Binding {
|
||||
return q.form.KeyBinds()
|
||||
return layout.KeyMapToSlice(helpKeys)
|
||||
}
|
||||
|
||||
func newQuitDialogCmp() QuitDialog {
|
||||
confirm := huh.NewConfirm().
|
||||
Title(question).
|
||||
Affirmative("Yes!").
|
||||
Key("quit").
|
||||
Negative("No.")
|
||||
|
||||
theme := styles.HuhTheme()
|
||||
theme.Focused.FocusedButton = theme.Focused.FocusedButton.Background(styles.Warning)
|
||||
theme.Blurred.FocusedButton = theme.Blurred.FocusedButton.Background(styles.Warning)
|
||||
form := huh.NewForm(huh.NewGroup(confirm)).
|
||||
WithShowHelp(false).
|
||||
WithWidth(0).
|
||||
WithHeight(0).
|
||||
WithTheme(theme).
|
||||
WithShowErrors(false)
|
||||
confirm.Focus()
|
||||
func NewQuitCmp() QuitDialog {
|
||||
return &quitDialogCmp{
|
||||
form: form,
|
||||
selectedNo: true,
|
||||
}
|
||||
}
|
||||
|
||||
func NewQuitDialogCmd() tea.Cmd {
|
||||
content := layout.NewSinglePane(
|
||||
newQuitDialogCmp().(*quitDialogCmp),
|
||||
layout.WithSinglePaneBordered(true),
|
||||
layout.WithSinglePaneFocusable(true),
|
||||
layout.WithSinglePaneActiveColor(styles.Warning),
|
||||
)
|
||||
content.Focus()
|
||||
return util.CmdHandler(core.DialogMsg{
|
||||
Content: content,
|
||||
WidthRatio: 0.2,
|
||||
HeightRatio: 0.1,
|
||||
MinWidth: 40,
|
||||
MinHeight: 5,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user