Skip to content
Merged
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Termcourse is a Go 1.27.2 terminal UI for browsing and posting to Discourse foru
- Create topics, select categories, reply to topics or posts, and like/unlike posts.
- Search posts and open the matching post context.
- Browse/filter notifications and jump to their topic/post.
- Consistent rounded main panes in every view, with headers sharing the main background and subdued backgrounds for editable fields. Search entry stays in the header above the results pane.
- Persistent folder tabs above each screen panel: Topics, Search, Notifications, and Compose. Open topics and images remain within the destination that led to them.
- A contextual second rail for topic/notification filters plus search and composition stages, with one spacer row below the masthead.
- Theme-colored folder rails and responsive, clickable footer controls for screen-specific hotkeys, with pointer-hover highlighting and the active theme anchored at bottom right.
Expand Down Expand Up @@ -203,6 +204,8 @@ The five built-in themes are `default`, `slate`, `fairground`, `rust`, and `hack

See [theme.example.yml](theme.example.yml) for partial built-in overrides and custom themes. Supported keys are `primary`, `background`, `highlighted`, `highlighted_text`, `borders`, `bar_backgrounds`, `separators`, `list_numbers`, `list_text`, `post_username`, `list_meta`, and `accent`. Colors accept `#rrggbb`, indexes `0`–`255`, `black`, `white`, `red`, `green`, `blue`, `yellow`, `cyan`, `magenta`, `gray`/`grey`, or `none`. Invalid fields, colors, files, and theme names produce actionable errors.

Headers and main panes use `background`; headers with no content are omitted and their title moves to the main pane border. `bar_backgrounds` continues to style progress bars. Editable fields use a subdued tint derived from the theme.

### Language

