Skip to content

Commit

Permalink
Show Fish / Powershell specific help
Browse files Browse the repository at this point in the history
On `moar --help`, if `moar` is not the default pager, try identifying
the user's shell and print instructions for setting `PAGER` in that
shell.

Identifies fish or Powershell, then falls back on bash / zsh
instructions (since I failed to come up with a sensible way of
identifying those).

Before this change, we always printed bash / zsh instructions.

Fixes: #251
  • Loading branch information
walles committed Oct 27, 2024
1 parent fdec6ce commit fc59ade
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
3 changes: 2 additions & 1 deletion scripts/test-path-help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ ln -s "$MOAR" "$WORKDIR/moar"
echo "moar" >"$WORKDIR/expected"

# Extract suggested PAGER value from moar --help
PATH="$WORKDIR" PAGER="" moar --help | grep "export PAGER" | sed -E 's/.*=//' >"$WORKDIR/actual"
unset PAGER
PATH="$WORKDIR" PAGER="" moar --help | grep "PAGER" | grep -v "is empty" | sed -E 's/.*PAGER[= ]//' >"$WORKDIR/actual"

# Ensure it matches the symlink we have in $PATH
cd "$WORKDIR"
Expand Down
27 changes: 22 additions & 5 deletions usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ func printUsage(flagSet *flag.FlagSet, colors twin.ColorCount) {
fmt.Print(envSection)
}

// We're not the default pager
printSetDefaultPagerHelp(colors)

fmt.Println()
Expand All @@ -194,6 +193,7 @@ func printUsage(flagSet *flag.FlagSet, colors twin.ColorCount) {
fmt.Println(" \tImmediately scroll to line 1234")
}

// If $PAGER isn't pointing to us, print a help text on how to set it.
func printSetDefaultPagerHelp(colors twin.ColorCount) {
absMoarPath, err := absLookPath(os.Args[0])
if err != nil {
Expand All @@ -207,15 +207,32 @@ func printSetDefaultPagerHelp(colors twin.ColorCount) {
}

if absPagerValue == absMoarPath {
// We're already the default pager
return
}

fmt.Println()
fmt.Println(heading("Making moar Your Default Pager", colors))
fmt.Println(" Put the following line in your ~/.bashrc, ~/.bash_profile or ~/.zshrc")
fmt.Println(" and moar will be used as the default pager in all new terminal windows:")
fmt.Println()
fmt.Printf(" export PAGER=%s\n", getMoarPath())

shellIsFish := strings.HasSuffix(os.Getenv("SHELL"), "fish")
shellIsPowershell := len(os.Getenv("PSModulePath")) > 0

if shellIsFish {
fmt.Println(" Write this command at your prompt:")
fmt.Println()
fmt.Printf(" set -Ux PAGER %s\n", getMoarPath())
} else if shellIsPowershell {
fmt.Println(" Put the following line in your $PROFILE file (\"echo $PROFILE\" to find it)")
fmt.Println(" and moar will be used as the default pager in all new terminal windows:")
fmt.Println()
fmt.Printf(" $env:PAGER = \"%s\"\n", getMoarPath())
} else {
// I don't know how to identify bash / zsh, put generic instructions here
fmt.Println(" Put the following line in your ~/.bashrc, ~/.bash_profile or ~/.zshrc")
fmt.Println(" and moar will be used as the default pager in all new terminal windows:")
fmt.Println()
fmt.Printf(" export PAGER=%s\n", getMoarPath())
}
}

// "moar" if we're in the $PATH, otherwise an absolute path
Expand Down

0 comments on commit fc59ade

Please sign in to comment.