Skip to content

Commit

Permalink
refactor(status): show help in status
Browse files Browse the repository at this point in the history
  • Loading branch information
idursun committed Mar 3, 2025
1 parent 02c51ab commit 35f33a1
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 47 deletions.
12 changes: 12 additions & 0 deletions internal/ui/common/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package common
import (
"bytes"
tea "github.com/charmbracelet/bubbletea"
"github.com/idursun/jjui/internal/ui/operations"
"os/exec"
)

type AppContext interface {
Op() operations.Operation
SetOp(op operations.Operation)
SelectedItem() SelectedItem
SetSelectedItem(item SelectedItem)
RunCommandImmediate(args []string) ([]byte, error)
Expand All @@ -28,6 +31,15 @@ type SelectedFile struct {
type MainContext struct {
selectedItem SelectedItem
location string
op operations.Operation
}

func (a *MainContext) Op() operations.Operation {
return a.op
}

func (a *MainContext) SetOp(op operations.Operation) {
a.op = op
}

func (a *MainContext) SelectedItem() SelectedItem {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/operations/abandon/abandon_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (a Operation) Render() string {
}

func (a Operation) Name() string {
return "Abandon"
return "abandon"
}

func NewOperation(context common.AppContext, selected *jj.Commit) (operations.Operation, tea.Cmd) {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/operations/git/git_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (o *Operation) Render() string {
}

func (o *Operation) Name() string {
return "Git"
return "git"
}

var (
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/operations/rebase/rebase_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (r *Operation) Render() string {
}

func (r *Operation) Name() string {
return "Rebase"
return "rebase"
}

func NewOperation(context common.AppContext, from string, source Source, target Target) *Operation {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/operations/squash/squash_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s *Operation) RenderPosition() operations.RenderPosition {
}

func (s *Operation) Name() string {
return "Squash"
return "squash"
}

func (s *Operation) ShortHelp() []key.Binding {
Expand Down
2 changes: 1 addition & 1 deletion internal/ui/operations/undo/undo_operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (o Operation) Render() string {
}

func (o Operation) Name() string {
return "Undo"
return "undo"
}

func NewOperation(context common.AppContext) (operations.Operation, tea.Cmd) {
Expand Down
3 changes: 3 additions & 0 deletions internal/ui/revisions/revisions.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (m *Model) Init() tea.Cmd {
}

func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
defer func() {
m.context.SetOp(m.op)
}()
cmds := make([]tea.Cmd, 0)
preSelectedRevision := m.SelectedRevision()
switch msg := msg.(type) {
Expand Down
63 changes: 41 additions & 22 deletions internal/ui/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,36 @@ package status

import (
"fmt"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/idursun/jjui/internal/ui/common"
"time"
)

type Model struct {
context common.AppContext
spinner spinner.Model
mode string
help help.Model
command string
running bool
output string
error error
width int
}

const CommandClearDuration = 3 * time.Second

var (
normalStyle = lipgloss.NewStyle()
successStyle = lipgloss.NewStyle().Inherit(normalStyle).Foreground(common.Green)
errorStyle = lipgloss.NewStyle().Inherit(normalStyle).Foreground(common.Red)
modeStyle = lipgloss.NewStyle().Inherit(normalStyle).Foreground(common.Black).Background(common.DarkBlue)
)

type clearMsg struct{}

func (m *Model) Width() int {
return m.width
}
Expand All @@ -31,23 +45,17 @@ func (m *Model) SetWidth(w int) {
}

func (m *Model) SetHeight(int) {}

func (m *Model) SetMode(mode string) {
m.mode = mode
}

var (
normalStyle = lipgloss.NewStyle()
successStyle = lipgloss.NewStyle().Inherit(normalStyle).Foreground(common.Green)
errorStyle = lipgloss.NewStyle().Inherit(normalStyle).Foreground(common.Red)
)

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

func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case clearMsg:
m.command = ""
m.error = nil
m.output = ""
return m, nil
case common.CommandRunningMsg:
m.command = string(msg)
m.running = true
Expand All @@ -56,38 +64,49 @@ func (m *Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.running = false
m.output = msg.Output
m.error = msg.Err
return m, tea.Tick(CommandClearDuration, func(time.Time) tea.Msg {
return clearMsg{}
})
default:
var cmd tea.Cmd
m.spinner, cmd = m.spinner.Update(msg)
return m, cmd
}
return m, nil
}

func (m *Model) View() string {
s := normalStyle.Render(" ")
if !m.running {
if m.error != nil {
s = errorStyle.Render("✗ ")
} else if m.command != "" {
s = successStyle.Render("✓ ")
}
} else {
if m.running {
s = normalStyle.Render(m.spinner.View())
} else if m.error != nil {
s = errorStyle.Render("✗ ")
} else if m.command != "" {
s = successStyle.Render("✓ ")
} else {
if o, ok := m.context.Op().(help.KeyMap); ok {
s = m.help.View(o)
}
}
ret := normalStyle.Width(m.width - 2).SetString(m.command).Render()
ret = lipgloss.JoinHorizontal(lipgloss.Left, m.mode, s, ret)
mode := modeStyle.Width(10).Render(m.context.Op().Name())
ret = lipgloss.JoinHorizontal(lipgloss.Left, mode, " ", s, ret)
if m.error != nil {
ret += " " + errorStyle.Render(fmt.Sprintf("\n%v\n%s", m.error, m.output))
}
return ret
}

func New() Model {
func New(context common.AppContext) Model {
s := spinner.New()
s.Spinner = spinner.Dot
h := help.New()
h.Styles.ShortKey = common.DefaultPalette.CommitShortStyle
h.Styles.ShortDesc = common.DefaultPalette.CommitIdRestStyle
h.ShortSeparator = " "
return Model{
context: context,
spinner: s,
help: h,
command: "",
running: false,
output: "",
Expand Down
23 changes: 3 additions & 20 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package ui

import (
"fmt"
"strings"

"github.com/charmbracelet/bubbles/key"
"github.com/idursun/jjui/internal/jj"
"github.com/idursun/jjui/internal/ui/helppage"
Expand All @@ -16,7 +14,6 @@ import (
"github.com/idursun/jjui/internal/ui/revisions"
"github.com/idursun/jjui/internal/ui/status"

"github.com/charmbracelet/bubbles/help"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
Expand All @@ -34,7 +31,6 @@ type Model struct {
previewVisible bool
helpPage tea.Model
diff tea.Model
help help.Model
state common.State
error error
status tea.Model
Expand Down Expand Up @@ -163,21 +159,12 @@ func (m Model) View() string {
}

topView := m.revsetModel.View()

if m.state == common.Error {
topView += fmt.Sprintf("\nerror: %v\n", m.error)
}

topViewHeight := lipgloss.Height(topView)

var b strings.Builder
if h, ok := m.revisions.(help.KeyMap); ok {
b.WriteString(m.help.View(h))
b.WriteString("\n")
}
b.WriteString(m.status.View())

footer := b.String()
footer := m.status.View()
footerHeight := lipgloss.Height(footer)

if m.helpPage != nil {
Expand Down Expand Up @@ -208,20 +195,16 @@ func (m Model) View() string {
}

func New(c common.AppContext) tea.Model {
h := help.New()
h.Styles.ShortKey = common.DefaultPalette.CommitShortStyle
h.Styles.ShortDesc = common.DefaultPalette.CommitIdRestStyle
h.ShortSeparator = " "
c.SetOp(&operations.Noop{})
defaultRevSet, _ := c.RunCommandImmediate(jj.ConfigGet("revsets.log"))
revisionsModel := revisions.New(c)
previewModel := preview.New(c)
statusModel := status.New()
statusModel := status.New(c)
return Model{
context: c,
state: common.Loading,
revisions: &revisionsModel,
previewModel: &previewModel,
help: h,
status: &statusModel,
revsetModel: revset.New(string(defaultRevSet)),
}
Expand Down

0 comments on commit 35f33a1

Please sign in to comment.