Skip to content

Commit

Permalink
central theme for ui components
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Apr 10, 2024
1 parent 7a769bd commit b663c0c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
5 changes: 5 additions & 0 deletions cmd/slackdump/internal/cfg/wizard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package cfg

import "github.com/charmbracelet/huh"

var Theme = huh.ThemeCharm() // Theme is the default Wizard theme.
8 changes: 5 additions & 3 deletions cmd/slackdump/internal/wizard/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type model struct {
val string
}

const kSelection = "selection" // selection key

func newModel(m *menu) model {
var options []huh.Option[string]
for i, name := range m.names {
Expand All @@ -27,12 +29,12 @@ func newModel(m *menu) model {
form: huh.NewForm(
huh.NewGroup(
huh.NewSelect[string]().
Key("selection").
Key(kSelection).
Title(m.title).
Description(currentWsp()).
Options(options...),
),
),
).WithTheme(cfg.Theme),
}
}

Expand All @@ -57,7 +59,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

if m.form.State == huh.StateCompleted {
m.val = m.form.GetString("selection")
m.val = m.form.GetString(kSelection)
cmds = append(cmds, tea.Quit)
}

Expand Down
32 changes: 16 additions & 16 deletions cmd/slackdump/internal/workspace/wizard.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ func wizSelect(ctx context.Context, cmd *base.Command, args []string) error {
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
Foreground(cfg.Theme.Focused.NoteTitle.GetForeground()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Foreground(cfg.Theme.Focused.Option.GetBackground()).
Background(cfg.Theme.Focused.SelectedOption.GetForeground()).
Bold(false)
t.SetStyles(s)

mod, err := tea.NewProgram(model{table: t}).Run()
if err != nil {
return fmt.Errorf("error running program: %w", err)
return fmt.Errorf("workspace select wizard error: %w", err)
}
if newWsp := mod.(model).selected; newWsp != "" {
if err := m.Select(newWsp); err != nil {
Expand All @@ -84,13 +85,16 @@ func wizSelect(ctx context.Context, cmd *base.Command, args []string) error {
return nil
}

var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
// var baseStyle = lipgloss.NewStyle().
// BorderStyle(lipgloss.NormalBorder()).
// BorderForeground(lipgloss.Color("240"))

var baseStyle = cfg.Theme.Form

type model struct {
table table.Model
selected string
finished bool
}

func (m model) Init() tea.Cmd { return nil }
Expand All @@ -100,16 +104,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
if m.table.Focused() {
m.table.Blur()
} else {
m.table.Focus()
}
case "q", "ctrl+c":
case "q", "ctrl+c", "esc":
m.finished = true
return m, tea.Quit
case "enter":
m.selected = m.table.SelectedRow()[1]
m.finished = true
return m, tea.Quit
}
}
Expand All @@ -118,8 +118,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

func (m model) View() string {
if m.selected == "" {
return baseStyle.Render(m.table.View()) + "\n"
if m.finished {
return "" // don't render the table if we've selected a workspace
}
return ""
return baseStyle.Render(m.table.View()) + "\n\n" + cfg.Theme.Help.Ellipsis.Render("Select the workspace with arrow keys, press [Enter] to confirm, [Esc] to cancel.")
}

0 comments on commit b663c0c

Please sign in to comment.