Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add short forms for some flags #267

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 17 additions & 4 deletions moar.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,17 +334,23 @@ func pagerFromArgs(
flagSet.SetOutput(io.Discard) // We want to do our own printing

printVersion := flagSet.Bool("version", false, "Prints the moar version number")
flagSet.BoolVar(printVersion, "V", 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")
flagSet.BoolVar(wrap, "w", false, "Wrap long lines")
follow := flagSet.Bool("follow", false, "Follow piped input just like \"tail -f\"")
flagSet.BoolVar(follow, "f", false, "Follow piped input just like \"tail -f\"")
styleOption := flagSetFunc(flagSet,
"style", nil,
"Highlighting `style` from https://xyproto.github.io/splash/docs/longer/all.html", parseStyleOption)
lexer := flagSetFunc(flagSet,
"lang", nil,
"File contents, used for highlighting. Mime type or file extension (\"html\"). Default is to guess by filename.", parseLexerOption)
flagSetFuncVar(flagSet,
lexer, "l", nil,
"File contents, used for highlighting. Mime type or file extension (\"html\"). Default is to guess by filename.", parseLexerOption)
terminalFg := flagSet.Bool("terminal-fg", false, "Use terminal foreground color rather than style foreground for plain text")

defaultFormatter, err := parseColorsOption("auto")
Expand All @@ -359,6 +365,7 @@ func pagerFromArgs(
reFormat := flagSet.Bool("reformat", false, "Reformat some input files (JSON)")
flagSet.Bool("no-reformat", true, "No effect, kept for compatibility. See --reformat")
quitIfOneScreen := flagSet.Bool("quit-if-one-screen", false, "Don't page if contents fits on one screen")
flagSet.BoolVar(quitIfOneScreen, "F", false, "Don't page if contents fits on one screen")
noClearOnExit := flagSet.Bool("no-clear-on-exit", false, "Retain screen contents when exiting moar")
statusBarStyle := flagSetFunc(flagSet, "statusbar", m.STATUSBAR_STYLE_INVERSE,
"Status bar `style`: inverse, plain or bold", parseStatusBarStyle)
Expand Down Expand Up @@ -642,18 +649,24 @@ func main() {
// The return value is the address of a variable that stores the parsed value of
// the flag.
func flagSetFunc[T any](flagSet *flag.FlagSet, name string, defaultValue T, usage string, parser func(valueString string) (T, error)) *T {
parsed := defaultValue
var parsed T
flagSetFuncVar(flagSet, &parsed, name, defaultValue, usage, parser)
return &parsed
}

// Define a generic flag with specified name, default value, and usage string.
// The parsed value of the flag will be written to the passed pointer.
func flagSetFuncVar[T any](flagSet *flag.FlagSet, p *T, name string, defaultValue T, usage string, parser func(valueString string) (T, error)) {
*p = defaultValue

flagSet.Func(name, usage, func(valueString string) error {
parseResult, err := parser(valueString)
if err != nil {
return err
}
parsed = parseResult
*p = parseResult
return nil
})

return &parsed
}

func startPaging(pager *m.Pager, screen twin.Screen, chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter) {
Expand Down
31 changes: 27 additions & 4 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,32 @@ import (
"github.com/walles/moar/twin"
)

const flags_usage = ` --colors value Highlighting palette size: 8, 16, 256, 16M, auto
--debug Print debug logs after exiting
-f, --follow Follow piped input just like "tail -f"
-l, --lang value File contents, used for highlighting. Mime type or extension ("html"). Default is to guess by filename.
--mousemode mode Mouse mode: auto, select or scroll: https://github.com/walles/moar/blob/master/MOUSE.md
--no-clear-on-exit Retain screen contents when exiting moar
--no-linenumbers Hide line numbers on startup, press left arrow key to show
--no-reformat No effect, kept for compatibility. See --reformat (default true)
--no-statusbar Hide the status bar, toggle with '='
-F, --quit-if-one-screen Don't page if contents fits on one screen
--reformat Reformat some input files (JSON)
--render-unprintable value How unprintable characters are rendered: highlight or whitespace
--scroll-left-hint value Shown when view can scroll left. One character with optional ANSI highlighting.
--scroll-right-hint value Shown when view can scroll right. One character with optional ANSI highlighting.
--shift amount Horizontal scroll amount >= 1, defaults to 16
--statusbar style Status bar style: inverse, plain or bold
--style style Highlighting style from https://xyproto.github.io/splash/docs/longer/all.html
--terminal-fg Use terminal foreground color rather than style foreground for plain text
--trace Print trace logs after exiting
-w, --wrap Wrap long lines

+1234 Immediately scroll to line 1234

-h, --help Show this help text
-V, --version Print the moar version number`

func renderLessTermcapEnvVar(envVarName string, description string, colors twin.ColorCount) string {
value := os.Getenv(envVarName)
if len(value) == 0 {
Expand Down Expand Up @@ -187,10 +213,7 @@ func printUsage(flagSet *flag.FlagSet, colors twin.ColorCount) {
fmt.Println()
fmt.Println(heading("Options", colors))

flagSet.PrintDefaults()

fmt.Println(" +1234")
fmt.Println(" \tImmediately scroll to line 1234")
fmt.Println(flags_usage)
}

// If $PAGER isn't pointing to us, print a help text on how to set it.
Expand Down