From eafb70d79042958235221a1428545253adc2220f Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Sun, 19 Mar 2023 11:56:54 +0100 Subject: [PATCH] Configurable side scroll amount Using the new --shift command line flag. Fixes #130. --- m/pager.go | 33 ++++++++++++++++++--------------- moar.1 | 3 +++ moar.go | 19 +++++++++++++++++++ 3 files changed, 40 insertions(+), 15 deletions(-) diff --git a/m/pager.go b/m/pager.go index 131d5334..7615b74d 100644 --- a/m/pager.go +++ b/m/pager.go @@ -74,6 +74,8 @@ type Pager struct { ScrollLeftHint twin.Cell ScrollRightHint twin.Cell + SideScrollAmount int // Should be positive + // If true, pager will clear the screen on return. If false, pager will // clear the last line, and show the cursor. DeInit bool @@ -142,7 +144,7 @@ func (pm _PagerMode) isViewing() bool { return pm == _Viewing || pm == _NotFound } -// NewPager creates a new Pager +// NewPager creates a new Pager with default settings func NewPager(r *Reader) *Pager { var name string if r == nil || r.name == nil || len(*r.name) == 0 { @@ -151,14 +153,15 @@ func NewPager(r *Reader) *Pager { name = "Pager " + *r.name } return &Pager{ - reader: r, - quit: false, - ShowLineNumbers: true, - ShowStatusBar: true, - DeInit: true, - ScrollLeftHint: twin.NewCell('<', twin.StyleDefault.WithAttr(twin.AttrReverse)), - ScrollRightHint: twin.NewCell('>', twin.StyleDefault.WithAttr(twin.AttrReverse)), - scrollPosition: newScrollPosition(name), + reader: r, + quit: false, + ShowLineNumbers: true, + ShowStatusBar: true, + DeInit: true, + SideScrollAmount: 16, + ScrollLeftHint: twin.NewCell('<', twin.StyleDefault.WithAttr(twin.AttrReverse)), + ScrollRightHint: twin.NewCell('>', twin.StyleDefault.WithAttr(twin.AttrReverse)), + scrollPosition: newScrollPosition(name), } } @@ -262,10 +265,10 @@ func (p *Pager) onKey(keyCode twin.KeyCode) { p.handleScrolledDown() case twin.KeyRight: - p.moveRight(16) + p.moveRight(p.SideScrollAmount) case twin.KeyLeft: - p.moveRight(-16) + p.moveRight(-p.SideScrollAmount) case twin.KeyAltRight: p.moveRight(1) @@ -346,11 +349,11 @@ func (p *Pager) onRune(char rune) { case 'l': // vim right - p.moveRight(16) + p.moveRight(p.SideScrollAmount) case 'h': // vim left - p.moveRight(-16) + p.moveRight(-p.SideScrollAmount) case '<': p.scrollPosition = newScrollPosition("Pager scroll position") @@ -492,10 +495,10 @@ func (p *Pager) StartPaging(screen twin.Screen) { p.scrollPosition = p.scrollPosition.NextLine(1) case twin.MouseWheelLeft: - p.moveRight(-16) + p.moveRight(-p.SideScrollAmount) case twin.MouseWheelRight: - p.moveRight(16) + p.moveRight(p.SideScrollAmount) } case twin.EventResize: diff --git a/moar.1 b/moar.1 index 59b6aada..5240d126 100644 --- a/moar.1 +++ b/moar.1 @@ -76,6 +76,9 @@ in caps will be interpreted as one escape character. Example value for faint (using ANSI SGR code 2) tilde characters: .B ESC[2m~ .TP +\fB\-\-shift\fR=int +Arrow keys side scroll amount. Or try ALT+arrow to scroll one column at a time. +.TP \fB\-\-statusbar\fR={\fBinverse\fR | \fBplain\fR | \fBbold\fR} Status bar style .TP diff --git a/moar.go b/moar.go index fdf9a4e7..c8c4093a 100644 --- a/moar.go +++ b/moar.go @@ -8,6 +8,7 @@ import ( "os/exec" "path/filepath" "runtime" + "strconv" "strings" "time" @@ -205,6 +206,21 @@ func parseScrollHint(scrollHint string) (twin.Cell, error) { return twin.Cell{}, fmt.Errorf("Expected exactly one (optionally highlighted) character. For example: 'ESC[2m…'") } +func parseShiftAmount(shiftAmount string) (uint, error) { + value, err := strconv.ParseUint(shiftAmount, 10, 32) + if err != nil { + return 0, err + } + + if value < 1 { + return 0, fmt.Errorf("Shift amount must be at least 1, was %d", value) + } + + // Let's add an upper bound as well if / when requested + + return uint(value), nil +} + func main() { // FIXME: If we get a CTRL-C, get terminal back into a useful state before terminating @@ -226,6 +242,7 @@ func main() { printVersion := flagSet.Bool("version", false, "Prints the moar version number") debug := flagSet.Bool("debug", false, "Print debug logs after exiting") trace := flagSet.Bool("trace", false, "Print trace logs after exiting") + wrap := flagSet.Bool("wrap", false, "Wrap long lines") follow := flagSet.Bool("follow", false, "Follow piped input just like \"tail -f\"") style := flagSetFunc(flagSet, @@ -246,6 +263,7 @@ func main() { scrollRightHint := flagSetFunc(flagSet, "scroll-right-hint", twin.NewCell('>', twin.StyleDefault.WithAttr(twin.AttrReverse)), "Shown when view can scroll right. One character with optional ANSI highlighting.", parseScrollHint) + shift := flagSetFunc(flagSet, "shift", 16, "Horizontal scroll amount >=1, defaults to 16", parseShiftAmount) // Combine flags from environment and from command line flags := os.Args[1:] @@ -363,6 +381,7 @@ func main() { pager.UnprintableStyle = *unprintableStyle pager.ScrollLeftHint = *scrollLeftHint pager.ScrollRightHint = *scrollRightHint + pager.SideScrollAmount = int(*shift) startPaging(pager) }