Termcourse includes 16 languages with 17 locale options:
Expand Down
21 changes: 17 additions & 4 deletions internal/ui/background_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,14 @@ func backgroundTestLines(text string) []uv.Line {

func assertBackground(t *testing.T, got, want color.Color) {
t.Helper()
if got == nil || want == nil {
_, defaultBackground := want.(lipgloss.NoColor)
if want == nil || defaultBackground {
if got != nil {
t.Fatalf("background = %v, want terminal default", got)
}
return
}
if got == nil {
t.Fatalf("missing background: got %v want %v", got, want)
}
r, g, b, a := got.RGBA()
Expand Down Expand Up @@ -83,13 +90,19 @@ func TestPanelBackgroundSurvivesNestedStyles(t *testing.T) {
t.Setenv("TERMCOURSE_COLOR_MODE", mode)
s := NewStyle(value, &bytes.Buffer{})
body := s.Text("label", roleAccent) + " " + s.Text("value", roleListMeta) + " tail"
for _, lines := range [][]string{s.HeaderBox("PANEL", []string{body, ""}, 36), s.BarBox("BAR", []string{body, ""}, 36)} {
for _, line := range backgroundTestLines(strings.Join(lines, "\n")) {
for _, panel := range []struct {
lines []string
background color.Color
}{
{s.HeaderBox("PANEL", []string{body, ""}, 36), s.body.GetBackground()},
{s.BarBox("BAR", []string{body, ""}, 36), s.bar.GetBackground()},
} {
for _, line := range backgroundTestLines(strings.Join(panel.lines, "\n")) {
if len(line) != 36 {
t.Fatalf("panel width = %d", len(line))
}
for _, cell := range line {
assertBackground(t, cell.Style.Bg, s.header.GetBackground())
assertBackground(t, cell.Style.Bg, panel.background)
}
}
}
Expand Down
90 changes: 54 additions & 36 deletions internal/ui/composer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,7 @@ func (u *UI) promptSingleLine(titleKey, promptKey, prefix string, active primary
u.terminal.Run(input.Focus())
for {
u.primaryNavAllowed = active == primarySearch || (active == primaryCompose && strings.TrimSpace(input.Value()) == "")
width, height := u.terminal.Size()
input.SetWidth(max(width, 1))
header := u.navigationHeader(u.t(titleKey), active, context, selected, "", []string{u.t(promptKey)}, width, height)
screen := make([]string, height)
copy(screen, header)
row := min(len(header)+1, height-1)
screen[row] = input.View()
u.placeControlsFooter(screen, u.t("ui.controls.single_line"), width)
u.renderer.Render(screen, width, height, "single-line-"+titleKey, -1, -1, false)
u.renderSingleLine(titleKey, promptKey, &input, active, context, selected)
msg, err := u.terminal.ReadMsg(u.tick)
if err != nil {
return ""
Expand Down Expand Up @@ -82,6 +74,30 @@ func (u *UI) promptSingleLine(titleKey, promptKey, prefix string, active primary
}
}

func (u *UI) renderSingleLine(titleKey, promptKey string, input *textinput.Model, active primaryTabID, context, selected string) {
width, height := u.terminal.Size()
innerWidth, _ := frameInnerWidth(width)
u.styleTextInput(input)
input.SetWidth(max(innerWidth-visibleWidth(input.Prompt), 1))
extra := []string{u.t(promptKey)}
var literalRows []int
if active == primarySearch {
// Keep the query field in the same header area as the submitted query;
// the main pane remains reserved for search results.
extra = append(extra, u.style.InputField(input.View(), innerWidth))
literalRows = append(literalRows, len(extra)-1)
}
header := u.navigationHeader(u.t(titleKey), active, context, selected, "", extra, width, height, literalRows...)
screen, pane := u.mainPane(header, width, height, 1)
var lines []string
if active != primarySearch {
lines = []string{u.style.InputField(input.View(), pane.width)}
}
u.drawMainPane(screen, pane, lines)
u.placeControlsFooter(screen, u.t("ui.controls.single_line"), width)
u.renderer.Render(screen, width, height, "single-line-"+titleKey, -1, -1, false)
}

func (u *UI) compose(title string, context []string, category, variant string) string {
u.navigationLocked = true
defer func() { u.navigationLocked = false }()
Expand Down Expand Up @@ -144,39 +160,45 @@ func (u *UI) renderComposer(title string, area *textarea.Model, minLength int, c
lines = append(lines, headerLine("", category, inner))
}
header := u.navigationHeader(u.t("ui.composer.compose")+" "+title, primaryCompose, "compose", variant, "", lines, width, height)
screen := make([]string, height)
copy(screen, header)
start := len(header) + 1
area.SetWidth(max(width-1, 1))
area.SetHeight(max(height-start-2, 1))
screen, pane := u.mainPane(header, width, height, 1)
reservedRows := 0
if notice != "" {
reservedRows = 1
}
u.styleTextArea(area)
area.SetWidth(max(pane.width, 1))
area.SetHeight(max(pane.height-reservedRows, 1))
inputLines := strings.Split(strings.TrimSuffix(area.View(), "\n"), "\n")
for index, line := range inputLines {
if start+index < height {
screen[start+index] = " " + line
bodyLines := make([]string, pane.height)
for index := 0; index < max(pane.height-reservedRows, 0); index++ {
line := ""
if index < len(inputLines) {
line = inputLines[index]
}
bodyLines[index] = u.style.InputField(line, pane.width)
}
if notice != "" {
row := min(start+len(inputLines)+1, max(height-2, 0))
screen[row] = u.style.Text(notice, roleAccent)
if notice != "" && len(bodyLines) > 0 {
bodyLines[len(bodyLines)-1] = u.style.Text(notice, roleAccent)
}
u.drawMainPane(screen, pane, bodyLines)
u.placeControlsFooter(screen, status, width)
u.renderer.Render(screen, width, height, "composer-"+title, -1, -1, false)
}

func (u *UI) styleTextInput(input *textinput.Model) {
styles := input.Styles()
styles.Focused.Text = u.style.roles[roleListText]
styles.Focused.Prompt = u.style.roles[roleAccent]
styles.Focused.Placeholder = u.style.roles[roleListMeta]
styles.Focused.Suggestion = u.style.roles[roleListMeta]
styles.Focused.Text = u.style.roles[roleListText].Background(u.style.input.GetBackground())
styles.Focused.Prompt = u.style.roles[roleAccent].Background(u.style.input.GetBackground())
styles.Focused.Placeholder = u.style.roles[roleListMeta].Background(u.style.input.GetBackground())
styles.Focused.Suggestion = u.style.roles[roleListMeta].Background(u.style.input.GetBackground())
styles.Blurred = styles.Focused
styles.Cursor.Color = u.style.profile.Convert(lipgloss.Color(string(u.style.Theme.Accent)))
input.SetStyles(styles)
}

func (u *UI) styleTextArea(area *textarea.Model) {
styles := area.Styles()
styles.Focused.Base = u.style.roles[roleListText]
styles.Focused.Base = u.style.input
styles.Focused.Text = u.style.roles[roleListText]
styles.Focused.LineNumber = u.style.roles[roleListMeta]
styles.Focused.CursorLineNumber = u.style.roles[roleAccent]
Expand Down Expand Up @@ -233,22 +255,18 @@ func (u *UI) pickCategory() (*int, string) {
width, height := u.terminal.Size()
controls := u.t("ui.controls.category_picker")
header := u.navigationHeader(u.t("ui.composer.select_category"), primaryCompose, "compose", "category", "", nil, width, height)
screen := make([]string, height)
copy(screen, header)
rows := max(height-len(header)-1, 0)
start := max(selected-rows/2, 0)
for index := start; index < min(start+rows, len(options)); index++ {
screen, pane := u.mainPane(header, width, height, 1)
lines := make([]string, pane.height)
start := max(selected-pane.height/2, 0)
for index := start; index < min(start+pane.height, len(options)); index++ {
line := options[index].label
if index == selected {
line = u.style.Selected(line)
}
y := len(header) + 1 + index - start
if y < 0 || y >= height {
continue
}
screen[y] = line
u.addMouseRegion(0, y, width, 1, rowKey(index))
lines[index-start] = line
u.addMouseRegion(pane.x, pane.y+index-start, pane.width, 1, rowKey(index))
}
u.drawMainPane(screen, pane, lines)
u.placeControlsFooter(screen, controls, width)
u.renderer.Render(screen, width, height, "category-picker", -1, -1, false)
key, err := u.readKey(u.tick)
Expand Down
24 changes: 9 additions & 15 deletions internal/ui/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,17 @@ func (u *UI) showError(err error) bool {
message := u.errorMessage(err, time.Now())
width, height := u.terminal.Size()
u.resetMouseLayout()
inner := max(width-4, 1)
lines := wrapLines(strings.TrimSpace(message), inner, u.linksEnabled)
contextRows := 0
if u.activeContext != "" {
contextRows = 1
}
chromeRows := len(u.style.AppTitle(u.displayURL, u.options.Version, width, height)) + 1 + 1 + contextRows + 2
maxMessageLines := max(height-chromeRows-1, 1)
if len(lines) > maxMessageLines {
lines = append(lines[:maxMessageLines-1], truncate(u.t("ui.scroll.more_below"), inner))
}
panel := u.navigationHeader(
header := u.navigationHeader(
u.t("ui.errors.title"), u.activePrimary, u.activeContext, u.activeContextValue, u.activePeriod,
lines, width, height,
nil, width, height,
)
screen := make([]string, height)
copy(screen, panel[:min(len(panel), height)])
screen, pane := u.mainPane(header, width, height, 1)
lines := wrapLines(strings.TrimSpace(message), max(pane.width, 1), u.linksEnabled)
if len(lines) > pane.height && pane.height > 0 {
lines = append(lines[:pane.height-1], truncate(u.t("ui.scroll.more_below"), max(pane.width, 1)))
}
u.drawMainPane(screen, pane, lines)

u.placeControlsFooter(screen, u.t("ui.controls.continue"), width)
u.renderer.Render(screen, width, height, "error", -1, -1, true)
key, readErr := u.readKey(24 * time.Hour)
Expand Down
79 changes: 40 additions & 39 deletions internal/ui/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -532,31 +532,31 @@ renderLoop:
}
if backend == "chafa" && u.detectSixel() {
header := u.imageScreenHeader(width, height)
available := max(height-len(header)-1, 1)
if payload, nativeErr := u.renderNativeImage(imageURL, width, available, u.nativePixelViewport(height, len(header)+1)); nativeErr == nil && len(payload) > 0 {
screen, pane := u.mainPane(header, width, height, 1)
if payload, nativeErr := u.renderNativeImage(imageURL, max(pane.width, 1), max(pane.height, 1), u.nativePixelViewport(width, height, pane)); nativeErr == nil && len(payload) > 0 && pane.width > 0 && pane.height > 0 {
// Keep Bubble Tea's current model intact while the raw Sixel payload
// owns the image rows beneath the persistent managed navigation.
screen := make([]string, height)
copy(screen, header)
u.drawMainPane(screen, pane, nil)
u.placeControlsFooter(screen, u.t("ui.controls.fullscreen_image"), width)
u.renderer.Render(screen, width, height, "fullscreen-image-sixel", -1, -1, true)
u.terminal.Raw(xansi.HideCursor + xansi.CursorPosition(len(header)+1, 1) + string(payload) + xansi.HideCursor)
u.terminal.Raw(xansi.HideCursor + xansi.CursorPosition(pane.y+1, pane.x+1) + string(payload) + xansi.HideCursor)
for {
key, err := u.readKey(u.tick)
if err != nil || u.leaveImageForKey(key) {
u.renderer.Reset()
return
}
if key == keyThemeChanged || key == keyHoverChanged {
newWidth, newHeight := u.terminal.Size()
if key == keyThemeChanged || key == keyHoverChanged || newWidth != width || newHeight != height {
u.renderer.Reset()
continue renderLoop
}
}
}
}
header := u.imageScreenHeader(width, height)
available := max(height-len(header)-1, 1)
rendered, err := u.renderImage(imageURL, backend, width, available, false, false)
screen, pane := u.mainPane(header, width, height, 1)
rendered, err := u.renderImage(imageURL, backend, max(pane.width, 1), max(pane.height, 1), false, false)
if err != nil {
u.showError(err)
return
Expand All @@ -565,15 +565,12 @@ renderLoop:
u.showError(fmt.Errorf("image renderer returned no output"))
return
}
screen := make([]string, height)
copy(screen, header)
for index := 0; index < len(rendered) && index < available; index++ {
padding := max((width-visibleWidth(rendered[index]))/2, 0)
y := len(header) + index
if y < height {
screen[y] = strings.Repeat(" ", padding) + rendered[index]
}
lines := make([]string, pane.height)
for index := 0; index < len(rendered) && index < len(lines); index++ {
padding := max((pane.width-visibleWidth(rendered[index]))/2, 0)
lines[index] = strings.Repeat(" ", padding) + rendered[index]
}
u.drawMainPane(screen, pane, lines)
u.placeControlsFooter(screen, u.t("ui.controls.fullscreen_image"), width)
// Let Bubble Tea diff managed image screens. An explicit clear erases
// terminal graphics and, with all-motion mouse events, can race the
Expand All @@ -584,7 +581,8 @@ renderLoop:
if err != nil || u.leaveImageForKey(key) {
return
}
if key == keyThemeChanged || key == keyHoverChanged {
newWidth, newHeight := u.terminal.Size()
if key == keyThemeChanged || key == keyHoverChanged || newWidth != width || newHeight != height {
continue renderLoop
}
}
Expand All @@ -595,23 +593,20 @@ func (u *UI) fullscreenKittyImage(imageURL string) bool {
for {
width, height := u.terminal.Size()
header := u.imageScreenHeader(width, height)
available := max(height-len(header)-1, 1)
rendered, err := u.renderKittyImage(imageURL, width, available)
screen, pane := u.mainPane(header, width, height, 1)
rendered, err := u.renderKittyImage(imageURL, max(pane.width, 1), max(pane.height, 1))
if err != nil || len(rendered) == 0 {
if err != nil {
u.reportKittyFailure(imageURL, "fullscreen", err)
}
return false
}
screen := make([]string, height)
copy(screen, header)
for index := 0; index < len(rendered) && index < available; index++ {
padding := max((width-visibleWidth(rendered[index]))/2, 0)
y := len(header) + index
if y < height {
screen[y] = strings.Repeat(" ", padding) + rendered[index]
}
lines := make([]string, pane.height)
for index := 0; index < len(rendered) && index < len(lines); index++ {
padding := max((pane.width-visibleWidth(rendered[index]))/2, 0)
lines[index] = strings.Repeat(" ", padding) + rendered[index]
}
u.drawMainPane(screen, pane, lines)
u.placeControlsFooter(screen, u.t("ui.controls.fullscreen_image"), width)
// Kitty's Unicode placeholders are ordinary managed cells. Keeping this
// as a normal diff render preserves their visible image across hover and
Expand Down Expand Up @@ -701,21 +696,21 @@ func parseGraphicsResponse(response, expectedItem string) (int, int, bool) {
return width, height, width > 0 && height > 0
}

func (u *UI) nativePixelViewport(screenHeight, reservedRows int) *[2]int {
graphics, _ := u.terminal.Query("\x1b[?2;1;0S", func(msg any) bool {
_, ok := msg.(ultraviolet.UnknownCsiEvent)
func (u *UI) nativePixelViewport(screenWidth, screenHeight int, pane paneLayout) *[2]int {
response, _ := u.terminal.Query(xansi.WindowOp(14), func(msg any) bool {
_, ok := msg.(ultraviolet.PixelSizeEvent)
return ok
}, 150*time.Millisecond)
graphicsResponse, _ := graphics.(ultraviolet.UnknownCsiEvent)
width, height, ok := parseGraphicsResponse(string(graphicsResponse), "2")
pixels, ok := response.(ultraviolet.PixelSizeEvent)
width, height := pixels.Width, pixels.Height
ok = ok && width > 0 && height > 0
if !ok {
response, _ := u.terminal.Query(xansi.WindowOp(14), func(msg any) bool {
_, ok := msg.(ultraviolet.PixelSizeEvent)
graphics, _ := u.terminal.Query("\x1b[?2;1;0S", func(msg any) bool {
_, ok := msg.(ultraviolet.UnknownCsiEvent)
return ok
}, 150*time.Millisecond)
if pixels, responseOK := response.(ultraviolet.PixelSizeEvent); responseOK {
width, height, ok = pixels.Width, pixels.Height, pixels.Width > 0 && pixels.Height > 0
}
graphicsResponse, _ := graphics.(ultraviolet.UnknownCsiEvent)
width, height, ok = parseGraphicsResponse(string(graphicsResponse), "2")
}
if !ok {
return nil
Expand All @@ -725,11 +720,17 @@ func (u *UI) nativePixelViewport(screenHeight, reservedRows int) *[2]int {
return ok
}, 150*time.Millisecond)
cell, cellOK := cellResponse.(ultraviolet.CellSizeEvent)
cellHeight := cell.Height
cellWidth, cellHeight := cell.Width, cell.Height
if !cellOK || cellWidth <= 0 {
cellWidth = max(int(float64(width)/float64(max(screenWidth, 1))+0.5), 1)
}
if !cellOK || cellHeight <= 0 {
cellHeight = max(int(float64(height)/float64(max(screenHeight, 1))+0.5), 1)
}
viewport := [2]int{width, max(height-cellHeight*max(reservedRows, 1), 1)}
viewport := [2]int{
max(min(pane.width*cellWidth, width), 1),
max(min(pane.height*cellHeight, height), 1),
}
return &viewport
}

Expand Down
Loading
Loading