feat(ui): use t to toggle tree on/off

This commit is contained in:
Chris Wanstrath 2026-02-14 09:56:42 -08:00
parent a8303b51fa
commit dd4544b2d0

View File

@ -59,6 +59,7 @@ type Model struct {
focus Focus focus Focus
showHelp bool showHelp bool
flatMode bool flatMode bool
treeHidden bool
width, height int width, height int
} }
@ -202,6 +203,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() { switch msg.String() {
case "tab": case "tab":
if m.treeHidden {
return m, nil
}
if m.focus == FocusTree { if m.focus == FocusTree {
if item, ok := m.fileList.SelectedItem().(tree.TreeItem); ok && item.IsDir { if item, ok := m.fileList.SelectedItem().(tree.TreeItem); ok && item.IsDir {
return m, nil return m, nil
@ -224,8 +228,10 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.inputBuffer = "" m.inputBuffer = ""
case "h", "[", "ctrl+h", "left": case "h", "[", "ctrl+h", "left":
if !m.treeHidden {
m.focus = FocusTree m.focus = FocusTree
m.updateTreeFocus() m.updateTreeFocus()
}
m.inputBuffer = "" m.inputBuffer = ""
case "enter": case "enter":
@ -293,6 +299,18 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.inputBuffer = "" m.inputBuffer = ""
return m, nil return m, nil
case "t":
m.treeHidden = !m.treeHidden
if m.treeHidden {
m.focus = FocusDiff
} else {
m.focus = FocusTree
}
m.updateTreeFocus()
m.updateSizes()
m.inputBuffer = ""
return m, nil
case "z": case "z":
if m.focus == FocusDiff { if m.focus == FocusDiff {
m.pendingZ = true m.pendingZ = true
@ -478,20 +496,25 @@ func (m *Model) updateSizes() {
contentHeight = 1 contentHeight = 1
} }
treeWidth := int(float64(m.width) * 0.20)
if treeWidth < 20 {
treeWidth = 20
}
// Subtract border height (2) from contentHeight // Subtract border height (2) from contentHeight
listHeight := contentHeight - 2 listHeight := contentHeight - 2
if listHeight < 1 { if listHeight < 1 {
listHeight = 1 listHeight = 1
} }
m.fileList.SetSize(treeWidth, listHeight)
if m.treeHidden {
m.fileList.SetSize(0, listHeight)
m.diffViewport.Width = m.width - 2 // border (2)
m.diffViewport.Height = listHeight
} else {
treeWidth := int(float64(m.width) * 0.20)
if treeWidth < 20 {
treeWidth = 20
}
m.fileList.SetSize(treeWidth, listHeight)
m.diffViewport.Width = m.width - treeWidth - 4 // border (2) + padding (2) from tree pane m.diffViewport.Width = m.width - treeWidth - 4 // border (2) + padding (2) from tree pane
m.diffViewport.Height = listHeight m.diffViewport.Height = listHeight
}
} }
func (m *Model) updateTreeFocus() { func (m *Model) updateTreeFocus() {
@ -645,8 +668,12 @@ func (m Model) View() string {
rightPaneView = lipgloss.JoinVertical(lipgloss.Top, header, diffView) rightPaneView = lipgloss.JoinVertical(lipgloss.Top, header, diffView)
} }
if m.treeHidden {
mainContent = rightPaneView
} else {
mainContent = lipgloss.JoinHorizontal(lipgloss.Top, treeView, rightPaneView) mainContent = lipgloss.JoinHorizontal(lipgloss.Top, treeView, rightPaneView)
} }
}
var bottomBar string var bottomBar string
if m.showHelp { if m.showHelp {
@ -696,7 +723,7 @@ func (m Model) renderTopBar() string {
} }
func (m Model) viewStatusBar() string { func (m Model) viewStatusBar() string {
shortcuts := StatusKeyStyle.Render("? Help q Quit Tab Switch f Flat") shortcuts := StatusKeyStyle.Render("? Help q Quit Tab Switch f Flat t Tree")
return StatusBarStyle.Width(m.width).Render(shortcuts) return StatusBarStyle.Width(m.width).Render(shortcuts)
} }
@ -717,6 +744,7 @@ func (m Model) renderHelpDrawer() string {
HelpTextStyle.Render("H/M/L Move Cursor"), HelpTextStyle.Render("H/M/L Move Cursor"),
HelpTextStyle.Render("e Edit File"), HelpTextStyle.Render("e Edit File"),
HelpTextStyle.Render("f Flat Tree"), HelpTextStyle.Render("f Flat Tree"),
HelpTextStyle.Render("t Toggle Tree"),
) )
return HelpDrawerStyle.Copy(). return HelpDrawerStyle.Copy().