summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNomeji <nomeji@saucisseroyale.cc>2022-06-23 12:05:32 +0200
committerdelthas <delthas@dille.cc>2022-07-22 17:50:50 +0200
commitb75cb83397e32fc3bc8ead40f34efadafda6fb70 (patch)
treedf3a9aedc19dab333fd054d40576ee47ca4c250a
parentFix dropping words in /msg, /query, /part (diff)
Scroll to the channel when changing the horizontal channel
Previously, when running CTRL+N or CTRL+P to go to the next or previous channel, with the horizontal channel mode, the selected channel could appear offscreen. This patch ensures that we scroll just enough to make the newly selected chan visible. Thanks to Nomeji for his hard work on this. Adapted slightly to refactor existing code to use the newly created width computation routine.
-rw-r--r--app.go2
-rw-r--r--ui/buffers.go48
-rw-r--r--ui/ui.go16
3 files changed, 50 insertions, 16 deletions
diff --git a/app.go b/app.go
index 9f86b99..4d5e5b3 100644
--- a/app.go
+++ b/app.go
@@ -514,8 +514,10 @@ func (app *App) handleKeyEvent(ev *tcell.EventKey) {
app.win.ScrollDown()
case tcell.KeyCtrlN:
app.win.NextBuffer()
+ app.win.HorizontalBufferScrollTo()
case tcell.KeyCtrlP:
app.win.PreviousBuffer()
+ app.win.HorizontalBufferScrollTo()
case tcell.KeyRight:
if ev.Modifiers() == tcell.ModAlt {
app.win.NextBuffer()
diff --git a/ui/buffers.go b/ui/buffers.go
index 4bf4d20..f1f71b3 100644
--- a/ui/buffers.go
+++ b/ui/buffers.go
@@ -607,14 +607,7 @@ func (bs *BufferList) HorizontalBufferOffset(x int, offset int) int {
return -1
}
}
- if b.title == "" {
- x -= stringWidth(b.netName)
- } else {
- x -= stringWidth(b.title)
- }
- if 0 < b.highlights {
- x -= 2 + len(fmt.Sprintf("%d", b.highlights))
- }
+ x -= bufferWidth(&b)
if x < 0 {
return offset + i
}
@@ -622,19 +615,42 @@ func (bs *BufferList) HorizontalBufferOffset(x int, offset int) int {
return -1
}
+func (bs *BufferList) GetLeftMost(screenWidth int) int {
+ width := 0
+ var leftMost int
+
+ for leftMost = bs.current; leftMost >= 0; leftMost-- {
+ if leftMost < bs.current {
+ width++
+ }
+ width += bufferWidth(&bs.list[leftMost])
+ if width > screenWidth {
+ return leftMost + 1 // Went offscreen, need to go one step back
+ }
+ }
+
+ return 0
+}
+
+func bufferWidth(b *buffer) int {
+ width := 0
+ if b.title == "" {
+ width += stringWidth(b.netName)
+ } else {
+ width += stringWidth(b.title)
+ }
+ if 0 < b.highlights {
+ width += 2 + len(fmt.Sprintf("%d", b.highlights))
+ }
+ return width
+}
+
func (bs *BufferList) DrawHorizontalBufferList(screen tcell.Screen, x0, y0, width int, offset *int) {
x := width
for i := len(bs.list) - 1; i >= 0; i-- {
b := &bs.list[i]
x--
- if b.title == "" {
- x -= stringWidth(b.netName)
- } else {
- x -= stringWidth(b.title)
- }
- if 0 < b.highlights {
- x -= 2 + len(fmt.Sprintf("%d", b.highlights))
- }
+ x -= bufferWidth(b)
if x <= 10 {
break
}
diff --git a/ui/ui.go b/ui/ui.go
index 77757cb..9e4af88 100644
--- a/ui/ui.go
+++ b/ui/ui.go
@@ -452,6 +452,22 @@ func (ui *UI) Draw(members []irc.Member) {
ui.screen.Show()
}
+func (ui *UI) HorizontalBufferScrollTo() {
+ w, _ := ui.screen.Size()
+ screenWidth := w - ui.memberWidth
+ if ui.bs.current < ui.channelOffset {
+ ui.channelOffset = ui.bs.current
+ return
+ }
+
+ leftMost := ui.bs.GetLeftMost(screenWidth)
+
+ if ui.channelOffset >= leftMost {
+ return
+ }
+ ui.channelOffset = leftMost
+}
+
func (ui *UI) drawStatusBar(x0, y, width int) {
clearArea(ui.screen, x0, y, width, 1)