Skip to content

Commit

Permalink
WIP: std TUI
Browse files Browse the repository at this point in the history
  • Loading branch information
blaggacao committed Apr 15, 2022
1 parent 2202ca9 commit 2272715
Show file tree
Hide file tree
Showing 14 changed files with 976 additions and 557 deletions.
80 changes: 80 additions & 0 deletions cells/std/cli/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package main

import (
"fmt"

"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)

var (
ActionStyle = lipgloss.NewStyle().
Width(30).Height(32).
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("63"))
)

type ActionModel struct {
List list.Model
}

func (m *ActionModel) Init() tea.Cmd { return nil }

func (m *ActionModel) Update(msg tea.Msg) (*ActionModel, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
}
m.List, cmd = m.List.Update(msg)
return m, cmd
case tea.WindowSizeMsg:
}
return m, cmd
}
func (m *ActionModel) View() string {
return ActionStyle.Render(m.List.View())
}

func NewAction(i item) *ActionModel {
var (
appKeys = NewAppKeyMap()
numItems = cap(i.actions)
)
// Make list of actions
items := make([]list.Item, numItems)
for j := 0; j < numItems; j++ {
items[j] = i.actions[j]
}
actionList := list.New(items, list.NewDefaultDelegate(), 0, 32)
actionList.Title = fmt.Sprintf("Actions for %s", i.clade)
actionList.KeyMap = DefaultListKeyMap()
actionList.AdditionalShortHelpKeys = func() []key.Binding {
return []key.Binding{
appKeys.toggleFocus,
}
}
actionList.AdditionalFullHelpKeys = func() []key.Binding {
return []key.Binding{
appKeys.toggleFocus,
}
}
actionList.SetShowPagination(false)
actionList.SetShowHelp(false)
actionList.SetShowStatusBar(false)
actionList.SetFilteringEnabled(false)

return &ActionModel{actionList}
}

type action struct {
name string `json:"__action_name"`
command []string `json:"__action_command"`
description string `json:"__action_description"`
}

func (a action) Title() string { return a.name }
func (a action) Description() string { return a.description }
func (a action) FilterValue() string { return a.Title() }
82 changes: 0 additions & 82 deletions cells/std/cli/delegate.go

This file was deleted.

1 change: 0 additions & 1 deletion cells/std/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@ require (
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/treilik/bubbleboxer v0.1.0
)
3 changes: 1 addition & 2 deletions cells/std/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0
github.com/containerd/console v1.0.2/go.mod h1:ytZPjGgY2oeTkAONYafi2kSj0aYggsf8acV1PGKCbzQ=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
Expand All @@ -37,8 +38,6 @@ github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/treilik/bubbleboxer v0.1.0 h1:xA1dxzKGRDHTUiaj+p1MHWKtIMCpGIqir3IuSla74rw=
github.com/treilik/bubbleboxer v0.1.0/go.mod h1:fbg7Cm6Ex9qkQnN2jN6O/NZN49q8uAyKdmPcb4Y59kc=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
90 changes: 90 additions & 0 deletions cells/std/cli/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/list"
)

type AppKeyMap struct {
toggleFocus key.Binding
forceQuit key.Binding
}

func NewAppKeyMap() *AppKeyMap {
return &AppKeyMap{
// Swiching focus.
toggleFocus: key.NewBinding(
key.WithKeys("tab"),
key.WithHelp("⇥", "toggle focus"),
),

// Quitting.
forceQuit: key.NewBinding(key.WithKeys("ctrl+c")),
}
}

// DefaultListKeyMap returns a default set of keybindings.
func DefaultListKeyMap() list.KeyMap {
return list.KeyMap{
// Browsing.
CursorUp: key.NewBinding(
key.WithKeys("up"),
key.WithHelp("↑", "up"),
),
CursorDown: key.NewBinding(
key.WithKeys("down"),
key.WithHelp("↓", "down"),
),
PrevPage: key.NewBinding(
key.WithKeys("pgup"),
key.WithHelp("pgup", "prev page"),
),
NextPage: key.NewBinding(
key.WithKeys("pgdown"),
key.WithHelp("pgdn", "next page"),
),
GoToStart: key.NewBinding(
key.WithKeys("home"),
key.WithHelp("home", "go to start"),
),
GoToEnd: key.NewBinding(
key.WithKeys("end"),
key.WithHelp("end", "go to end"),
),
Filter: key.NewBinding(
key.WithKeys("/"),
key.WithHelp("/", "filter"),
),
ClearFilter: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "clear filter"),
),

// Filtering.
CancelWhileFiltering: key.NewBinding(
key.WithKeys("esc"),
key.WithHelp("esc", "cancel"),
),
AcceptWhileFiltering: key.NewBinding(
key.WithKeys("enter", "tab", "up", "down"),
key.WithHelp("enter", "apply filter"),
),

// Toggle help.
ShowFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "more"),
),
CloseFullHelp: key.NewBinding(
key.WithKeys("?"),
key.WithHelp("?", "close help"),
),

// Quitting.
Quit: key.NewBinding(
key.WithKeys("q"),
key.WithHelp("q", "quit"),
),
ForceQuit: key.NewBinding(key.WithKeys("ctrl+c")),
}
}

0 comments on commit 2272715

Please sign in to comment.