From b13768b2c5e6b00a62de4f80b7579db15fde0c21 Mon Sep 17 00:00:00 2001 From: RealDoc06 <73218979+RealDoc06@users.noreply.github.com> Date: Fri, 20 Nov 2026 21:14:37 +0100 Subject: [PATCH] feat(tui): calmer settings box, option to pack the chat list Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- internal/cli/cli.go | 4 ++-- internal/config/config.go | 7 ++++-- internal/tui/settings.go | 50 ++++++++++++++++++++++++++++----------- internal/tui/tui.go | 6 +++-- internal/tui/tui_test.go | 14 +++++++---- internal/tui/view.go | 24 ++++++++++++------- 7 files changed, 73 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index b094473..d95961d 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ Run `tg` with no arguments. | `ctrl+v` (`cmd+v` on a Mac) | Attach the image in the clipboard, the message becomes its caption | | `backspace` | Remove the attachment, when the message is empty | | `ctrl+c` | Clear the composer or cancel the edit, quit when it is empty | -| `esc` | In the chat list, open the settings: theme, spacing, how shortcuts are written | +| `esc` | In the chat list, open the settings: theme, spacing of messages and chats, how shortcuts are written | | `q` | Quit, from the chat list | The mouse works too: click a chat, scroll either pane, right click a message to reply, edit, copy its text or copy its ID. Copying uses OSC 52, so it also works over SSH; inside tmux it needs `set -g set-clipboard on`. Edits are sent as plain text, so bold and links made in another app are lost, and messages the composer cannot hold as they are (several lines, special characters) are edited elsewhere. New messages arrive live. What you type and leave unsent stays with its chat, shown in the list as a `[draft]`. diff --git a/internal/cli/cli.go b/internal/cli/cli.go index b922695..b5bf2bc 100644 --- a/internal/cli/cli.go +++ b/internal/cli/cli.go @@ -201,7 +201,7 @@ func (a *app) dispatch(ctx context.Context, args []string) error { label = name } look := func(f *config.File) tui.Look { - return tui.Look{Theme: f.Theme, Spaced: f.SpacedMessages, Keys: f.Keys} + return tui.Look{Theme: f.Theme, Spaced: f.SpacedMessages, Keys: f.Keys, PackedChats: f.PackedChats} } // The settings menu saves one change at a time, over config.json // as it is then. @@ -209,7 +209,7 @@ func (a *app) dispatch(ctx context.Context, args []string) error { return config.Update(func(f *config.File) error { l := look(f) change(&l) - f.Theme, f.SpacedMessages, f.Keys = l.Theme, l.Spaced, l.Keys + f.Theme, f.SpacedMessages, f.Keys, f.PackedChats = l.Theme, l.Spaced, l.Keys, l.PackedChats return nil }) } diff --git a/internal/config/config.go b/internal/config/config.go index 2239131..cc058e2 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -87,8 +87,11 @@ type File struct { // Keys is how the TUI writes shortcuts: "mac" with ⌥ and ⌘, "pc" with // alt+ and ctrl+. Empty follows the system tg runs on. The TUI's // settings menu (esc) sets it. - Keys string `json:"keys,omitempty"` - Accounts map[string]*Account `json:"accounts,omitempty"` + Keys string `json:"keys,omitempty"` + // PackedChats drops the blank row between chats in the TUI's chat list. + // The settings menu (esc) sets it. + PackedChats bool `json:"packed_chats,omitempty"` + Accounts map[string]*Account `json:"accounts,omitempty"` } // Account is one logged in Telegram account and its guardrails. Both diff --git a/internal/tui/settings.go b/internal/tui/settings.go index 21e65bd..efa36a1 100644 --- a/internal/tui/settings.go +++ b/internal/tui/settings.go @@ -6,6 +6,7 @@ import ( "strings" tea "charm.land/bubbletea/v2" + "github.com/charmbracelet/x/ansi" ) // The settings menu opens with esc from the chat list, as in btop. A change @@ -31,16 +32,22 @@ var settingsItems = []setting{ name := Themes[(i+delta+len(Themes))%len(Themes)].Name return func(l *Look) { l.Theme = name } }}, - {"Spacing", func(l Look) string { return map[bool]string{true: "on", false: "off"}[l.Spaced] }, func(l Look, _ int) func(*Look) { + {"Message spacing", func(l Look) string { return onOff[l.Spaced] }, func(l Look, _ int) func(*Look) { on := !l.Spaced return func(l *Look) { l.Spaced = on } }}, + {"Chat spacing", func(l Look) string { return onOff[!l.PackedChats] }, func(l Look, _ int) func(*Look) { + packed := !l.PackedChats + return func(l *Look) { l.PackedChats = packed } + }}, {"Shortcuts", func(l Look) string { return map[bool]string{true: "macOS", false: "PC"}[l.mac()] }, func(l Look, _ int) func(*Look) { keys := map[bool]string{true: "pc", false: "mac"}[l.mac()] return func(l *Look) { l.Keys = keys } }}, } +var onOff = map[bool]string{true: "on", false: "off"} + // mac reports whether shortcuts are written the way Mac keyboards label them. // Over SSH tg runs on another system than the keyboard, so it can be set. func (l Look) mac() bool { return l.Keys == "mac" || l.Keys == "" && runtime.GOOS == "darwin" } @@ -66,31 +73,46 @@ func (m *model) changeSetting(delta int) tea.Cmd { m.status = "the setting is not saved: " + err.Error() } } - return m.afterScroll() // spacing changes how many lines there are + // Spacing changes how many lines there are, and how many chats fit. + return tea.Batch(m.afterMove(), m.afterScroll()) } -// settingsWidth is the width of the settings menu. -const settingsWidth = 30 +// settingsWidth is the width of the settings box, borders included. +const settingsWidth = 38 -// settingsBox is where the settings menu is drawn: centered, a title line -// above one line per setting. +// The settings box is a rounded border with the title in its top edge and a +// blank row above and below the settings: setting i is on row settingsTop+i. +const settingsTop = 2 + +// settingsBox is where the settings box is drawn, centered over the panes. func (m *model) settingsBox() (x, y, w int) { w = min(settingsWidth, m.width) - return max(0, (m.width-w)/2), max(0, (m.bodyRows()-1-len(settingsItems))/2), w + h := len(settingsItems) + 2*settingsTop + return max(0, (m.width-w)/2), max(0, (m.bodyRows()-h)/2), w } -// settingsLines renders the settings menu, w cells wide. The highlighted -// setting shows arrows around its value, which change it. +// settingsLines renders the settings box, w cells wide. The highlighted +// setting is marked with a bar, as the selected chat is, and shows arrows +// around its value, which change it. func (m *model) settingsLines(w int) []string { - lines := []string{m.st.menu.Bold(true).Render(fit(" Settings", w))} + inner := max(0, w-2) + side := m.st.border.Render("│") + title := " Settings " + top := m.st.border.Render("╭─") + m.st.accent.Bold(true).Render(title) + + m.st.border.Render(strings.Repeat("─", max(0, inner-1-len(title)))+"╮") + blank := side + strings.Repeat(" ", inner) + side + + lines := []string{ansi.Truncate(top, w, ""), blank} for i, s := range settingsItems { - style, value := m.st.menu, " "+s.value(m.look)+" " + bar, name, value := " ", m.st.text.Render(s.name), m.st.muted.Render(s.value(m.look))+" " if i == m.settings.sel { - style, value = m.st.menuSel, "‹ "+s.value(m.look)+" ›" + bar = m.st.accent.Render("▌") + name = m.st.accent.Bold(true).Render(s.name) + value = m.st.text.Render("‹ "+s.value(m.look)+" ›") + " " } - lines = append(lines, style.Render(spread(" "+s.name, value+" ", w))) + lines = append(lines, side+bar+spread(" "+name, value, max(0, inner-1))+side) } - return lines + return append(lines, blank, m.st.border.Render("╰"+strings.Repeat("─", inner)+"╯")) } // keyName writes a key the way the keyboard labels it. On a Mac option and diff --git a/internal/tui/tui.go b/internal/tui/tui.go index 8375fc8..0e9db29 100644 --- a/internal/tui/tui.go +++ b/internal/tui/tui.go @@ -252,6 +252,8 @@ type Look struct { Theme string // one of Themes, "" for the default Spaced bool // a gap between messages of different senders or moments Keys string // "mac" or "pc" shortcut labels, "" for the system's + // PackedChats drops the blank row between chats in the list. + PackedChats bool } // Run starts the TUI and blocks until the user quits. account, when not @@ -384,7 +386,7 @@ func (m *model) update(msg tea.Msg) tea.Cmd { if m.settings != nil { // A click on a setting changes it, anywhere else closes the menu. x, y, w := m.settingsBox() - if i := msg.Y - y - 1; msg.Button == tea.MouseLeft && i >= 0 && i < len(settingsItems) && msg.X >= x && msg.X < x+w { + if i := msg.Y - y - settingsTop; msg.Button == tea.MouseLeft && i >= 0 && i < len(settingsItems) && msg.X >= x && msg.X < x+w { m.settings.sel = i return m.changeSetting(1) } @@ -420,7 +422,7 @@ func (m *model) update(msg tea.Msg) tea.Cmd { } return m.setFocus(focusInput) } - if k := (msg.Y - 1) / chatStride; (msg.Y-1)%chatStride < 2 && k < m.chatsDrawn() && m.top+k < len(m.visible()) { + if k := (msg.Y - 1) / m.chatStride(); (msg.Y-1)%m.chatStride() < 2 && k < m.chatsDrawn() && m.top+k < len(m.visible()) { m.sel = m.top + k return m.openSelected() } diff --git a/internal/tui/tui_test.go b/internal/tui/tui_test.go index 8033e20..72414c8 100644 --- a/internal/tui/tui_test.go +++ b/internal/tui/tui_test.go @@ -800,7 +800,7 @@ func TestClickOpensTheChatUnderIt(t *testing.T) { } // Below the last chat drawn, where the next one would go if it fit. if rows := m.listRows(); len(m.visible()) > rows { - m.Update(tea.MouseClickMsg{X: 10, Y: 1 + chatStride*rows, Button: tea.MouseLeft}) + m.Update(tea.MouseClickMsg{X: 10, Y: 1 + m.chatStride()*rows, Button: tea.MouseLeft}) if m.openID != 0 { t.Fatalf("a click below the list opened chat %d, which is not drawn", m.openID) } @@ -1025,16 +1025,22 @@ func TestSettingsShowAndSaveOneChange(t *testing.T) { t.Fatalf("theme is %q, drawn with %q", m.look.Theme, m.st.theme.Name) } m.Update(tea.KeyPressMsg{Code: tea.KeyDown}) - m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) // Spacing + m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) // Message spacing if !m.look.Spaced { - t.Fatal("spacing did not turn on") + t.Fatal("message spacing did not turn on") + } + m.Update(tea.KeyPressMsg{Code: tea.KeyDown}) + stride := screenRow(m, m.visible()[1].Title) - screenRow(m, m.visible()[0].Title) + m.Update(tea.KeyPressMsg{Code: tea.KeyEnter}) // Chat spacing + if packed := screenRow(m, m.visible()[1].Title) - screenRow(m, m.visible()[0].Title); packed != stride-1 { + t.Fatalf("chats are %d rows apart, %d before chat spacing turned off", packed, stride) } file := Look{Theme: "latte", Keys: "mac"} // changed elsewhere meanwhile for _, change := range saved { change(&file) } - if want := (Look{Theme: "macchiato", Spaced: true, Keys: "mac"}); file != want { + if want := (Look{Theme: "macchiato", Spaced: true, Keys: "mac", PackedChats: true}); file != want { t.Fatalf("config.json holds %+v, want %+v", file, want) } m.Update(tea.KeyPressMsg{Code: tea.KeyEscape}) diff --git a/internal/tui/view.go b/internal/tui/view.go index c8a1767..4107853 100644 --- a/internal/tui/view.go +++ b/internal/tui/view.go @@ -30,19 +30,25 @@ func (m *model) layout() (left, right int) { } // A chat takes two rows in the list, and chatGap blank ones set it apart -// from the next: chat k starts on row 1+chatStride*k, below the list's title. -const ( - chatGap = 1 - chatStride = 2 + chatGap -) +// from the next, none when the list is packed: chat k starts on row +// 1+chatStride*k, below the list's title. +func (m *model) chatGap() int { + if m.look.PackedChats { + return 0 + } + return 1 +} + +func (m *model) chatStride() int { return 2 + m.chatGap() } // bodyRows is how many rows the panes take: all but the status line and the // rule above it. func (m *model) bodyRows() int { return max(0, m.height-2) } -// chatsDrawn is how many chats fit in the list, possibly none. listRows is -// the same at least one, for paging and keeping the selection in view. -func (m *model) chatsDrawn() int { return m.bodyRows() / chatStride } +// chatsDrawn is how many chats fit in the list below its title, possibly +// none. listRows is the same at least one, for paging and keeping the +// selection in view. +func (m *model) chatsDrawn() int { return (m.bodyRows() - 1 + m.chatGap()) / m.chatStride() } func (m *model) listRows() int { return max(1, m.chatsDrawn()) } @@ -241,7 +247,7 @@ func (m *model) chatList(w, h int) []string { rows = append(rows, fit(" "+m.st.muted.Render(hint), w)) } for i := m.top; i < len(chats); i++ { - gap := min(i-m.top, 1) * chatGap // none above the first + gap := min(i-m.top, 1) * m.chatGap() // none above the first if len(rows)+gap+2 > h { break }