Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/ui/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ type KeyMap struct {
ToggleHelp key.Binding
ToggleMessage key.Binding
ToggleSelection key.Binding
IncreaseFileTreeWidth key.Binding
DecreaseFileTreeWidth key.Binding
}

var keys = &KeyMap{
Expand Down Expand Up @@ -156,6 +158,14 @@ var keys = &KeyMap{
key.WithKeys("v"),
key.WithHelp("v", "toggle selection"),
),
IncreaseFileTreeWidth: key.NewBinding(
key.WithKeys(">"),
key.WithHelp(">", "increase file tree width"),
),
DecreaseFileTreeWidth: key.NewBinding(
key.WithKeys("<"),
key.WithHelp("<", "decrease file tree width"),
),
}

func KeyGroups() [][]key.Binding {
Expand All @@ -171,6 +181,7 @@ func KeyGroups() [][]key.Binding {
keys.CtrlU,
keys.ScrollLeft,
keys.ScrollRight,
keys.IncreaseFileTreeWidth,
}, {
keys.ToggleFileTree,
keys.SearchFiles,
Expand All @@ -183,6 +194,7 @@ func KeyGroups() [][]key.Binding {
keys.ToggleDiffView,
keys.ToggleIconStyle,
keys.ToggleSelection,
keys.DecreaseFileTreeWidth,
}, {
keys.ToggleMessage,
keys.ToggleHelp,
Expand Down
41 changes: 41 additions & 0 deletions pkg/ui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,15 @@ func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, keys.CtrlD, keys.CtrlU, keys.CtrlE, keys.CtrlY):
m.diffViewer, cmd = m.diffViewer.Update(msg)
cmds = append(cmds, cmd)

case key.Matches(msg, keys.IncreaseFileTreeWidth):
m, cmd = m.handleSidebarResize(minResizeStep)
cmds = append(cmds, cmd)

case key.Matches(msg, keys.DecreaseFileTreeWidth):
m, cmd = m.handleSidebarResize(-minResizeStep)
cmds = append(cmds, cmd)

default:
if m.activePanel == DiffViewerPanel {
m.diffViewer, cmd = m.diffViewer.Update(msg)
Expand Down Expand Up @@ -1324,6 +1333,38 @@ func (m mainModel) handleSidebarDrag(msg tea.MouseMsg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m mainModel) handleSidebarResize(resize int) (mainModel, tea.Cmd) {
if m.searchingFiles {
return m, nil
}

// Clamp to reasonable bounds.
minWidth := sidebarMinWidth
maxWidth := m.width / 2
newWidth := max(minWidth, min(maxWidth, m.sidebarWidth()+resize))

// Hide sidebar if resized below threshold.
if newWidth < sidebarHideWidth {
m.isShowingFileTree = false
cmd := m.diffViewer.SetSize(m.width, m.mainContentHeight())
return m, cmd
}

// TODO: for some reason setting a value smaller than minResizeStep
// will garble up the output when resizing. I have no idea why.
if abs(newWidth-m.sidebarWidth()) < minResizeStep {
return m, nil
}

// Resize components.
cmds := []tea.Cmd{}

cmds = append(cmds, m.diffViewer.SetSize(m.width-newWidth, m.mainContentHeight()))
m.fileTree.SetSize(newWidth-1, m.mainContentHeight()-searchHeight-1)

return m, tea.Batch(cmds...)
}

func abs(x int) int {
if x < 0 {
return -x
Expand Down