Skip to content

Commit

Permalink
Configurable side scroll amount
Browse files Browse the repository at this point in the history
Using the new --shift command line flag.

Fixes #130.
  • Loading branch information
walles committed Mar 19, 2023
1 parent a5bd703 commit eafb70d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
33 changes: 18 additions & 15 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions moar.1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions moar.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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

Expand All @@ -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,
Expand All @@ -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:]
Expand Down Expand Up @@ -363,6 +381,7 @@ func main() {
pager.UnprintableStyle = *unprintableStyle
pager.ScrollLeftHint = *scrollLeftHint
pager.ScrollRightHint = *scrollRightHint
pager.SideScrollAmount = int(*shift)
startPaging(pager)
}

Expand Down

0 comments on commit eafb70d

Please sign in to comment.