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

Commit

Permalink
add adaptive color
Browse files Browse the repository at this point in the history
  • Loading branch information
ingbyr committed Sep 16, 2022
1 parent cb46376 commit 2f97223
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 31 deletions.
4 changes: 4 additions & 0 deletions tui/confirm_view.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tui

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

Expand Down Expand Up @@ -41,6 +43,8 @@ func NewConfirmView(model *Model) *ConfirmView {
}

func (v *ConfirmView) Init() tea.Cmd {
v.model.setShortHelp(StateConfirmView, []key.Binding{keys.Up, keys.Down, keys.Enter, keys.Esc})
v.model.setFullHelp(StateConfirmView, [][]key.Binding{{keys.Up, keys.Down, keys.Enter, keys.Esc}})
return nil
}

Expand Down
29 changes: 21 additions & 8 deletions tui/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type EditorView struct {
width, height int
}

func NewTextView(model *Model) *EditorView {
func NewEditorView(model *Model) *EditorView {
hostEditor := textarea.New()
hostEditor.ShowLineNumbers = true
hostEditor.CharLimit = 0
Expand All @@ -41,7 +41,6 @@ func NewTextView(model *Model) *EditorView {
}

func (v *EditorView) Init() tea.Cmd {
km := v.hostEditor.KeyMap
v.model.setShortHelp(StateEditorView, []key.Binding{
keys.Up,
keys.Down,
Expand All @@ -51,14 +50,14 @@ func (v *EditorView) Init() tea.Cmd {
keys.Esc,
})
v.model.setFullHelp(StateEditorView, [][]key.Binding{
{keys.Up, keys.Down, keys.Left, keys.Right, keys.Save},
{km.CharacterForward, km.CharacterBackward}, // TODO add all key map from textarea.KeyMap
{keys.Up, keys.Down, keys.Left, keys.Right, keys.Save, keys.Esc},
})
v.SetHostNode(svc.SysHostNode)
return nil
}

func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
host := v.Host()
var cmd tea.Cmd
var cmds []tea.Cmd
switch m := msg.(type) {
Expand All @@ -74,18 +73,22 @@ func (v *EditorView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(m, keys.Esc):
return v, nil
case key.Matches(m, keys.Save):
host := v.Host()
if host.IsEditable() {
host.SetContent([]byte(v.hostEditor.Value()))
svc.UpdateNode(v.hostNode)
v.SetSaved()
} else {
v.statusMsg = "Can not edit this"
}
case key.Matches(m, keys.Up, keys.Down):
v.hostEditor, cmd = v.hostEditor.Update(msg)
msg = nil
}
}
if host.IsEditable() {
v.hostEditor, cmd = v.hostEditor.Update(msg)
} else {
v.statusMsg = "Can not edit this file"
}
v.RefreshStatusLine()
v.hostEditor, cmd = v.hostEditor.Update(msg)
cmds = append(cmds, cmd)
return v, tea.Batch(cmds...)
}
Expand Down Expand Up @@ -124,6 +127,7 @@ func (v *EditorView) SetHostNode(hostNode *gohost.TreeNode) {
v.hostEditor.Reset()
v.hostEditor.SetValue(string(hostNode.Node.(gohost.Host).GetContent()))
v.prevLen = v.hostEditor.Length()
v.statusMsg = ""
}

func (v *EditorView) RefreshStatusLine() {
Expand All @@ -146,3 +150,12 @@ func (v *EditorView) SetSaved() {
v.prevLen = v.hostEditor.Length()
v.saved = true
}

func (v *EditorView) FullHelp() [][]key.Binding {
keyMap := v.hostEditor.KeyMap
return [][]key.Binding{
{keyMap.CharacterBackward, keyMap.CharacterForward},
{keyMap.DeleteAfterCursor, keyMap.DeleteBeforeCursor},
{keyMap.DeleteCharacterBackward, keyMap.DeleteCharacterForward},
}
}
10 changes: 5 additions & 5 deletions tui/help_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type HelpView struct {
model *Model
helpView help.Model
helpModel help.Model
shortHelp map[State][]key.Binding
fullHelp map[State][][]key.Binding
enableDebug bool
Expand All @@ -17,7 +17,7 @@ type HelpView struct {
func NewHelpView(model *Model) *HelpView {
return &HelpView{
model: model,
helpView: help.New(),
helpModel: help.New(),
shortHelp: make(map[State][]key.Binding, 8),
fullHelp: make(map[State][][]key.Binding, 8),
enableDebug: true,
Expand All @@ -31,13 +31,13 @@ func (h *HelpView) Init() tea.Cmd {
func (h *HelpView) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h.helpView.Width = msg.Width
h.helpModel.Width = msg.Width
}
return h, nil
}

func (h *HelpView) View() string {
return h.helpView.View(h)
return h.helpModel.View(h)
}

func (h *HelpView) ShortHelp() []key.Binding {
Expand All @@ -49,7 +49,7 @@ func (h *HelpView) FullHelp() [][]key.Binding {
}

func (h *HelpView) Width() int {
return h.helpView.Width
return h.helpModel.Width
}

func (h *HelpView) SetShortHelp(state State, kb []key.Binding) {
Expand Down
10 changes: 3 additions & 7 deletions tui/keys/keymap.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ var (
)
Esc = key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "switch group or exit"),
key.WithHelp("esc", "left/exit"),
)
Enter = key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select or confirm"),
key.WithHelp("enter", "select/confirm"),
)
Switch = key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("tab", "switch helpView"),
key.WithHelp("tab", "switch next view"),
)
Save = key.NewBinding(
key.WithKeys("ctrl+s"),
Expand All @@ -56,7 +56,3 @@ var (
key.WithHelp("a", "apply"),
)
)

func Arrows() []key.Binding {
return []key.Binding{Up, Down, Left, Right}
}
7 changes: 4 additions & 3 deletions tui/main_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewModel() (*Model, error) {
model.helpView = NewHelpView(model)
model.confirmView = NewConfirmView(model)
model.treeView = NewTreeView(model)
model.editorView = NewTextView(model)
model.editorView = NewEditorView(model)
model.nodeView = NewNodeView(model)
return model, nil
}
Expand Down Expand Up @@ -86,6 +86,7 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch {
case key.Matches(msg, keys.Switch):
m.switchNextState()
m.helpView.helpModel.ShowAll = false
case key.Matches(msg, keys.ForceQuit):
cmds = append(cmds, tea.Quit)
case key.Matches(msg, keys.Esc):
Expand All @@ -97,10 +98,10 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.Help):
if m.state != StateHelpView {
m.switchState(StateHelpView)
m.helpView.helpView.ShowAll = true
m.helpView.helpModel.ShowAll = true
} else {
m.switchState(m.preState)
m.helpView.helpView.ShowAll = false
m.helpView.helpModel.ShowAll = false
}
default:
switch m.state {
Expand Down
4 changes: 3 additions & 1 deletion tui/node_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tui

import (
"fmt"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"gohost/gohost"
Expand Down Expand Up @@ -125,7 +126,8 @@ func NewNodeView(model *Model) *NodeView {
}

func (v *NodeView) Init() tea.Cmd {
v.model.setShortHelp(StateNodeView, keys.Arrows())
v.model.setShortHelp(StateNodeView, []key.Binding{keys.Up, keys.Down, keys.Enter})
v.model.setFullHelp(StateNodeView, [][]key.Binding{{keys.Up, keys.Down, keys.Enter}})
return nil
}

Expand Down
6 changes: 3 additions & 3 deletions tui/styles/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ var (
PaddingLeft(padding).
PaddingRight(padding).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("243"))
BorderForeground(lipgloss.AdaptiveColor{Light: "244", Dark: "244"})
FocusedView = lipgloss.NewStyle().
PaddingLeft(padding).
PaddingRight(padding).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("73"))
BorderForeground(lipgloss.AdaptiveColor{Light: "28", Dark: "34"})
UnfocusedFormItem = lipgloss.NewStyle()
FocusedFormItem = lipgloss.NewStyle().Foreground(lipgloss.Color("73"))
FocusedFormItem = lipgloss.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "28", Dark: "34"})

StatusLine = lipgloss.NewStyle()
)
11 changes: 7 additions & 4 deletions tui/tree_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (d *nodeItemDelegate) SetWidth(width int) {
d.width = width
}

// TreeView is tui helpView for nodes tree
// TreeView is tui helpModel for nodes tree
type TreeView struct {
model *Model
nodeList list.Model
Expand All @@ -112,7 +112,7 @@ type TreeView struct {
}

func NewTreeView(model *Model) *TreeView {
// Create nodes list helpView
// Create nodes list helpModel
nodes := svc.TreeNodesAsItem()
delegate := newNodeItemDelegate()
nodeList := list.New(nodes, delegate, 0, 0)
Expand All @@ -133,8 +133,11 @@ func NewTreeView(model *Model) *TreeView {
}

func (v *TreeView) Init() tea.Cmd {
v.model.setShortHelp(StateTreeView, []key.Binding{keys.Create, keys.Delete, keys.Apply, keys.Save, keys.ForceQuit})
v.model.setFullHelp(StateTreeView, append(v.nodeList.FullHelp(), []key.Binding{keys.Create}))
v.model.setShortHelp(StateTreeView, []key.Binding{keys.Create, keys.Delete, keys.Apply, keys.Save, keys.ForceQuit, keys.Help})
v.model.setFullHelp(StateTreeView,
append(v.nodeList.FullHelp(),
[]key.Binding{keys.Create, keys.Delete, keys.Apply, keys.Save, keys.ForceQuit, keys.Help}),
)
return nil
}

Expand Down

0 comments on commit 2f97223

Please sign in to comment.