diff --git a/go.mod b/go.mod index dc16fcb..a46fe1b 100644 --- a/go.mod +++ b/go.mod @@ -21,7 +21,7 @@ require ( github.com/shirou/gopsutil/v3 v3.24.5 github.com/stretchr/testify v1.10.0 github.com/syndtr/goleveldb v1.0.0 - github.com/urfave/cli/v2 v2.27.6 + github.com/urfave/cli/v3 v3.3.3 github.com/valyala/bytebufferpool v1.0.0 github.com/wk8/go-ordered-map/v2 v2.1.8 github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 @@ -50,7 +50,6 @@ require ( github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect github.com/bahlo/generic-list-go v0.2.0 // indirect github.com/buger/jsonparser v1.1.1 // indirect - github.com/cpuguy83/go-md2man/v2 v2.0.7 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect github.com/golang/snappy v1.0.0 // indirect @@ -67,7 +66,6 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/rivo/uniseg v0.4.7 // indirect - github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/shopspring/decimal v1.4.0 // indirect github.com/spf13/cast v1.8.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect diff --git a/go.sum b/go.sum index 1c1a6d4..ebecba7 100644 --- a/go.sum +++ b/go.sum @@ -22,8 +22,6 @@ github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPn github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= -github.com/cpuguy83/go-md2man/v2 v2.0.7 h1:zbFlGlXEAKlwXpmvle3d8Oe3YnkKIK4xSRTd3sHPnBo= -github.com/cpuguy83/go-md2man/v2 v2.0.7/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -106,8 +104,6 @@ github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= -github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= -github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d h1:hrujxIzL1woJ7AwssoOcM/tq5JjjG2yYOc8odClEiXA= github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU= github.com/savioxavier/termlink v1.4.2 h1:PRlvcStluuSKA87KCIqzODknYeQ3XEcgJP6DvAAVl1c= @@ -127,8 +123,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/syndtr/goleveldb v1.0.0 h1:fBdIW9lB4Iz0n9khmH8w27SJ3QEJ7+IgjPEwGSZiFdE= github.com/syndtr/goleveldb v1.0.0/go.mod h1:ZVVdQEZoIme9iO1Ch2Jdy24qqXrMMOU6lpPAyBWyWuQ= -github.com/urfave/cli/v2 v2.27.6 h1:VdRdS98FNhKZ8/Az8B7MTyGQmpIr36O1EHybx/LaZ4g= -github.com/urfave/cli/v2 v2.27.6/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ= +github.com/urfave/cli/v3 v3.3.3 h1:byCBaVdIXuLPIDm5CYZRVG6NvT7tv1ECqdU4YzlEa3I= +github.com/urfave/cli/v3 v3.3.3/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc= diff --git a/internal/cli/display.go b/internal/cli/display.go index e4b31cb..66cb3a9 100644 --- a/internal/cli/display.go +++ b/internal/cli/display.go @@ -1,12 +1,13 @@ package cli import ( + "context" "fmt" "github.com/Equationzhao/g/internal/content" "github.com/Equationzhao/g/internal/display" "github.com/Equationzhao/g/internal/theme" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) var displayFlag = []cli.Flag{ @@ -15,8 +16,8 @@ var displayFlag = []cli.Flag{ Name: "tree-style", Usage: "set tree style [ascii/unicode(default)/rectangle]", Category: "DISPLAY", - Action: func(context *cli.Context, s string) error { - _ = context.Set("tree", "1") + Action: func(c context.Context, cmd *cli.Command, s string) error { + _ = cmd.Set("tree", "1") switch s { case "ascii", "ASCII", "Ascii": display.DefaultTreeStyle = display.TreeASCII @@ -31,17 +32,17 @@ var displayFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "T", - Aliases: []string{"tree"}, - Usage: "recursively list in tree", - DisableDefaultText: true, - Category: "DISPLAY", + Name: "T", + Aliases: []string{"tree"}, + Usage: "recursively list in tree", + HideDefault: true, + Category: "DISPLAY", }, &cli.StringFlag{ Name: "color", DefaultText: "auto", Usage: "when to use terminal colors [always|auto|never][basic|256|24bit]", - Action: func(context *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { switch s { case "always", "force": if theme.ColorLevel == theme.None { @@ -50,7 +51,7 @@ var displayFlag = []cli.Flag{ case "auto", "tty": // skip case "never", "none", "off": - _ = context.Set("no-color", "true") + _ = cmd.Set("no-color", "true") case "16", "basic": theme.ColorLevel = theme.Ascii case "256", "8bit": @@ -73,14 +74,14 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "R", - Aliases: []string{"recurse"}, - Usage: "recurse into directories", - DisableDefaultText: true, - Category: "DISPLAY", - Action: func(context *cli.Context, b bool) error { + Name: "R", + Aliases: []string{"recurse"}, + Usage: "recurse into directories", + HideDefault: true, + Category: "DISPLAY", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - if context.Args().Len() > 1 { + if cmd.Args().Len() > 1 { return fmt.Errorf("'--recurse' should not be used with more than one directory") } } @@ -88,11 +89,11 @@ var displayFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "byline", - Aliases: []string{"1", "oneline", "single-column"}, - Usage: "print by line", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "byline", + Aliases: []string{"1", "oneline", "single-column"}, + Usage: "print by line", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.Byline); !ok { p = display.NewByline() @@ -103,19 +104,19 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "zero", - Aliases: []string{"0"}, - Usage: "end each output line with NUL, not newline", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "zero", + Aliases: []string{"0"}, + Usage: "end each output line with NUL, not newline", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.Zero); !ok { p = display.NewZero() } - _ = context.Set("header", "0") - _ = context.Set("footer", "0") - _ = context.Set("statistic", "0") - _ = context.Set("total-size", "0") + _ = cmd.Set("header", "0") + _ = cmd.Set("footer", "0") + _ = cmd.Set("statistic", "0") + _ = cmd.Set("total-size", "0") sizeEnabler.DisableTotal() } return nil @@ -123,11 +124,11 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "m", - Aliases: []string{"comma"}, - Usage: "fill width with a comma separated list of entries", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "m", + Aliases: []string{"comma"}, + Usage: "fill width with a comma separated list of entries", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.CommaPrint); !ok { p = display.NewCommaPrint() @@ -138,11 +139,11 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "x", - Aliases: []string{"col", "across", "horizontal"}, - Usage: "list entries by lines instead of by columns", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "x", + Aliases: []string{"col", "across", "horizontal"}, + Usage: "list entries by lines instead of by columns", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.Across); !ok { p = display.NewAcross() @@ -153,11 +154,11 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "C", - Aliases: []string{"vertical"}, - Usage: "list entries by columns (default)", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "C", + Aliases: []string{"vertical"}, + Usage: "list entries by columns (default)", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.FitTerminal); !ok { p = display.NewFitTerminal() @@ -168,18 +169,18 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "j", - Aliases: []string{"json"}, - Usage: "output in json format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "j", + Aliases: []string{"json"}, + Usage: "output in json format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.JsonPrinter); !ok { p = display.NewJsonPrinter() } } - _ = context.Set("header", "0") + _ = cmd.Set("header", "0") theme.SetClassic() theme.ColorLevel = theme.None return nil @@ -190,8 +191,8 @@ var displayFlag = []cli.Flag{ Name: "tb-style", Aliases: []string{"table-style"}, Usage: "set table style [ascii(default)/unicode]", - Action: func(context *cli.Context, s string) error { - _ = context.Set("table", "1") + Action: func(c context.Context, cmd *cli.Command, s string) error { + _ = cmd.Set("table", "1") switch s { case "ascii", "ASCII", "Ascii": // no action needed @@ -205,11 +206,11 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "tb", - Aliases: []string{"table"}, - Usage: "output in table format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "tb", + Aliases: []string{"table"}, + Usage: "output in table format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.TablePrinter); !ok { p = display.NewTablePrinter(display.DefaultTB) @@ -220,16 +221,16 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "md", - Aliases: []string{"markdown", "Markdown"}, - Usage: "output in markdown-table format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "md", + Aliases: []string{"markdown", "Markdown"}, + Usage: "output in markdown-table format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.MDPrinter); !ok { p = display.NewMDPrinter() - _ = context.Set("no-color", "1") - err := context.Set("header", "1") + _ = cmd.Set("no-color", "1") + err := cmd.Set("header", "1") if err != nil { return err } @@ -240,16 +241,16 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "CSV", - Aliases: []string{"csv"}, - Usage: "output in csv format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "CSV", + Aliases: []string{"csv"}, + Usage: "output in csv format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.CSVPrinter); !ok { p = display.NewCSVPrinter() - _ = context.Set("no-color", "1") - _ = context.Set("no-icon", "1") + _ = cmd.Set("no-color", "1") + _ = cmd.Set("no-icon", "1") } } return nil @@ -257,16 +258,16 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "TSV", - Aliases: []string{"tsv"}, - Usage: "output in tsv format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "TSV", + Aliases: []string{"tsv"}, + Usage: "output in tsv format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.TSVPrinter); !ok { p = display.NewTSVPrinter() - _ = context.Set("no-color", "1") - _ = context.Set("no-icon", "1") + _ = cmd.Set("no-color", "1") + _ = cmd.Set("no-icon", "1") } } return nil @@ -278,7 +279,7 @@ var displayFlag = []cli.Flag{ DefaultText: "C", Usage: `across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C, table -tb, markdown -md, csv -csv, tsv -tsv, json -j, tree -T`, - Action: func(context *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { switch s { case "across", "x", "horizontal": if _, ok := p.(*display.Across); !ok { @@ -292,10 +293,10 @@ var displayFlag = []cli.Flag{ contentFunc = append( contentFunc, content.EnableFileMode(r), sizeEnabler.EnableSize(sizeUint, r), ) - if !context.Bool("O") { + if !cmd.Bool("O") { contentFunc = append(contentFunc, ownerEnabler.EnableOwner(r)) } - if !context.Bool("G") { + if !cmd.Bool("G") { contentFunc = append(contentFunc, groupEnabler.EnableGroup(r)) } for _, s := range timeType { @@ -319,8 +320,8 @@ var displayFlag = []cli.Flag{ case "Markdown", "md", "MD", "markdown": if _, ok := p.(*display.MDPrinter); !ok { p = display.NewMDPrinter() - _ = context.Set("no-color", "1") - err := context.Set("header", "1") + _ = cmd.Set("no-color", "1") + err := cmd.Set("header", "1") if err != nil { return err } @@ -328,19 +329,19 @@ var displayFlag = []cli.Flag{ case "CSV", "csv": if _, ok := p.(*display.CSVPrinter); !ok { p = display.NewCSVPrinter() - _ = context.Set("no-color", "1") - _ = context.Set("no-icon", "1") + _ = cmd.Set("no-color", "1") + _ = cmd.Set("no-icon", "1") } case "TSV", "tsv": if _, ok := p.(*display.TSVPrinter); !ok { p = display.NewTSVPrinter() - _ = context.Set("no-color", "1") - _ = context.Set("no-icon", "1") + _ = cmd.Set("no-color", "1") + _ = cmd.Set("no-icon", "1") } case "json", "j": if _, ok := p.(*display.JsonPrinter); !ok { p = display.NewJsonPrinter() - _ = context.Set("classic", "1") + _ = cmd.Set("classic", "1") theme.SetClassic() theme.ColorLevel = theme.None } @@ -358,7 +359,7 @@ var displayFlag = []cli.Flag{ &cli.StringFlag{ Name: "theme", Usage: "apply theme `path/to/theme`", - Action: func(context *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { err := theme.GetTheme(s) if err != nil { return err @@ -368,11 +369,11 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "colorless", - Aliases: []string{"no-color", "nocolor"}, - Usage: "without color", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "colorless", + Aliases: []string{"no-color", "nocolor"}, + Usage: "without color", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { theme.ColorLevel = theme.None } @@ -381,14 +382,14 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "classic", - Usage: "enable classic mode (no colors or icons)", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "classic", + Usage: "enable classic mode (no colors or icons)", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { theme.SetClassic() theme.ColorLevel = theme.None - err := context.Set("no-icon", "1") + err := cmd.Set("no-icon", "1") if err != nil { return err } @@ -398,32 +399,32 @@ var displayFlag = []cli.Flag{ Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "d", - Aliases: []string{"directory", "list-dirs"}, - DisableDefaultText: true, - Usage: "list directories themselves, not their contents", - Category: "DISPLAY", + Name: "d", + Aliases: []string{"directory", "list-dirs"}, + HideDefault: true, + Usage: "list directories themselves, not their contents", + Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "F", - Aliases: []string{"classify"}, - DisableDefaultText: true, - Usage: "append indicator (one of */=@|) to entries", - Category: "DISPLAY", + Name: "F", + Aliases: []string{"classify"}, + HideDefault: true, + Usage: "append indicator (one of */=@|) to entries", + Category: "DISPLAY", }, &cli.BoolFlag{ - Name: "ft", - Aliases: []string{"file-type"}, - DisableDefaultText: true, - Usage: "like classify, except do not append '*'", - Category: "DISPLAY", + Name: "ft", + Aliases: []string{"file-type"}, + HideDefault: true, + Usage: "like classify, except do not append '*'", + Category: "DISPLAY", }, &cli.UintFlag{ Name: "term-width", DefaultText: "auto", Usage: "set screen width", Category: "DISPLAY", - Action: func(context *cli.Context, u uint) error { + Action: func(c context.Context, cmd *cli.Command, u uint) error { display.CustomTermSize = u return nil }, diff --git a/internal/cli/filtering.go b/internal/cli/filtering.go index e09ef31..fdde7b9 100644 --- a/internal/cli/filtering.go +++ b/internal/cli/filtering.go @@ -1,6 +1,7 @@ package cli import ( + "context" "errors" "fmt" "slices" @@ -9,7 +10,7 @@ import ( "github.com/Equationzhao/g/internal/filter" strftime "github.com/itchyny/timefmt-go" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) var filteringFlag = []cli.Flag{ @@ -25,7 +26,7 @@ var filteringFlag = []cli.Flag{ Name: "I", Aliases: []string{"ignore"}, Usage: "ignore Glob patterns", - Action: func(context *cli.Context, s []string) error { + Action: func(c context.Context, cmd *cli.Command, s []string) error { if len(s) > 0 { f, err := filter.RemoveGlob(s...) if err != nil { @@ -41,7 +42,7 @@ var filteringFlag = []cli.Flag{ Name: "M", Aliases: []string{"match"}, Usage: "match Glob patterns", - Action: func(context *cli.Context, s []string) error { + Action: func(c context.Context, cmd *cli.Command, s []string) error { if len(s) > 0 { f, err := filter.GlobOnly(s...) if err != nil { @@ -54,11 +55,11 @@ var filteringFlag = []cli.Flag{ Category: "FILTERING", }, &cli.BoolFlag{ - Name: "show-only-hidden", - Aliases: []string{"hidden"}, - DisableDefaultText: true, - Usage: "show only hidden files(overridden by --show-hidden/-a/-A)", - Action: func(context *cli.Context, b bool) error { + Name: "show-only-hidden", + Aliases: []string{"hidden"}, + HideDefault: true, + Usage: "show only hidden files(overridden by --show-hidden/-a/-A)", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { itemFilterFunc = slices.DeleteFunc(itemFilterFunc, func(e *filter.ItemFilterFunc) bool { return e == &filter.RemoveHidden @@ -70,11 +71,11 @@ var filteringFlag = []cli.Flag{ Category: "FILTERING", }, &cli.BoolFlag{ - Name: "a", - Aliases: []string{"sh", "show-hidden"}, - DisableDefaultText: true, - Usage: "show hidden files", - Action: func(context *cli.Context, b bool) error { + Name: "a", + Aliases: []string{"sh", "show-hidden"}, + HideDefault: true, + Usage: "show hidden files", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { // remove filter.RemoveHidden itemFilterFunc = slices.DeleteFunc(itemFilterFunc, func(e *filter.ItemFilterFunc) bool { @@ -88,7 +89,7 @@ var filteringFlag = []cli.Flag{ &cli.StringSliceFlag{ Name: "ext", Usage: "show file which has target ext, eg: --ext=go,java", - Action: func(context *cli.Context, s []string) error { + Action: func(c context.Context, cmd *cli.Command, s []string) error { if len(s) > 0 { f := filter.ExtOnly(s...) itemFilterFunc = append(itemFilterFunc, &f) @@ -101,7 +102,7 @@ var filteringFlag = []cli.Flag{ Name: "no-ext", Aliases: []string{"noext"}, Usage: "show file which doesn't have target ext", - Action: func(context *cli.Context, s []string) error { + Action: func(c context.Context, cmd *cli.Command, s []string) error { if len(s) > 0 { f := filter.RemoveByExt(s...) itemFilterFunc = append(itemFilterFunc, &f) @@ -111,11 +112,11 @@ var filteringFlag = []cli.Flag{ Category: "FILTERING", }, &cli.BoolFlag{ - Name: "no-dir", - Aliases: []string{"nodir", "file"}, - DisableDefaultText: true, - Usage: "do not show directory", - Action: func(context *cli.Context, b bool) error { + Name: "no-dir", + Aliases: []string{"nodir", "file"}, + HideDefault: true, + Usage: "do not show directory", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { itemFilterFunc = append(itemFilterFunc, &filter.RemoveDir) } @@ -124,11 +125,11 @@ var filteringFlag = []cli.Flag{ Category: "FILTERING", }, &cli.BoolFlag{ - Name: "D", - Aliases: []string{"dir", "only-dir"}, - DisableDefaultText: true, - Usage: "show directory only", - Action: func(context *cli.Context, b bool) error { + Name: "D", + Aliases: []string{"dir", "only-dir"}, + HideDefault: true, + Usage: "show directory only", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { itemFilterFunc = append(itemFilterFunc, &filter.DirOnly) } @@ -137,11 +138,11 @@ var filteringFlag = []cli.Flag{ Category: "FILTERING", }, &cli.BoolFlag{ - Name: "B", - Aliases: []string{"ignore-backups"}, - DisableDefaultText: true, - Usage: "do not list implied entries ending with ~", - Action: func(context *cli.Context, b bool) error { + Name: "B", + Aliases: []string{"ignore-backups"}, + HideDefault: true, + Usage: "do not list implied entries ending with ~", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { itemFilterFunc = append(itemFilterFunc, &filter.RemoveBackups) } @@ -150,11 +151,11 @@ var filteringFlag = []cli.Flag{ Category: "FILTERING", }, &cli.BoolFlag{ - Name: "A", - Aliases: []string{"almost-all"}, - DisableDefaultText: true, - Usage: "do not list implied . and ..", - Action: func(context *cli.Context, b bool) error { + Name: "A", + Aliases: []string{"almost-all"}, + HideDefault: true, + Usage: "do not list implied . and ..", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { // remove filter.RemoveHidden itemFilterFunc = slices.DeleteFunc(itemFilterFunc, func(e *filter.ItemFilterFunc) bool { @@ -169,11 +170,11 @@ var filteringFlag = []cli.Flag{ Name: "only-mime", Usage: "only show file with given mime type", Category: "FILTERING", - Action: func(context *cli.Context, i []string) error { + Action: func(c context.Context, cmd *cli.Command, i []string) error { if len(i) > 0 { err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -191,7 +192,7 @@ var filteringFlag = []cli.Flag{ the time will be parsed using format: MM-dd, MM-dd HH:mm, HH:mm, YYYY-MM-dd, YYYY-MM-dd HH:mm, and the format set by --time-style`, Category: "FILTERING", - Action: func(ctx *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { possibleTimeFormat := []string{"01-02", "01-02 15:04", "15:04", "2006-01-02", "2006-01-02 15:04", timeFormat} for _, f := range possibleTimeFormat { if strings.HasPrefix(f, "+") { @@ -225,7 +226,7 @@ var filteringFlag = []cli.Flag{ Name: "after", Usage: "show items which was modified/access/created after given time, see --before", Category: "FILTERING", - Action: func(ctx *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { possibleTimeFormat := []string{"01-02", "01-02 15:04", "15:04", "2006-01-02", "2006-01-02 15:04", timeFormat} for _, f := range possibleTimeFormat { t, err := time.ParseInLocation(f, s, time.Local) diff --git a/internal/cli/g.go b/internal/cli/g.go index cfffe15..15042fa 100644 --- a/internal/cli/g.go +++ b/internal/cli/g.go @@ -2,6 +2,7 @@ package cli import ( "bufio" + "context" "errors" "fmt" "os" @@ -30,7 +31,7 @@ import ( "github.com/Equationzhao/pathbeautify" "github.com/hako/durafmt" "github.com/savioxavier/termlink" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" "github.com/xrash/smetrics" "go.szostok.io/version/upgrade" ) @@ -68,11 +69,11 @@ var ( allPart []string ) -var G *cli.App +var G *cli.Command func init() { itemFilterFunc = append(itemFilterFunc, &filter.RemoveHidden) - G = &cli.App{ + G = &cli.Command{ Name: "g", Usage: "a powerful ls", UsageText: "g [options] [path]", @@ -80,12 +81,12 @@ func init() { SliceFlagSeparator: ",", HideHelpCommand: true, Suggest: true, - OnUsageError: func(cCtx *cli.Context, err error, isSubcommand bool) error { + OnUsageError: func(cCtx context.Context, cmd *cli.Command, err error, isSubcommand bool) error { ReturnCode = 2 str := err.Error() const prefix = "flag provided but not defined: " if strings.HasPrefix(str, prefix) { - suggest := suggestFlag(cCtx.App.Flags, strings.TrimLeft(strings.TrimPrefix(str, prefix), "-")) + suggest := suggestFlag(cmd.Flags, strings.TrimLeft(strings.TrimPrefix(str, prefix), "-")) if suggest != "" { str = fmt.Sprintf("%s, Did you mean %s?", str, suggest) } @@ -102,7 +103,7 @@ func init() { Name: "check-new-version", Usage: "check if there's new release", Category: "\b\b\b META", // add \b to ensure the category is the first one to show - Action: func(context *cli.Context, b bool) error { + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { upgrade.WithUpdateCheckTimeout(5 * time.Second) notice := upgrade.NewGitHubDetector("Equationzhao", "g") @@ -111,21 +112,21 @@ func init() { } return nil }, - DisableDefaultText: true, + HideDefault: true, }, &cli.BoolFlag{ - Name: "no-path-transform", - Aliases: []string{"np"}, - DisableDefaultText: true, + Name: "no-path-transform", + Aliases: []string{"np"}, + HideDefault: true, Usage: `By default, .../a/b/c will be transformed to ../../a/b/c, and ~ will be replaced by homedir, using this flag to disable this feature`, }, &cli.BoolFlag{ - Name: "duplicate", - Aliases: []string{"dup"}, - Usage: "show duplicate files", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "duplicate", + Aliases: []string{"dup"}, + Usage: "show duplicate files", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { noOutputFunc = append(noOutputFunc, duplicateDetect.Enable()) hookPost = append( @@ -141,7 +142,7 @@ func init() { &cli.StringFlag{ Name: "init", Usage: `show the init script for shell, support zsh, bash, fish, powershell, nushell`, - Action: func(context *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { init, err := shell.Init(s) if err != nil { return err @@ -152,15 +153,15 @@ func init() { Category: "SHELL", }, &cli.BoolFlag{ - Name: "no-config", - Usage: "do not load config file", - DisableDefaultText: true, + Name: "no-config", + Usage: "do not load config file", + HideDefault: true, }, &cli.BoolFlag{ - Name: "bug", - Usage: "report bug", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "bug", + Usage: "report bug", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { _, _ = fmt.Println("please report bug to equationzhao@foxmail.com\nor file an issue at https://github.com/Equationzhao/g/issues") return Err4Exit{} }, @@ -246,7 +247,7 @@ func initHelpTemp() { if err != nil { configDir = filepath.Join("$UserConfigDir", "g") } - cli.AppHelpTemplate = fmt.Sprintf( + cli.CommandHelpTemplate = fmt.Sprintf( `USAGE: g [options] [files...] @@ -420,7 +421,7 @@ VIEW -o like -all, but do not list group information` func initVersionHelpFlags() { - cli.VersionPrinter = func(cCtx *cli.Context) { + cli.VersionPrinter = func(cmd *cli.Command) { _, _ = fmt.Fprintf(os.Stdout, `💡 g - a powerful ls | Version %s | Go Version %s @@ -434,19 +435,19 @@ func initVersionHelpFlags() { } cli.VersionFlag = &cli.BoolFlag{ - Name: "version", - Aliases: []string{"v"}, - Usage: "print the version", - DisableDefaultText: true, - Category: "\b\b\b META", + Name: "version", + Aliases: []string{"v"}, + Usage: "print the version", + HideDefault: true, + Category: "\b\b\b META", } cli.HelpFlag = &cli.BoolFlag{ - Name: "help", - Aliases: []string{"h", "?"}, - Usage: "show help", - DisableDefaultText: true, - Category: "\b\b\b META", + Name: "help", + Aliases: []string{"h", "?"}, + Usage: "show help", + HideDefault: true, + Category: "\b\b\b META", } } @@ -494,54 +495,54 @@ func suggestFlag(flags []cli.Flag, provided string) string { return suggestion } -var logic = func(context *cli.Context) error { +var logic = func(ctx context.Context, cmd *cli.Command) error { var ( minorErr = false seriousErr = false ) - path := context.Args().Slice() + path := cmd.Args().Slice() - if !context.Bool("no-icon") && (context.Bool("icon") || context.Bool("all")) { + if !cmd.Bool("no-icon") && (cmd.Bool("icon") || cmd.Bool("all")) { nameToDisplay.SetIcon() } - if context.Bool("F") { + if cmd.Bool("F") { nameToDisplay.SetClassify() } - if context.Bool("file-type") { + if cmd.Bool("file-type") { nameToDisplay.SetClassify() nameToDisplay.SetFileType() } if _, ok := p.(*display.JsonPrinter); ok { nameToDisplay.SetJson() } - git := context.Bool("git") + git := cmd.Bool("git") if git { contentFunc = append(contentFunc, gitEnabler.Enable(r)) } - gitBranch := context.Bool("git-repo-branch") + gitBranch := cmd.Bool("git-repo-branch") if gitBranch { contentFunc = append(contentFunc, gitRepoEnabler.Enable(r)) } - gitRepoStatus := context.Bool("git-repo-status") + gitRepoStatus := cmd.Bool("git-repo-status") if gitRepoStatus { contentFunc = append(contentFunc, gitRepoEnabler.EnableStatus(r)) } - if context.Bool("git-detail") { + if cmd.Bool("git-detail") { contentFunc = append(contentFunc, gitCommitEnabler.EnableHash(r), gitCommitEnabler.EnableAuthor(r), gitCommitEnabler.EnableAuthorDateWithTimeFormat(r, timeFormat)) } - if context.Bool("flags") { + if cmd.Bool("flags") { contentFunc = append(contentFunc, flagsEnabler.Enable()) } - if context.Bool("no-dereference") { + if cmd.Bool("no-dereference") { nameToDisplay.SetNoDeference() } - fuzzy := context.Bool("fuzzy") + fuzzy := cmd.Bool("fuzzy") if fuzzy { defer func() { for i := 0; i < 10; i++ { @@ -554,28 +555,28 @@ var logic = func(context *cli.Context) error { }() } - disableIndex := context.Bool("di") + disableIndex := cmd.Bool("di") wgUpdateIndex := sync.WaitGroup{} nameToDisplay.SetQuoteString(`'`) // set quote to always - if context.Bool("Q") { + if cmd.Bool("Q") { nameToDisplay.SetQuote() } // if no quote, set quote to never // this will override the quote set by -Q - if context.Bool("N") { + if cmd.Bool("N") { nameToDisplay.UnsetQuote() } - if context.Bool("mounts") { + if cmd.Bool("mounts") { nameToDisplay.SetMounts() } // no path transform - transformEnabled := !context.Bool("np") - if rp := context.String("relative-to"); rp != "" { + transformEnabled := !cmd.Bool("np") + if rp := cmd.String("relative-to"); rp != "" { if transformEnabled { rp = pathbeautify.Beautify(rp) } @@ -583,13 +584,13 @@ var logic = func(context *cli.Context) error { rp = temp } nameToDisplay.SetRelativeTo(rp) - } else if context.Bool("fp") { + } else if cmd.Bool("fp") { nameToDisplay.SetFullPath() } contentFunc = append(contentFunc, nameToDisplay.Enable(r)) itemFilter := filter.NewItemFilter(itemFilterFunc...) - gitignore := context.Bool("git-ignore") + gitignore := cmd.Bool("git-ignore") removeGitIgnore := new(filter.ItemFilterFunc) if gitignore { itemFilter.AppendTo(removeGitIgnore) @@ -599,23 +600,23 @@ var logic = func(context *cli.Context) error { path = append(path, ".") } - depth := context.Int("depth") + depth := cmd.Int("depth") // flag: if d is set, display directory them self - flagd := context.Bool("d") + flagd := cmd.Bool("d") // flag: if A is set - flagA := context.Bool("A") - flagR := context.Bool("R") + flagA := cmd.Bool("A") + flagR := cmd.Bool("R") if flagR { depthLimitMap = make(map[string]int) } - header := context.Bool("header") - footer := context.Bool("footer") - if context.Bool("statistic") { + header := cmd.Bool("header") + footer := cmd.Bool("footer") + if cmd.Bool("statistic") { nameToDisplay.SetStatistics(&contents.Statistics{}) } - hyperlink := context.String("hyperlink") + hyperlink := cmd.String("hyperlink") switch hyperlink { case "never": case "always": @@ -635,8 +636,8 @@ var logic = func(context *cli.Context) error { } } - flagSharp := context.Bool("#") - tree := context.Bool("tree") + flagSharp := cmd.Bool("#") + tree := cmd.Bool("tree") if tree { if _, ok := p.(*display.TreePrinter); !ok { p = display.NewTreePrinter() @@ -646,20 +647,20 @@ var logic = func(context *cli.Context) error { } } - smartGroup := context.Bool("smart-group") + smartGroup := cmd.Bool("smart-group") if smartGroup { groupEnabler.EnableSmartMode() } - if n := context.Uint("n"); n > 0 && !tree { + if n := cmd.Uint("n"); n > 0 && !tree { contentFilter.LimitN = n } longestEachPart := make(map[string]int) startDir, _ := os.Getwd() - dereference := context.Bool("dereference") + dereference := cmd.Bool("dereference") - if !context.Bool("colorless") && !context.Bool("classic") && context.String("theme") == "" { + if !cmd.Bool("colorless") && !cmd.Bool("classic") && cmd.String("theme") == "" { if config.Default.ThemeLocation != "" { err := theme.GetTheme(config.Default.ThemeLocation) if err != nil { @@ -670,7 +671,7 @@ var logic = func(context *cli.Context) error { theme.ConvertThemeColor() - if len(path) != 0 && context.Bool("stdin") { + if len(path) != 0 && cmd.Bool("stdin") { newPath, err := getStdin() if err != nil { return err diff --git a/internal/cli/index.go b/internal/cli/index.go index 4b51dc8..5a92703 100644 --- a/internal/cli/index.go +++ b/internal/cli/index.go @@ -1,6 +1,7 @@ package cli import ( + "context" "errors" "fmt" "os" @@ -8,24 +9,24 @@ import ( "github.com/Equationzhao/g/internal/index" "github.com/Equationzhao/pathbeautify" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) var indexFlags = []cli.Flag{ &cli.BoolFlag{ - Name: "disable-index", - Aliases: []string{"di", "no-update"}, - Usage: "disable updating index", - Category: "INDEX", - DisableDefaultText: true, + Name: "disable-index", + Aliases: []string{"di", "no-update"}, + Usage: "disable updating index", + Category: "INDEX", + HideDefault: true, }, &cli.BoolFlag{ - Name: "rebuild-index", - Aliases: []string{"ri", "remove-all"}, - Usage: "rebuild index", - DisableDefaultText: true, - Category: "INDEX", - Action: func(context *cli.Context, b bool) error { + Name: "rebuild-index", + Aliases: []string{"ri", "remove-all"}, + Usage: "rebuild index", + HideDefault: true, + Category: "INDEX", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { err := index.RebuildIndex() if err != nil { @@ -36,22 +37,22 @@ var indexFlags = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "fuzzy", - Aliases: []string{"fz", "f"}, - Usage: "fuzzy search", - DisableDefaultText: true, - Category: "INDEX", + Name: "fuzzy", + Aliases: []string{"fz", "f"}, + Usage: "fuzzy search", + HideDefault: true, + Category: "INDEX", }, &cli.StringSliceFlag{ Name: "remove-index", Aliases: []string{"rm"}, Usage: "remove paths from index", Category: "INDEX", - Action: func(context *cli.Context, i []string) error { + Action: func(c context.Context, cmd *cli.Command, i []string) error { var errSum error = nil beautification := true - if context.Bool("np") { // --no-path-transform + if cmd.Bool("np") { // --no-path-transform beautification = false } @@ -79,12 +80,12 @@ var indexFlags = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "list-index", - Aliases: []string{"li"}, - Usage: "list index", - DisableDefaultText: true, - Category: "INDEX", - Action: func(context *cli.Context, b bool) error { + Name: "list-index", + Aliases: []string{"li"}, + Usage: "list index", + HideDefault: true, + Category: "INDEX", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { keys, err := index.All() if err != nil { @@ -98,12 +99,12 @@ var indexFlags = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "remove-current-path", - Aliases: []string{"rcp", "rc", "rmc"}, - Usage: "remove current path from index", - Category: "INDEX", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "remove-current-path", + Aliases: []string{"rcp", "rc", "rmc"}, + Usage: "remove current path from index", + Category: "INDEX", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { r, err := os.Getwd() if err != nil { @@ -118,12 +119,12 @@ var indexFlags = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "remove-invalid-path", - Aliases: []string{"rip"}, - Usage: "remove invalid paths from index", - Category: "INDEX", - DisableDefaultText: true, - Action: func(ctx *cli.Context, b bool) error { + Name: "remove-invalid-path", + Aliases: []string{"rip"}, + Usage: "remove invalid paths from index", + Category: "INDEX", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { paths, err := index.All() if err != nil { diff --git a/internal/cli/sort.go b/internal/cli/sort.go index 5efc2e9..d709928 100644 --- a/internal/cli/sort.go +++ b/internal/cli/sort.go @@ -1,11 +1,12 @@ package cli import ( + "context" "fmt" "slices" "github.com/Equationzhao/g/internal/sorter" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) var sortingFlags = []cli.Flag{ @@ -20,7 +21,7 @@ var sortingFlags = []cli.Flag{ name,.name(sorts by name without a leading dot), size,time,owner,group,extension,inode,width,mime. following '-descend' to sort descending`, - Action: func(context *cli.Context, slice []string) error { + Action: func(c context.Context, cmd *cli.Command, slice []string) error { if slices.ContainsFunc(slice, func(s string) bool { nosort := []string{"none", "None", "nosort", "U"} return slices.Contains(nosort, s) @@ -49,14 +50,14 @@ var sortingFlags = []cli.Flag{ case ".Name-descend": sort.AddOption(sorter.ByNameWithoutALeadingDotCaseSensitiveDescend) case "size-descend", "S", "sizesort": - if context.Bool("recursive-size") { - sort.AddOption(sorter.ByRecursiveSizeDescend(context.Int("depth"))) + if cmd.Bool("recursive-size") { + sort.AddOption(sorter.ByRecursiveSizeDescend(cmd.Int("depth"))) } else { sort.AddOption(sorter.BySizeDescend) } case "size": - if context.Bool("recursive-size") { - sort.AddOption(sorter.ByRecursiveSizeAscend(context.Int("depth"))) + if cmd.Bool("recursive-size") { + sort.AddOption(sorter.ByRecursiveSizeAscend(cmd.Int("depth"))) } else { sort.AddOption(sorter.BySizeAscend) } @@ -95,7 +96,7 @@ var sortingFlags = []cli.Flag{ case "mime", "mimetype", "Mime", "Mimetype": err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -105,7 +106,7 @@ var sortingFlags = []cli.Flag{ case "mime-descend", "mimetype-descend", "Mime-descend", "Mimetype-descend": err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -129,11 +130,11 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "sort-reverse", - Aliases: []string{"reverse", "r"}, - Usage: "reverse the order of the sort", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "sort-reverse", + Aliases: []string{"reverse", "r"}, + Usage: "reverse the order of the sort", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { sort.Reverse() } @@ -142,11 +143,11 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "df", - Aliases: []string{"dir-first", "group-directories-first"}, - Usage: "list directories before other files", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "df", + Aliases: []string{"dir-first", "group-directories-first"}, + Usage: "list directories before other files", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { sort.DirFirst() } else { @@ -157,13 +158,13 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "S", - Aliases: []string{"sort-by-size", "sizesort"}, - Usage: "sort by file size, largest first(descending)", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { - if context.Bool("srs") { // recursive size - sort.AddOption(sorter.ByRecursiveSizeDescend(context.Int("depth"))) + Name: "S", + Aliases: []string{"sort-by-size", "sizesort"}, + Usage: "sort by file size, largest first(descending)", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { + if cmd.Bool("srs") { // recursive size + sort.AddOption(sorter.ByRecursiveSizeDescend(cmd.Int("depth"))) } else { sort.AddOption(sorter.BySizeDescend) } @@ -172,34 +173,34 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "X", - Aliases: []string{"sort-by-ext"}, - Usage: "sort alphabetically by entry extension", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "X", + Aliases: []string{"sort-by-ext"}, + Usage: "sort alphabetically by entry extension", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { sort.AddOption(sorter.ByExtensionAscend) return nil }, Category: "SORTING", }, &cli.BoolFlag{ - Name: "width", - Usage: "sort by entry name width", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "width", + Usage: "sort by entry name width", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { sort.AddOption(sorter.ByNameWidthAscend) return nil }, Category: "SORTING", }, &cli.BoolFlag{ - Name: "sort-by-mime", - Usage: "sort by mimetype", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "sort-by-mime", + Usage: "sort by mimetype", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -212,14 +213,14 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "sort-by-mime-descend", - Usage: "sort by mimetype, descending", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "sort-by-mime-descend", + Usage: "sort by mimetype, descending", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -233,14 +234,14 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "sort-by-mime-parent", - Usage: "sort by mimetype parent", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "sort-by-mime-parent", + Usage: "sort by mimetype parent", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -254,14 +255,14 @@ var sortingFlags = []cli.Flag{ Category: "SORTING", }, &cli.BoolFlag{ - Name: "sort-by-mime-parent-descend", - Usage: "sort by mimetype parent", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "sort-by-mime-parent-descend", + Usage: "sort by mimetype parent", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -278,18 +279,18 @@ var sortingFlags = []cli.Flag{ Name: "versionsort", Aliases: []string{"sort-by-version"}, Usage: "sort by version numbers, ascending", - Action: func(context *cli.Context, b bool) error { + Action: func(c context.Context, cmd *cli.Command, b bool) error { sort.AddOption(sorter.ByVersionAscend) return nil }, Category: "SORTING", }, &cli.BoolFlag{ - Name: "U", - Aliases: []string{"nosort", "no-sort"}, - Usage: "do not sort; list entries in directory order. ", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "U", + Aliases: []string{"nosort", "no-sort"}, + Usage: "do not sort; list entries in directory order. ", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { sort.Reset() return nil }, diff --git a/internal/cli/view.go b/internal/cli/view.go index 64e132a..70f0892 100644 --- a/internal/cli/view.go +++ b/internal/cli/view.go @@ -1,6 +1,7 @@ package cli import ( + "context" "errors" "fmt" "runtime" @@ -11,17 +12,17 @@ import ( "github.com/Equationzhao/g/internal/display" "github.com/Equationzhao/g/internal/filter" "github.com/gabriel-vasile/mimetype" - "github.com/urfave/cli/v2" + "github.com/urfave/cli/v3" ) var viewFlag = []cli.Flag{ // VIEW &cli.BoolFlag{ - Name: "header", - Aliases: []string{"title"}, - Usage: "add a header row", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "header", + Aliases: []string{"title"}, + Usage: "add a header row", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.Byline); !ok { p = display.NewByline() @@ -32,10 +33,10 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "footer", - Usage: "add a footer row", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "footer", + Usage: "add a footer row", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { if _, ok := p.(*display.Byline); !ok { p = display.NewByline() @@ -46,17 +47,17 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "statistic", - Usage: "show statistic info", - DisableDefaultText: true, - Category: "VIEW", + Name: "statistic", + Usage: "show statistic info", + HideDefault: true, + Category: "VIEW", }, &cli.StringSliceFlag{ Name: "time-type", Usage: "time type, mod(default), create, access, all, birth[macOS only]", - EnvVars: []string{"TIME_TYPE"}, - Action: func(context *cli.Context, ss []string) error { - _ = context.Set("time", "1") + Sources: cli.EnvVars("TIME_TYPE"), + Action: func(c context.Context, cmd *cli.Command, ss []string) error { + _ = cmd.Set("time", "1") timeType = make([]string, 0, len(ss)) accepts := []string{"mod", "modified", "create", "cr", "access", "ac", "birth"} for _, s := range ss { @@ -74,13 +75,13 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "access", - Aliases: []string{"ac", "accessed"}, - Usage: "accessed time", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "access", + Aliases: []string{"ac", "accessed"}, + Usage: "accessed time", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - _ = context.Set("time", "1") + _ = cmd.Set("time", "1") timeType = append(timeType, "access") } return nil @@ -88,13 +89,13 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "modify", - Aliases: []string{"mod", "modified"}, - Usage: "modified time", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "modify", + Aliases: []string{"mod", "modified"}, + Usage: "modified time", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - _ = context.Set("time", "1") + _ = cmd.Set("time", "1") timeType = append(timeType, "mod") } return nil @@ -102,13 +103,13 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "create", - Aliases: []string{"cr", "created"}, - Usage: "created time", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "create", + Aliases: []string{"cr", "created"}, + Usage: "created time", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - _ = context.Set("time", "1") + _ = cmd.Set("time", "1") timeType = append(timeType, "create") } return nil @@ -116,15 +117,15 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "birth", - Usage: "birth time[macOS only]", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "birth", + Usage: "birth time[macOS only]", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if runtime.GOOS != "darwin" { return errors.New("birth is only supported in darwin") } if b { - _ = context.Set("time", "1") + _ = cmd.Set("time", "1") timeType = append(timeType, "birth") } return nil @@ -135,8 +136,8 @@ var viewFlag = []cli.Flag{ Name: "si", Usage: `use powers of 1000 not 1024 for size format eg: 1K = 1000 bytes`, - EnvVars: []string{"SI"}, - Action: func(context *cli.Context, b bool) error { + Sources: cli.EnvVars("SI"), + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { sizeEnabler.SetSI() } @@ -148,12 +149,12 @@ var viewFlag = []cli.Flag{ Aliases: []string{"su", "block-size"}, Usage: `size unit: bit, b, k, m, g, t, auto`, - Action: func(context *cli.Context, s string) error { - _ = context.Set("size", "1") + Action: func(c context.Context, cmd *cli.Command, s string) error { + _ = cmd.Set("size", "1") if strings.EqualFold(s, "auto") { return nil } - si := context.Bool("si") + si := cmd.Bool("si") sizeUint = contents.ConvertFromSizeString(s, si) if sizeUint == contents.Unknown { ReturnCode = 2 @@ -169,9 +170,9 @@ var viewFlag = []cli.Flag{ valid timestamp styles are default, iso, long-iso, full-iso, locale, custom +FORMAT like date(1). (default: +%b %d %H:%M ,like Jan 02 15:04)`, - EnvVars: []string{"TIME_STYLE"}, - Action: func(context *cli.Context, s string) error { - _ = context.Set("time", "1") + Sources: cli.EnvVars("TIME_STYLE"), + Action: func(c context.Context, cmd *cli.Command, s string) error { + _ = cmd.Set("time", "1") /* The TIME_STYLE argument can be full-iso, long-iso, iso, locale, or +FORMAT. FORMAT is interpreted like in date(1). @@ -202,12 +203,12 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "full-time", - Usage: "like -all/l --time-style=full-iso", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "full-time", + Usage: "like -all/l --time-style=full-iso", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - _ = context.Set("time", "1") + _ = cmd.Set("time", "1") timeFormat = "2006-01-02 15:04:05.000000000 -0700" } return nil @@ -215,11 +216,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "#", - DisableDefaultText: true, - Usage: "print entry Number for each entry", - Category: "DISPLAY", - Action: func(context *cli.Context, b bool) error { + Name: "#", + HideDefault: true, + Usage: "print entry Number for each entry", + Category: "DISPLAY", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, contents.NewIndexEnabler().Enable()) } @@ -227,11 +228,11 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "inode", - Aliases: []string{"i"}, - Usage: "show inode[linux/darwin only]", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "inode", + Aliases: []string{"i"}, + Usage: "show inode[linux/darwin only]", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { i := contents.NewInodeEnabler() contentFunc = append(contentFunc, i.Enable(r)) return nil @@ -239,10 +240,10 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "uid", - Usage: "show uid instead of username [sid in windows]", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "uid", + Usage: "show uid instead of username [sid in windows]", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { ownerEnabler.EnableNumeric() } @@ -251,10 +252,10 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "gid", - Usage: "show gid instead of groupname [sid in windows]", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "gid", + Usage: "show gid instead of groupname [sid in windows]", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { groupEnabler.EnableNumeric() } @@ -263,11 +264,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "numeric", - Aliases: []string{"numeric-uid-gid"}, - Usage: "list numeric user and group IDs instead of name [sid in windows]", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "numeric", + Aliases: []string{"numeric-uid-gid"}, + Usage: "list numeric user and group IDs instead of name [sid in windows]", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { ownerEnabler.EnableNumeric() groupEnabler.EnableNumeric() @@ -277,11 +278,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "octal-perm", - Aliases: []string{"octal-permission"}, - Usage: "list each file's permission in octal format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "octal-perm", + Aliases: []string{"octal-permission"}, + Usage: "list each file's permission in octal format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, contents.EnableFileOctalPermissions(r)) } @@ -290,11 +291,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "perm", - Aliases: []string{"permission"}, - Usage: "show permission", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "perm", + Aliases: []string{"permission"}, + Usage: "show permission", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, contents.EnableFileMode(r)) } @@ -303,10 +304,10 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "size", - Usage: "show file/dir size", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "size", + Usage: "show file/dir size", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, sizeEnabler.EnableSize(sizeUint, r)) } @@ -315,13 +316,13 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "recursive-size", - Usage: "show recursive size of dir, only work with --size", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "recursive-size", + Usage: "show recursive size of dir, only work with --size", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - _ = context.Set("size", "1") - n := context.Int("depth") + _ = cmd.Set("size", "1") + n := cmd.Int("depth") sizeEnabler.SetRecursive(contents.NewSizeRecursive(n)) } return nil @@ -329,11 +330,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "block", - Aliases: []string{"blocks"}, - Usage: "show block size", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "block", + Aliases: []string{"blocks"}, + Usage: "show block size", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, blockEnabler.Enable(r)) } @@ -342,11 +343,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "lh", - Aliases: []string{"human-readable"}, - DisableDefaultText: true, - Usage: "show human readable size", - Action: func(context *cli.Context, b bool) error { + Name: "lh", + Aliases: []string{"human-readable"}, + HideDefault: true, + Usage: "show human readable size", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append( contentFunc, contents.EnableFileMode(r), sizeEnabler.EnableSize(sizeUint, r), @@ -364,11 +365,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "H", - Aliases: []string{"link"}, - Usage: "list each file's number of hard links", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "H", + Aliases: []string{"link"}, + Usage: "list each file's number of hard links", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { link := contents.NewLinkEnabler() contentFunc = append(contentFunc, link.Enable(r)) @@ -378,11 +379,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "owner", - Aliases: []string{"author"}, - Usage: "show owner", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "owner", + Aliases: []string{"author"}, + Usage: "show owner", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, ownerEnabler.EnableOwner(r)) } @@ -391,10 +392,10 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "group", - Usage: "show group", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "group", + Usage: "show group", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append(contentFunc, groupEnabler.EnableGroup(r)) } @@ -403,16 +404,16 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "smart-group", - Usage: "only show group if it has a different name from owner", - DisableDefaultText: true, - Category: "VIEW", + Name: "smart-group", + Usage: "only show group if it has a different name from owner", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "time", - Usage: "show time", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "time", + Usage: "show time", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { for _, s := range timeType { contentFunc = append(contentFunc, contents.EnableTime(timeFormat, s, r)) @@ -423,25 +424,25 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "no-icon", - Usage: "disable icon(always override --icon)", - Aliases: []string{"noicon", "ni"}, - DisableDefaultText: true, - Category: "VIEW", + Name: "no-icon", + Usage: "disable icon(always override --icon)", + Aliases: []string{"noicon", "ni"}, + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "icon", - Usage: "show icon", - Aliases: []string{"icons"}, - DisableDefaultText: true, - Category: "VIEW", + Name: "icon", + Usage: "show icon", + Aliases: []string{"icons"}, + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "fp", - Usage: "show full path", - Aliases: []string{"full-path", "fullpath"}, - DisableDefaultText: true, - Category: "VIEW", + Name: "fp", + Usage: "show full path", + Aliases: []string{"full-path", "fullpath"}, + HideDefault: true, + Category: "VIEW", }, &cli.StringFlag{ Name: "relative-to", @@ -450,13 +451,13 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "total-size", - Usage: "show total size", - DisableDefaultText: true, - Category: "VIEW", - Action: func(context *cli.Context, b bool) error { + Name: "total-size", + Usage: "show total size", + HideDefault: true, + Category: "VIEW", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { - _ = context.Set("size", "1") + _ = cmd.Set("size", "1") sizeEnabler.SetEnableTotal() } return nil @@ -471,17 +472,17 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "mime", - Usage: "show mime file type", - Aliases: []string{"mime-type", "mimetype"}, - DisableDefaultText: true, - Category: "VIEW", - Action: func(context *cli.Context, b bool) error { + Name: "mime", + Usage: "show mime file type", + Aliases: []string{"mime-type", "mimetype"}, + HideDefault: true, + Category: "VIEW", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { exact := contents.NewMimeFileTypeEnabler() err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -493,19 +494,19 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "mime-parent", - Usage: "show mime parent type", - Aliases: []string{"mime-parent-type", "mimetype-parent"}, - Category: "VIEW", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "mime-parent", + Usage: "show mime parent type", + Aliases: []string{"mime-parent-type", "mimetype-parent"}, + Category: "VIEW", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { exact := contents.NewMimeFileTypeEnabler() exact.ParentOnly = true err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -517,16 +518,16 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "charset", - Usage: "show charset of text file in mime type field", - DisableDefaultText: true, - Category: "VIEW", - Action: func(context *cli.Context, b bool) error { + Name: "charset", + Usage: "show charset of text file in mime type field", + HideDefault: true, + Category: "VIEW", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { charset := contents.NewCharsetEnabler() err := limitOnce.Do( func() error { - return setLimit(context) + return setLimit(cmd) }, ) if err != nil { @@ -543,21 +544,21 @@ var viewFlag = []cli.Flag{ md5, sha1, sha224, sha256, sha384, sha512, crc32`, Aliases: []string{"ca"}, DefaultText: "sha1", - Value: cli.NewStringSlice("sha1"), + Value: []string{"sha1"}, Category: "VIEW", - Action: func(context *cli.Context, i []string) error { - _ = context.Set("checksum", "1") + Action: func(c context.Context, cmd *cli.Command, i []string) error { + _ = cmd.Set("checksum", "1") return nil }, }, &cli.BoolFlag{ - Name: "checksum", - Usage: `show checksum of file with algorithm, see --checksum-algorithm`, - Aliases: []string{"cs"}, - DisableDefaultText: true, - Category: "VIEW", - Action: func(context *cli.Context, b bool) error { - ss := context.StringSlice("checksum-algorithm") + Name: "checksum", + Usage: `show checksum of file with algorithm, see --checksum-algorithm`, + Aliases: []string{"cs"}, + HideDefault: true, + Category: "VIEW", + Action: func(c context.Context, cmd *cli.Command, b bool) error { + ss := cmd.StringSlice("checksum-algorithm") if ss == nil { ss = []string{"sha1"} } @@ -590,76 +591,76 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "git", - Usage: "show git status [if git is installed]", - Aliases: []string{"git-status"}, - DisableDefaultText: true, - Category: "VIEW", + Name: "git", + Usage: "show git status [if git is installed]", + Aliases: []string{"git-status"}, + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "git-detail", - Usage: "show git commit detail with hash, author, author date [if git is installed]", - DisableDefaultText: true, - Category: "VIEW", + Name: "git-detail", + Usage: "show git commit detail with hash, author, author date [if git is installed]", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "git-repo-branch", - Usage: "list root of git-tree branch [if git is installed]", - Aliases: []string{"branch"}, - DisableDefaultText: true, - Category: "VIEW", + Name: "git-repo-branch", + Usage: "list root of git-tree branch [if git is installed]", + Aliases: []string{"branch"}, + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "git-repo-status", - Usage: "list root of git-tree status [if git is installed]", - Aliases: []string{"repo-status"}, - DisableDefaultText: true, - Category: "VIEW", + Name: "git-repo-status", + Usage: "list root of git-tree status [if git is installed]", + Aliases: []string{"repo-status"}, + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "Q", - Aliases: []string{"quote-name"}, - Usage: "enclose entry names in double quotes(overridden by --literal)", - DisableDefaultText: true, - Category: "VIEW", + Name: "Q", + Aliases: []string{"quote-name"}, + Usage: "enclose entry names in double quotes(overridden by --literal)", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "mounts", - Usage: "show mount details", - DisableDefaultText: true, - Category: "VIEW", + Name: "mounts", + Usage: "show mount details", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "N", - Aliases: []string{"literal"}, - Usage: "print entry names without quoting", - DisableDefaultText: true, - Category: "VIEW", + Name: "N", + Aliases: []string{"literal"}, + Usage: "print entry names without quoting", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "no-dereference", - Usage: "do not follow symbolic links", - DisableDefaultText: true, - Category: "VIEW", + Name: "no-dereference", + Usage: "do not follow symbolic links", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "dereference", - Usage: "dereference symbolic links", - DisableDefaultText: true, - Category: "VIEW", + Name: "dereference", + Usage: "dereference symbolic links", + HideDefault: true, + Category: "VIEW", }, &cli.StringFlag{ Name: "hyperlink", Usage: "attach hyperlink to filenames [auto|always|never]", Category: "VIEW", DefaultText: "auto", - Action: func(context *cli.Context, s string) error { + Action: func(c context.Context, cmd *cli.Command, s string) error { if strings.EqualFold(s, "auto") { - _ = context.Set("hyperlink", "auto") + _ = cmd.Set("hyperlink", "auto") } else if strings.EqualFold(s, "always") { - _ = context.Set("hyperlink", "always") + _ = cmd.Set("hyperlink", "always") } else if strings.EqualFold(s, "never") { - _ = context.Set("hyperlink", "never") + _ = cmd.Set("hyperlink", "never") } else { return fmt.Errorf("invalid hyperlink value: %s", s) } @@ -667,10 +668,10 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "o", - DisableDefaultText: true, - Usage: "like -all, but do not list group information", - Action: func(context *cli.Context, b bool) error { + Name: "o", + HideDefault: true, + Usage: "like -all, but do not list group information", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { // remove filter.RemoveHidden newFF := make([]*filter.ItemFilterFunc, 0, len(itemFilterFunc)) @@ -696,10 +697,10 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "g", - DisableDefaultText: true, - Usage: "like -all, but do not list owner", - Action: func(context *cli.Context, b bool) error { + Name: "g", + HideDefault: true, + Usage: "like -all, but do not list owner", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { // remove filter.RemoveHidden newFF := make([]*filter.ItemFilterFunc, 0, len(itemFilterFunc)) @@ -725,34 +726,34 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "G", - DisableDefaultText: true, - Aliases: []string{"no-group"}, - Usage: "in a long listing, don't print group names", - Category: "VIEW", + Name: "G", + HideDefault: true, + Aliases: []string{"no-group"}, + Usage: "in a long listing, don't print group names", + Category: "VIEW", }, &cli.BoolFlag{ - Name: "O", - DisableDefaultText: true, - Aliases: []string{"no-owner"}, - Usage: "in a long listing, don't print owner names", - Category: "VIEW", + Name: "O", + HideDefault: true, + Aliases: []string{"no-owner"}, + Usage: "in a long listing, don't print owner names", + Category: "VIEW", }, &cli.BoolFlag{ - Name: "l", - Aliases: []string{"long"}, - Usage: "use a long listing format", - Category: "VIEW", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "l", + Aliases: []string{"long"}, + Usage: "use a long listing format", + Category: "VIEW", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { contentFunc = append( contentFunc, contents.EnableFileMode(r), sizeEnabler.EnableSize(sizeUint, r), ) - if !context.Bool("O") { + if !cmd.Bool("O") { contentFunc = append(contentFunc, ownerEnabler.EnableOwner(r)) } - if !context.Bool("G") { + if !cmd.Bool("G") { contentFunc = append(contentFunc, groupEnabler.EnableGroup(r)) } for _, s := range timeType { @@ -766,11 +767,11 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "all", - Aliases: []string{"la"}, - Usage: "show all info/use a long listing format", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "all", + Aliases: []string{"la"}, + Usage: "show all info/use a long listing format", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { // remove filter.RemoveHidden newFF := make([]*filter.ItemFilterFunc, 0, len(itemFilterFunc)) @@ -783,10 +784,10 @@ var viewFlag = []cli.Flag{ contentFunc = append( contentFunc, contents.EnableFileMode(r), sizeEnabler.EnableSize(sizeUint, r), ) - if !context.Bool("O") { + if !cmd.Bool("O") { contentFunc = append(contentFunc, ownerEnabler.EnableOwner(r)) } - if !context.Bool("G") { + if !cmd.Bool("G") { contentFunc = append(contentFunc, groupEnabler.EnableGroup(r)) } for _, s := range timeType { @@ -801,11 +802,11 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "no-total-size", - Usage: "disable total size(always override --total-size)", - DisableDefaultText: true, - Category: "VIEW", - Action: func(context *cli.Context, b bool) error { + Name: "no-total-size", + Usage: "disable total size(always override --total-size)", + HideDefault: true, + Category: "VIEW", + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { sizeEnabler.DisableTotal() } @@ -813,11 +814,11 @@ var viewFlag = []cli.Flag{ }, }, &cli.BoolFlag{ - Name: "rt", - Aliases: []string{"relative-time"}, - Usage: "show relative time", - DisableDefaultText: true, - Action: func(context *cli.Context, b bool) error { + Name: "rt", + Aliases: []string{"relative-time"}, + Usage: "show relative time", + HideDefault: true, + Action: func(c context.Context, cmd *cli.Command, b bool) error { if b { for _, s := range timeType { rt := contents.NewRelativeTimeEnabler() @@ -830,20 +831,20 @@ var viewFlag = []cli.Flag{ Category: "VIEW", }, &cli.BoolFlag{ - Name: "stdin", - Usage: "read path from stdin, split by newline", - DisableDefaultText: true, - Category: "VIEW", + Name: "stdin", + Usage: "read path from stdin, split by newline", + HideDefault: true, + Category: "VIEW", }, &cli.BoolFlag{ - Name: "flags", - Usage: "list file flags[macOS only]", - DisableDefaultText: true, - Category: "VIEW", + Name: "flags", + Usage: "list file flags[macOS only]", + HideDefault: true, + Category: "VIEW", }, } -func setLimit(context *cli.Context) error { +func setLimit(context *cli.Command) error { size := context.String("detect-size") var bytes uint64 = 1024 * 1024 if size == "0" || strings.EqualFold(size, "infinity") || strings.EqualFold(size, "nolimit") { diff --git a/main.go b/main.go index 388a56d..e22132d 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "errors" "fmt" "os" @@ -14,7 +15,7 @@ import ( "github.com/Equationzhao/g/internal/global/doc" "github.com/Equationzhao/g/internal/util" "github.com/Equationzhao/g/man" - ucli "github.com/urfave/cli/v2" + ucli "github.com/urfave/cli/v3" ) func main() { @@ -29,7 +30,7 @@ func main() { man.GenMan() } else { preprocessArgs() - err := cli.G.Run(os.Args) + err := cli.G.Run(context.Background(), os.Args) if err != nil { if !errors.Is(err, cli.Err4Exit{}) { if cli.ReturnCode == 0 { diff --git a/main_test.go b/main_test.go index 29d4b2f..37f74dd 100644 --- a/main_test.go +++ b/main_test.go @@ -10,7 +10,7 @@ import ( "github.com/Equationzhao/g/internal/config" "github.com/agiledragon/gomonkey/v2" "github.com/stretchr/testify/assert" - ucli "github.com/urfave/cli/v2" + ucli "github.com/urfave/cli/v3" ) func Test_catchPanic(t *testing.T) { diff --git a/man/man.go b/man/man.go index b0e4f72..c630a4b 100644 --- a/man/man.go +++ b/man/man.go @@ -1,20 +1,5 @@ package man -import ( - "compress/gzip" - "os" - "path/filepath" - - "github.com/Equationzhao/g/internal/cli" -) - func GenMan() { // man - man, _ := os.Create(filepath.Join("man", "g.1.gz")) - s, _ := cli.G.ToMan() - // compress to gzip - manGz := gzip.NewWriter(man) - defer manGz.Close() - _, _ = manGz.Write([]byte(s)) - _ = manGz.Flush() }