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
22 changes: 4 additions & 18 deletions internal/ui/controls.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,7 @@ func layoutControls(value string, width int) []laidOutControl {
return nil
}
segments := strings.Split(value, " | ")
type tier struct {
labels []string
separator string
}
build := func(compact bool, framed bool, separator string) tier {
build := func(compact bool, framed bool, separator string) labelTier {
labels := make([]string, len(segments))
for index, segment := range segments {
label := strings.ToUpper(strings.TrimSpace(segment))
Expand All @@ -60,25 +56,15 @@ func layoutControls(value string, width int) []laidOutControl {
}
labels[index] = label
}
return tier{labels: labels, separator: separator}
return labelTier{labels: labels, separator: separator}
}
tiers := []tier{
tiers := []labelTier{
build(false, true, " "),
build(true, true, " "),
build(true, false, " "),
build(true, false, ""),
}
chosen := tiers[len(tiers)-1]
for _, candidate := range tiers {
total := max(len(candidate.labels)-1, 0) * displayWidth(candidate.separator)
for _, label := range candidate.labels {
total += displayWidth(label)
}
if total <= width {
chosen = candidate
break
}
}
chosen := chooseLabelTier(tiers, width)

x := 0
controls := make([]laidOutControl, 0, len(segments))
Expand Down
37 changes: 15 additions & 22 deletions internal/ui/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,17 +565,7 @@ renderLoop:
u.showError(fmt.Errorf("image renderer returned no output"))
return
}
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
// replacement frame on every hover change.
u.renderer.Render(screen, width, height, "fullscreen-image", -1, -1, false)
u.drawFullscreenImage(screen, pane, rendered, width, height, "fullscreen-image")
for {
key, err := u.readKey(u.tick)
if err != nil || u.leaveImageForKey(key) {
Expand All @@ -601,17 +591,7 @@ func (u *UI) fullscreenKittyImage(imageURL string) bool {
}
return false
}
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
// resize redraws instead of clearing it immediately before repainting.
u.renderer.Render(screen, width, height, "fullscreen-image-kitty", -1, -1, false)
u.drawFullscreenImage(screen, pane, rendered, width, height, "fullscreen-image-kitty")
for {
key, readErr := u.readKey(u.tick)
if readErr != nil || u.leaveImageForKey(key) {
Expand All @@ -628,6 +608,19 @@ func (u *UI) fullscreenKittyImage(imageURL string) bool {
}
}

func (u *UI) drawFullscreenImage(screen []string, pane paneLayout, rendered []string, width, height int, viewKey string) {
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)
// Both backends draw managed cells, including Kitty's Unicode placeholders.
// A full clear can erase terminal graphics between redraws.
u.renderer.Render(screen, width, height, viewKey, -1, -1, false)
}

func (u *UI) imageScreenHeader(width, height int) []string {
return u.navigationHeader(u.t("ui.status.image"), u.activePrimary, "", "", "", nil, width, height)
}
Expand Down
26 changes: 4 additions & 22 deletions internal/ui/search_notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,8 @@ func (u *UI) searchLoop(query string) discourse.JSON {
continue
}
switch key {
case "up", "wheelup":
step := 1
if key == "wheelup" {
step = 3
}
selected = max(selected-step, 0)
case "down", "wheeldown":
step := 1
if key == "wheeldown" {
step = 3
}
selected = min(selected+step, max(len(results)-1, 0))
case "up", "wheelup", "down", "wheeldown":
selected = moveListSelection(selected, len(results), key)
case "enter":
if selected < len(results) {
return results[selected]
Expand Down Expand Up @@ -192,17 +182,9 @@ func (u *UI) notificationsLoop() bool {
}
switch key {
case "up", "wheelup":
step := 1
if key == "wheelup" {
step = 3
}
selected = max(selected-step, 0)
selected = moveListSelection(selected, len(filtered), key)
case "down", "wheeldown":
step := 1
if key == "wheeldown" {
step = 3
}
selected = min(selected+step, max(len(filtered)-1, 0))
selected = moveListSelection(selected, len(filtered), key)
if nextURL != "" && selected >= len(filtered)-3 && !loading {
loading = true
u.renderNotifications(filtered, selected, filter, loading)
Expand Down
22 changes: 4 additions & 18 deletions internal/ui/tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,40 +87,26 @@ func layoutTabRail(specs []tabSpec, width int) tabRail {
if width <= 0 || len(specs) == 0 {
return tabRail{}
}
type tier struct {
labels []string
separator string
}
build := func(format func(tabSpec) string, separator string) tier {
build := func(format func(tabSpec) string, separator string) labelTier {
labels := make([]string, len(specs))
for index, spec := range specs {
labels[index] = format(spec)
}
return tier{labels: labels, separator: separator}
return labelTier{labels: labels, separator: separator}
}
badge := func(spec tabSpec) string {
if spec.badge <= 0 {
return ""
}
return " " + strconv.Itoa(spec.badge)
}
tiers := []tier{
tiers := []labelTier{
build(func(spec tabSpec) string { return "╭ " + strings.ToUpper(spec.label) + badge(spec) + " ╮" }, " "),
build(func(spec tabSpec) string { return "╭" + strings.ToUpper(spec.short) + badge(spec) + "╮" }, " "),
build(func(spec tabSpec) string { return strings.ToUpper(spec.micro) }, " "),
build(func(spec tabSpec) string { return strings.ToUpper(spec.micro) }, ""),
}
chosen := tiers[len(tiers)-1]
for _, candidate := range tiers {
total := max(len(candidate.labels)-1, 0) * displayWidth(candidate.separator)
for _, label := range candidate.labels {
total += displayWidth(label)
}
if total <= width {
chosen = candidate
break
}
}
chosen := chooseLabelTier(tiers, width)

rail := tabRail{separator: chosen.separator}
x := 0
Expand Down
19 changes: 19 additions & 0 deletions internal/ui/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ func displayWidth(value string) int {
return ansi.StringWidth(value)
}

type labelTier struct {
labels []string
separator string
}

// chooseLabelTier returns the first tier that fits, or the last tier as a fallback.
func chooseLabelTier(tiers []labelTier, width int) labelTier {
for _, candidate := range tiers {
total := max(len(candidate.labels)-1, 0) * displayWidth(candidate.separator)
for _, label := range candidate.labels {
total += displayWidth(label)
}
if total <= width {
return candidate
}
}
return tiers[len(tiers)-1]
}

func visibleWidth(value string) int { return displayWidth(stripANSI(value)) }

func truncateVisible(value string, width int) string {
Expand Down
56 changes: 27 additions & 29 deletions internal/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,17 +331,9 @@ func (u *UI) topicListLoop(data discourse.JSON, filter, period string) loopResul
case keyTick:
continue
case "up", "wheelup":
step := 1
if key == "wheelup" {
step = 3
}
selected = max(selected-step, 0)
selected = moveListSelection(selected, len(topics), key)
case "down", "wheeldown":
step := 1
if key == "wheeldown" {
step = 3
}
selected = min(selected+step, max(len(topics)-1, 0))
selected = moveListSelection(selected, len(topics), key)
if nextURL != "" && selected >= len(topics)-3 && !loading {
loading = true
u.renderTopicList(topics, selected, filter, period, loading)
Expand Down Expand Up @@ -388,6 +380,21 @@ func (u *UI) topicListLoop(data discourse.JSON, filter, period string) loopResul
}
}

func moveListSelection(selected, count int, key string) int {
switch key {
case "up":
return max(selected-1, 0)
case "wheelup":
return max(selected-3, 0)
case "down":
return min(selected+1, max(count-1, 0))
case "wheeldown":
return min(selected+3, max(count-1, 0))
default:
return selected
}
}

func (u *UI) renderTopicList(topics []discourse.JSON, selected int, filter, period string, loading bool) {
width, height := u.terminal.Size()
controls := u.t("ui.controls.topic_list")
Expand Down Expand Up @@ -550,38 +557,29 @@ func tableRow(cells []cell) string {
func (u *UI) pmUsers(topic discourse.JSON) string {
var names []string
login := strings.ToLower(strings.TrimSpace(u.options.Username))
addName := func(user discourse.JSON) {
name := strings.ToLower(strings.TrimSpace(discourse.String(user["username"])))
if name != "" && name != login {
names = appendUnique(names, name)
}
}
for _, key := range []string{"participants", "allowed_users"} {
for _, raw := range discourse.Slice(topic[key]) {
name := strings.ToLower(strings.TrimSpace(discourse.String(discourse.Map(raw)["username"])))
if name != "" && name != login {
names = appendUnique(names, name)
}
addName(discourse.Map(raw))
}
if len(names) > 0 {
break
}
}
ids := discourse.Slice(topic["participant_ids"])
if len(names) == 0 {
for _, raw := range ids {
id := discourse.Int(raw)
if user := u.users[id]; user != nil {
name := strings.ToLower(strings.TrimSpace(discourse.String(user["username"])))
if name != "" && name != login {
names = appendUnique(names, name)
}
}
for _, raw := range discourse.Slice(topic["participant_ids"]) {
addName(u.users[discourse.Int(raw)])
}
}
if len(names) == 0 {
for _, raw := range discourse.Slice(topic["posters"]) {
id := discourse.Int(discourse.Map(raw)["user_id"])
if user := u.users[id]; user != nil {
name := strings.ToLower(strings.TrimSpace(discourse.String(user["username"])))
if name != "" && name != login {
names = appendUnique(names, name)
}
}
addName(u.users[id])
}
}
if len(names) == 0 {
Expand Down
28 changes: 28 additions & 0 deletions internal/ui/ui_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,34 @@ func TestCharmTerminalModelOwnsResizeInputCursorAndQueries(t *testing.T) {
}
}

func TestPMUsersPreservesFallbackOrder(t *testing.T) {
u := &UI{
options: Options{Username: " Me "},
users: map[int]discourse.JSON{
1: {"username": "ME"},
2: {"username": " IDUser "},
3: {"username": " Poster "},
},
}
topic := discourse.JSON{
"participants": []any{discourse.JSON{"username": " First "}, discourse.JSON{"username": "FIRST"}},
"allowed_users": []any{discourse.JSON{"username": "Allowed"}},
"participant_ids": []any{1, 999, 2, 2},
"posters": []any{discourse.JSON{"user_id": 3}},
}
for _, source := range []struct{ key, want string }{
{"participants", "first"},
{"allowed_users", "allowed"},
{"participant_ids", "iduser"},
{"posters", "poster"},
} {
if got := u.pmUsers(topic); got != source.want {
t.Fatalf("%s: got %q, want %q", source.key, got, source.want)
}
delete(topic, source.key)
}
}

func TestTabRailsStayCompleteAndResponsive(t *testing.T) {
specs := []tabSpec{
{id: "latest", label: "Latest", short: "LAT", micro: "L", selected: true, enabled: true},
Expand Down
Loading