From 70f8957d4e55f203c8e38e8b7771c824bd4dcc15 Mon Sep 17 00:00:00 2001 From: Ben Garrett Date: Tue, 12 Sep 2023 21:48:37 +0800 Subject: [PATCH] help and layout improvements. --- cmd/example.go | 6 +++--- cmd/pkg/example/example.go | 20 ++++++++++++-------- cmd/pkg/list/list.go | 16 +++++++++++----- pkg/info/detail.go | 25 +++++++++++++------------ pkg/table/list.go | 8 ++++---- pkg/table/table.go | 2 +- 6 files changed, 44 insertions(+), 33 deletions(-) diff --git a/cmd/example.go b/cmd/example.go index a342533d..34d3c968 100644 --- a/cmd/example.go +++ b/cmd/example.go @@ -11,15 +11,15 @@ import ( ) func ListExample() *cobra.Command { - s := fmt.Sprintf("List builtin tester text files available for use with the %s and %s commands", + s := fmt.Sprintf("List the included sample text files available for use with the %s and %s commands", term.Example("info"), term.Example("view")) - l := fmt.Sprintf("List builtin tester text art and documents available for use with the %s and %s commands.", + l := fmt.Sprintf("List the included sample text art and documents available for use with the %s and %s commands.", term.Example("info"), term.Example("view")) expl := strings.Builder{} example.ListExamples.String(&expl) return &cobra.Command{ Use: "example", - Aliases: []string{"e", "sample"}, + Aliases: []string{"e", "sample", "s"}, GroupID: IDsample, Short: s, Long: l, diff --git a/cmd/pkg/example/example.go b/cmd/pkg/example/example.go index f55cfebb..fd4f0ee4 100644 --- a/cmd/pkg/example/example.go +++ b/cmd/pkg/example/example.go @@ -89,11 +89,13 @@ func (e Example) result() string { } func cmd() string { - const todo = " # print text files partial info TODO" s := &strings.Builder{} fmt.Fprintf(s, " %s info %s\n", meta.Bin, Filenames) fmt.Fprintf(s, " %s view %s\n", meta.Bin, Filenames) - fmt.Fprintf(s, " %s %s %s", meta.Bin, Filenames, todo) + fmt.Fprintf(s, " %s example\n\n", meta.Bin) + fmt.Fprintf(s, " %s list\n", meta.Bin) + fmt.Fprintf(s, " %s tables\n", meta.Bin) + fmt.Fprintf(s, " %s table [codepage names or aliases]", meta.Bin) return s.String() } @@ -107,11 +109,12 @@ func list() string { func listExamples() string { s := &strings.Builder{} - fmt.Fprintf(s, " %s examples # list the builtin examples\n", meta.Bin) - fmt.Fprintf(s, " %s info ascii # information on the buildin ascii example\n", meta.Bin) - fmt.Fprintf(s, " %s view ascii # view the ascii example\n", meta.Bin) - fmt.Fprintf(s, " %s info ansi.rgb # information on the 24-bit color ansi example\n", meta.Bin) - fmt.Fprintf(s, " %s view ansi.rgb # view the 24-bit color ansi example", meta.Bin) + fmt.Fprintf(s, " %s examples\t\t# list the builtin examples\n", meta.Bin) + fmt.Fprintf(s, " %s info ascii\t\t# information on the buildin ascii sample\n", meta.Bin) + fmt.Fprintf(s, " %s info ascii -f json\t# information in json format on the ascii sample\n", meta.Bin) + fmt.Fprintf(s, " %s view ascii\t\t# view the ascii example\n", meta.Bin) + fmt.Fprintf(s, " %s info ansi.rgb\t# information on the 24-bit color ansi example\n", meta.Bin) + fmt.Fprintf(s, " %s view ansi.rgb\t# view the 24-bit color ansi example", meta.Bin) return s.String() } @@ -119,7 +122,8 @@ func listTable() string { s := &strings.Builder{} fmt.Fprintf(s, " %s table cp437\n", meta.Bin) fmt.Fprintf(s, " %s table cp437 latin1 windows-1252\n", meta.Bin) - fmt.Fprintf(s, " %s table iso-8859-15", meta.Bin) + fmt.Fprintf(s, " %s table iso-8859-15\n", meta.Bin) + fmt.Fprintf(s, " %s list\t# list the supported codepage tables\n", meta.Bin) return s.String() } diff --git a/cmd/pkg/list/list.go b/cmd/pkg/list/list.go index 4c396b4b..6b0e511e 100644 --- a/cmd/pkg/list/list.go +++ b/cmd/pkg/list/list.go @@ -29,6 +29,7 @@ func Examples(wr io.Writer) error { if wr == nil { wr = io.Discard } + const width = 80 m := sample.Map() keys := make([]string, 0, len(m)) for k := range m { @@ -38,15 +39,20 @@ func Examples(wr io.Writer) error { const padding = 2 w := tabwriter.NewWriter(wr, 0, 0, padding, ' ', 0) bin := fmt.Sprintf(" %s ", meta.Bin) - if _, err := term.Head(w, 0, - fmt.Sprintf("Packaged example text and ANSI files to test and play with %s\n", meta.Name)); err != nil { + header := "Packaged example text and ANSI files to test and play with " + meta.Name + if _, err := term.Head(w, width, header); err != nil { return err } - for _, k := range keys { - fmt.Fprintf(w, "%s\t%s\t\n", k, m[k].Description) - } fmt.Fprintf(w, "\nAny of these packaged examples will work with the %s and %s commands.\n", term.Example("info"), term.Example("view")) + fmt.Fprintln(w) + for _, k := range keys { + fmt.Fprintf(w, " %s\t%s\t\n", k, m[k].Description) + } + fmt.Fprintln(w) + if _, err := term.Head(w, width, "Usage sample commands"); err != nil { + return err + } fmt.Fprintf(w, "\nPrint the Windows-1252 English test to the terminal.\n%s\n", term.Example(bin+"view 1252")) fmt.Fprintf(w, "\nConvert the Windows-1252 English test to UTF-8 encoding and save it to a file.\n%s\n", diff --git a/pkg/info/detail.go b/pkg/info/detail.go index b8ac363b..12945cdb 100644 --- a/pkg/info/detail.go +++ b/pkg/info/detail.go @@ -149,35 +149,35 @@ func (d *Detail) Marshal(w io.Writer, f Format) error { if w == nil { w = io.Discard } - var err error - var b []byte switch f { case ColorText: return d.marshal(w, true) case PlainText: return d.marshal(w, false) case JSON: - b, err = json.MarshalIndent(d, "", " ") + b, err := json.MarshalIndent(d, "", " ") if err != nil { return fmt.Errorf("detail json indent marshal: %w", err) } + _, err = w.Write(b) + return err case JSONMin: - b, err = json.Marshal(d) + b, err := json.Marshal(d) if err != nil { return fmt.Errorf("detail json marshal: %w", err) } + _, err = w.Write(b) + return err case XML: - b, err = xml.MarshalIndent(d, "", "\t") + b, err := xml.MarshalIndent(d, "", "\t") if err != nil { return fmt.Errorf("detail xml marshal: %w", err) } + _, err = w.Write(b) + return err default: return fmt.Errorf("detail marshal %q: %w", f, ErrFmt) } - if _, err := w.Write(b); err != nil { - return fmt.Errorf("detail marshal write: %w", err) - } - return nil } // MimeUnknown detects non-Standard legacy data. @@ -341,13 +341,14 @@ func (d *Detail) marshal(w io.Writer, color bool) error { return fmt.Sprintf("%s\t", s) } gookit.Enable = color + if _, err := term.Head(w, width, "File information"); err != nil { + return err + } + fmt.Fprintln(w) data := d.marshalled() l := len(fmt.Sprintf(" filename%s%s", strings.Repeat(" ", padding), data[0].v)) const tabWidth = 8 tw := tabwriter.NewWriter(w, 0, tabWidth, 0, '\t', 0) - if _, err := term.Head(tw, width, "File information"); err != nil { - return err - } for _, x := range data { if !d.validate(x) { continue diff --git a/pkg/table/list.go b/pkg/table/list.go index 5d020f63..c8fd424e 100644 --- a/pkg/table/list.go +++ b/pkg/table/list.go @@ -127,14 +127,14 @@ func List(wr io.Writer) error { //nolint:funlen term.Example(cmds), term.Comment("latin1"), term.Fuzzy("# alias")) fmt.Fprintf(w, "\n IBM Code Page 437 (%s) is commonly used on MS-DOS and ANSI art.\n", term.Comment("cp437")) - fmt.Fprintf(w, " ISO 8859-1 (%s) is found on legacy Unix, Amiga and the early Internet.\n", + fmt.Fprintf(w, " ISO 8859-1 (%s) is found on historic Unix, Amiga and the early Internet.\n", term.Comment("latin1")) - fmt.Fprintf(w, " Windows 1252 (%s) is found on legacy Windows 9x and earlier systems.\n", + fmt.Fprintf(w, " Windows 1252 (%s) is found on Windows ME/98 and earlier systems.\n", term.Comment("cp1252")) fmt.Fprintf(w, " Macintosh (%s) is found on Mac OS 9 and earlier systems.\n", term.Comment("macintosh")) - fmt.Fprintf(w, "\n%s, PCs and the web today use Unicode UTF-8. As a subset of ISO 8895-1,\n", meta.Name) - fmt.Fprintln(w, "UTF-8 is backwards compatible with it and US-ASCII.") + fmt.Fprintf(w, "\n%s, PCs and the web today use Unicode UTF-8. As a subset,\n", meta.Name) + fmt.Fprintln(w, "UTF-8 is backwards compatible with both ISO 8895-1 and US-ASCII.") return w.Flush() } diff --git a/pkg/table/table.go b/pkg/table/table.go index 4ea6f923..89303b26 100644 --- a/pkg/table/table.go +++ b/pkg/table/table.go @@ -31,7 +31,7 @@ var ( ) const ( - width = 67 + width = 68 ) // Table prints out all the characters in the named 8-bit character set.