Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
add confirm view
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 15, 2022
1 parent 1423c95 commit 0df2ce9
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 155 deletions.
54 changes: 54 additions & 0 deletions tui/confirm_view.go
@@ -0,0 +1,54 @@
package tui

import (
tea "github.com/charmbracelet/bubbletea"
"gohost/tui/form"
"gohost/tui/styles"
)

type ConfirmView struct {
model *Model
*form.Form
tipLabel *form.Label
confirmButton *form.Button
cancelButton *form.Button
width, height int
}

func NewConfirmView(model *Model) *ConfirmView {
confirmForm := form.New()
confirmForm.SetItemFocusedStyle(styles.FocusedFormItem)
confirmForm.SetItemUnfocusedStyle(styles.UnfocusedFormItem)

tipLabel := form.NewLabel("Default label")
confirmForm.AddItem(tipLabel)

confirmButton := form.NewButton("Confirm")
confirmButton.Focus(form.FocusFirstMode)
confirmForm.AddItem(confirmButton)

cancelButton := form.NewButton("Cancel")
confirmForm.AddItem(cancelButton)
return &ConfirmView{
model: model,
Form: confirmForm,
tipLabel: tipLabel,
confirmButton: confirmButton,
cancelButton: cancelButton,
}
}

func (v *ConfirmView) Init() tea.Cmd {
return nil
}

func (v *ConfirmView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
v.width, v.height = msg.Width, msg.Height
case tea.KeyMsg:
}
_, cmd = v.Form.Update(msg)
return v, cmd
}
6 changes: 5 additions & 1 deletion tui/form/button.go
Expand Up @@ -13,7 +13,7 @@ var _ Item = (*Button)(nil)

func NewButton(text string) *Button {
return &Button{
Text: text,
Text: "[ " + text + " ]",
OnClick: func() tea.Cmd { return nil },
focused: false,
focusedStyle: styles.None,
Expand All @@ -31,6 +31,10 @@ type Button struct {
HideFunc HideCondition
}

func (b *Button) Focusable() bool {
return true
}

func (b *Button) Hide() bool {
if b.HideFunc == nil {
return false
Expand Down
4 changes: 4 additions & 0 deletions tui/form/choice.go
Expand Up @@ -47,6 +47,10 @@ type Choices struct {
selectedIndex int
}

func (c *Choices) Focusable() bool {
return true
}

func (c *Choices) Hide() bool {
if c.HideFunc == nil {
return false
Expand Down
20 changes: 18 additions & 2 deletions tui/form/form.go
Expand Up @@ -5,11 +5,27 @@ import (
"github.com/charmbracelet/bubbles/viewport"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"gohost/config"
"gohost/tui/keys"
"gohost/tui/styles"
"strings"
)

var cfg = config.Instance()

type Item interface {
tea.Model
Focus(mode FocusMode) tea.Cmd
Unfocus() tea.Cmd
InterceptKey(m tea.KeyMsg) bool
Focusable() bool
SetFocusedStyle(style lipgloss.Style)
SetUnfocusedStyle(style lipgloss.Style)
Hide() bool
}

type HideCondition func() bool

func New() *Form {
vp := viewport.New(0, 0)
return &Form{
Expand Down Expand Up @@ -165,7 +181,7 @@ func (v *Form) idxAfterFocusItem() int {
if idx >= len(v.Items) {
idx = 0
}
if !v.Items[idx].Hide() {
if !v.Items[idx].Hide() && v.Items[idx].Focusable() {
return idx
}
if idx == v.focus {
Expand All @@ -181,7 +197,7 @@ func (v *Form) idxBeforeFocusItem() int {
if idx < 0 {
idx = len(v.Items) - 1
}
if !v.Items[idx].Hide() {
if !v.Items[idx].Hide() && v.Items[idx].Focusable() {
return idx
}
if idx == v.focus {
Expand Down
21 changes: 0 additions & 21 deletions tui/form/item.go

This file was deleted.

59 changes: 59 additions & 0 deletions tui/form/label.go
@@ -0,0 +1,59 @@
package form

import (
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

var _ Item = (*Label)(nil)

func NewLabel(label string) *Label {
return &Label{label: label}
}

type Label struct {
label string
width, height int
}

func (l *Label) Focusable() bool {
return false
}

func (l *Label) Init() tea.Cmd {
return nil
}

func (l *Label) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
l.width, l.height = msg.Width, msg.Height
}
return l, nil
}

func (l *Label) View() string {
return l.label
}

func (l *Label) Focus(mode FocusMode) tea.Cmd {
return nil
}

func (l *Label) Unfocus() tea.Cmd {
return nil
}

func (l *Label) InterceptKey(m tea.KeyMsg) bool {
return false
}

func (l *Label) SetFocusedStyle(style lipgloss.Style) {
}

func (l *Label) SetUnfocusedStyle(style lipgloss.Style) {
}

func (l *Label) Hide() bool {
return false
}
4 changes: 4 additions & 0 deletions tui/form/textinput.go
Expand Up @@ -28,6 +28,10 @@ type TextInput struct {
unfocusedStyle lipgloss.Style
}

func (t *TextInput) Focusable() bool {
return true
}

func (t *TextInput) Hide() bool {
if t.HideFunc == nil {
return false
Expand Down
23 changes: 0 additions & 23 deletions tui/headtip.go

This file was deleted.

98 changes: 0 additions & 98 deletions tui/log_view.go

This file was deleted.

0 comments on commit 0df2ce9

Please sign in to comment.