Making cli pager optional + better visual indicators for scrolling (#4819)
This commit is contained in:
@@ -38,6 +38,12 @@ var globalFlags = []cli.Flag{
|
||||
Usage: "disable progress bar display",
|
||||
EnvVar: envPrefix + "QUIET",
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "disable-pager, dp",
|
||||
Usage: "disable mc internal pager and print to raw stdout",
|
||||
EnvVar: envPrefix + "DISABLE_PAGER",
|
||||
Hidden: true,
|
||||
},
|
||||
cli.BoolFlag{
|
||||
Name: "no-color",
|
||||
Usage: "disable color theme",
|
||||
|
||||
+23
-1
@@ -22,6 +22,7 @@ import (
|
||||
"context"
|
||||
"crypto/x509"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/lipgloss"
|
||||
@@ -85,12 +86,33 @@ var (
|
||||
// Terminal height/width, zero if not found
|
||||
globalTermWidth, globalTermHeight int
|
||||
|
||||
globalHelpPager *termPager
|
||||
globalDisablePagerFlag = "--disable-pager"
|
||||
globalPagerDisabled = false
|
||||
globalHelpPager *termPager
|
||||
globalPagerEnabledTerms = map[string]bool{
|
||||
"xterm": true,
|
||||
"xterm-256color": true,
|
||||
"tmux": true,
|
||||
"tmux-256color": true,
|
||||
}
|
||||
|
||||
// CA root certificates, a nil value means system certs pool will be used
|
||||
globalRootCAs *x509.CertPool
|
||||
)
|
||||
|
||||
func terminalSupportsPager() (ok bool) {
|
||||
_, ok = globalPagerEnabledTerms[os.Getenv("TERM")]
|
||||
return
|
||||
}
|
||||
|
||||
func parsePagerDisableFlag(args []string) {
|
||||
for _, arg := range args {
|
||||
if arg == globalDisablePagerFlag {
|
||||
globalPagerDisabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set global states. NOTE: It is deliberately kept monolithic to ensure we dont miss out any flags.
|
||||
func setGlobalsFromContext(ctx *cli.Context) error {
|
||||
quiet := ctx.IsSet("quiet") || ctx.GlobalIsSet("quiet")
|
||||
|
||||
+5
-1
@@ -137,6 +137,7 @@ func Main(args []string) error {
|
||||
// Wait until the user quits the pager
|
||||
defer globalHelpPager.WaitForExit()
|
||||
|
||||
parsePagerDisableFlag(args)
|
||||
// Run the app
|
||||
return registerApp(appName).Run(args)
|
||||
}
|
||||
@@ -510,8 +511,11 @@ func registerApp(name string) *cli.App {
|
||||
app.CustomAppHelpTemplate = mcHelpTemplate
|
||||
app.EnableBashCompletion = true
|
||||
app.OnUsageError = onUsageError
|
||||
if isTerminal() {
|
||||
|
||||
if isTerminal() && terminalSupportsPager() && !globalPagerDisabled {
|
||||
app.HelpWriter = globalHelpPager
|
||||
} else {
|
||||
app.HelpWriter = os.Stdout
|
||||
}
|
||||
|
||||
return app
|
||||
|
||||
+21
-5
@@ -19,6 +19,7 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -28,7 +29,7 @@ import (
|
||||
"github.com/muesli/reflow/wordwrap"
|
||||
)
|
||||
|
||||
var percentStyle = lipgloss.NewStyle().Width(5).Align(lipgloss.Left)
|
||||
var percentStyle = lipgloss.NewStyle().Width(4).Align(lipgloss.Left)
|
||||
|
||||
type model struct {
|
||||
viewport viewport.Model
|
||||
@@ -100,10 +101,25 @@ func (m model) headerView() string {
|
||||
}
|
||||
|
||||
func (m model) footerView() string {
|
||||
percent := percentStyle.Render(fmt.Sprintf("%d%%", int(m.viewport.ScrollPercent()*100)))
|
||||
info := fmt.Sprintf(" %s", percent)
|
||||
line := strings.Repeat("─", max(0, m.viewport.Width-lipgloss.Width(info)))
|
||||
return lipgloss.JoinHorizontal(lipgloss.Center, line, info)
|
||||
// Disables printing if the viewport is not ready
|
||||
if m.viewport.Width == 0 {
|
||||
return ""
|
||||
}
|
||||
if math.IsNaN(m.viewport.ScrollPercent()) {
|
||||
return ""
|
||||
}
|
||||
|
||||
viewP := int(m.viewport.ScrollPercent() * 100)
|
||||
info := fmt.Sprintf(" %s", percentStyle.Render(fmt.Sprintf("%d%%", viewP)))
|
||||
totalLength := m.viewport.Width - lipgloss.Width(info)
|
||||
finishedCount := int((float64(totalLength) / 100) * float64(viewP))
|
||||
|
||||
return lipgloss.JoinHorizontal(
|
||||
lipgloss.Center,
|
||||
info,
|
||||
strings.Repeat("/", finishedCount),
|
||||
strings.Repeat("─", max(0, totalLength-finishedCount)),
|
||||
)
|
||||
}
|
||||
|
||||
type termPager struct {
|
||||
|
||||
Reference in New Issue
Block a user