Skip to content

Commit

Permalink
removed the subcommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Sep 12, 2023
1 parent e455fba commit 72e36ee
Show file tree
Hide file tree
Showing 9 changed files with 168 additions and 171 deletions.
18 changes: 12 additions & 6 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,19 @@ var (
ErrUsage = errors.New("command usage could not display")
)

const (
IDcodepage = "idcp"
IDfile = "idfile"
IDsample = "idsample"
)

// Cmd represents the base command when called without any subcommands.
var Cmd = base()

func base() *cobra.Command {
s := fmt.Sprintf("Use %s to print plain, BBS era and ANSI textfiles", meta.Name)
l := meta.Name + " takes legacy codepage and ANSI encoded textfiles and\n" +
"prints them to a modern Unicode terminal."
s := "Use " + meta.Name + " to print legacy text on modern terminals."
l := "Text files and art created without Unicode often fail to display on modern systems. " +
"\n" + s
expl := strings.Builder{}
example.Cmd.String(&expl)
return &cobra.Command{
Expand Down Expand Up @@ -68,9 +74,9 @@ func Execute() error {

func Init() {
Cmd = Tester(Cmd)
Cmd.AddGroup(&cobra.Group{ID: "listCmds", Title: "Codepages:"})
Cmd.AddGroup(&cobra.Group{ID: "fileCmds", Title: "Files:"})
Cmd.AddGroup(&cobra.Group{ID: "exaCmds", Title: "Testers:"})
Cmd.AddGroup(&cobra.Group{ID: IDcodepage, Title: "Codepage:"})
Cmd.AddGroup(&cobra.Group{ID: IDfile, Title: "File:"})
Cmd.AddGroup(&cobra.Group{ID: IDsample, Title: "Sample:"})
// create a version flag that only works on root.
Cmd.LocalNonPersistentFlags().BoolP("version", "v", false, "")
// hide the cobra introduced help command.
Expand Down
4 changes: 2 additions & 2 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func (t cmdT) tester(args []string) ([]byte, error) {
switch t {
case infoT:
c = cmd.InfoInit()
case listT:
c = cmd.ListInit()
// case listT:
// c = cmd.ListInit()
case viewT:
c = cmd.ViewInit()
default:
Expand Down
35 changes: 35 additions & 0 deletions cmd/example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"
"strings"

"github.com/bengarrett/retrotxtgo/cmd/pkg/example"
"github.com/bengarrett/retrotxtgo/cmd/pkg/list"
"github.com/bengarrett/retrotxtgo/pkg/term"
"github.com/spf13/cobra"
)

func ListExample() *cobra.Command {
s := fmt.Sprintf("List builtin tester 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.",
term.Example("info"), term.Example("view"))
expl := strings.Builder{}
example.ListExamples.String(&expl)
return &cobra.Command{
Use: "example",
Aliases: []string{"e", "sample"},
GroupID: IDsample,
Short: s,
Long: l,
Example: expl.String(),
RunE: func(cmd *cobra.Command, args []string) error {
return list.Examples(cmd.OutOrStdout())
},
}
}

func init() {
Cmd.AddCommand(ListExample())
}
12 changes: 8 additions & 4 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ import (
)

func InfoCommand() *cobra.Command {
s := "Information on a text file"
l := "Discover details and information about any text or text art file."
expl := strings.Builder{}
example.Info.String(&expl)
return &cobra.Command{
Use: fmt.Sprintf("info %s", example.Filenames),
Aliases: []string{"i"},
GroupID: "fileCmds",
Short: "Information on a text file",
Long: "Discover details and information about any text or text art file.",
Example: fmt.Sprint(example.Info),
GroupID: IDfile,
Short: s,
Long: l,
Example: expl.String(),
RunE: func(cmd *cobra.Command, args []string) error {
return info.Run(cmd.OutOrStdout(), cmd, args...)
},
Expand Down
40 changes: 11 additions & 29 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,27 @@ package cmd
import (
"fmt"

"github.com/bengarrett/retrotxtgo/cmd/internal/flag"
"github.com/bengarrett/retrotxtgo/cmd/pkg/example"
"github.com/bengarrett/retrotxtgo/pkg/logs"
"github.com/bengarrett/retrotxtgo/meta"
"github.com/bengarrett/retrotxtgo/pkg/table"
"github.com/spf13/cobra"
)

func ListCommand() *cobra.Command {
func ListCodepage() *cobra.Command {
s := fmt.Sprintf("List the legacy codepages that %s can convert to UTF-8", meta.Name)
l := fmt.Sprintf("List the available legacy codepages that %s can convert to UTF-8.", meta.Name)
return &cobra.Command{
Use: "list",
Aliases: []string{"l"},
GroupID: "listCmds",
Short: "Available codepages and tabled datasets",
Long: "List the available codepages and their tabled values.",
Example: fmt.Sprint(example.List),
Aliases: []string{"l", "cp", "codepage"},
Short: s,
Long: l,
GroupID: IDcodepage,
RunE: func(cmd *cobra.Command, args []string) error {
if err := flag.Help(cmd, args...); err != nil {
return err
}
if len(args) > 0 {
logs.FatalSubCmd("list", args...)
// TODO replace with a print and error return
}
return nil
return table.List(cmd.OutOrStdout())
},
}
}

func ListInit() *cobra.Command {
lc := ListCommand()
lc.AddGroup(&cobra.Group{ID: "codepages", Title: "Codepages:"})
lc.AddGroup(&cobra.Group{ID: "tables", Title: "Codepage Table:"})
lc.AddCommand(ListCodepages())
lc.AddCommand(ListTable())
lc.AddCommand(ListTables())
return lc
}

//nolint:gochecknoinits
func init() {
Cmd.AddCommand(ListInit())
Cmd.AddCommand(ListExamples())
Cmd.AddCommand(ListCodepage())
}
100 changes: 0 additions & 100 deletions cmd/lists.go

This file was deleted.

69 changes: 43 additions & 26 deletions cmd/pkg/example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,26 @@ func (e Example) String(w io.Writer) {
// the last hash #, which is treated as a comment
const cmmt, sentence = "#", 2
scanner := bufio.NewScanner(b)
cnt := 0
rows := len(strings.Split(b.String(), "\n"))
for scanner.Scan() {
cnt++
s := strings.Split(scanner.Text(), cmmt)
l := len(s)
if l < sentence {
fmt.Fprintln(w, term.Info(scanner.Text()))
fmt.Fprint(w, term.Info(scanner.Text()))
if cnt < rows {
fmt.Fprintln(w)
}
continue
}
// do not the last hash as a comment
ex := strings.Join(s[:l-1], cmmt)
fmt.Fprint(w, term.Info(ex))
fmt.Fprintf(w, "%s%s\n ", color.Secondary.Sprint(cmmt), s[l-1])
fmt.Fprintf(w, "%s%s", color.Secondary.Sprint(cmmt), s[l-1])
if cnt < rows {
fmt.Fprintln(w)
}
}
}

Expand All @@ -80,43 +89,51 @@ func (e Example) result() string {
}

func cmd() string {
return fmt.Sprintf(" %s\n%s\n%s",
fmt.Sprintf("%s info %s", meta.Bin, Filenames),
fmt.Sprintf("%s view %s", meta.Bin, Filenames),
fmt.Sprintf("%s %s # print text files partial info TODO", meta.Bin, Filenames),
)
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)
return s.String()
}

func list() string {
return fmt.Sprintf(" %s\n%s\n%s",
fmt.Sprintf("%s list codepages", meta.Bin),
fmt.Sprintf("%s list table cp437 cp1252", meta.Bin),
fmt.Sprintf("%s list tables", meta.Bin))
s := &strings.Builder{}
fmt.Fprintf(s, " %s list codepages\n", meta.Bin)
fmt.Fprintf(s, " %s list table cp437 cp1252\n", meta.Bin)
fmt.Fprintf(s, " %s list tables", meta.Bin)
return s.String()
}

func listExamples() string {
return fmt.Sprintf(" %s\n%s\n%s",
fmt.Sprintf("%s list examples # list the builtin examples", meta.Bin),
fmt.Sprintf("%s info ascii # information on the buildin ascii example", meta.Bin),
fmt.Sprintf("%s view ascii # view the ascii example", meta.Bin))
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)
return s.String()
}

func listTable() string {
return fmt.Sprintf(" %s\n%s\n%s",
fmt.Sprintf("%s table cp437", meta.Bin),
fmt.Sprintf("%s table cp437 latin1 windows-1252", meta.Bin),
fmt.Sprintf("%s table iso-8859-15", meta.Bin))
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)
return s.String()
}

func info() string {
return fmt.Sprintf(" %s %s\n%s %s",
meta.Bin, "info text.asc logo.jpg # print the information of multiple files",
meta.Bin, "info file.txt --format=json # print the information using a structured syntax")
s := &strings.Builder{}
fmt.Fprintf(s, " %s info text.asc logo.jpg # print the information of multiple files\n", meta.Bin)
fmt.Fprintf(s, " %s info file.txt --format=json # print the information using a structured syntax\n", meta.Bin)
return s.String()
}

func view() string {
return fmt.Sprintf(" %s\n%s\n%s",
fmt.Sprintf("%s view file.txt -e latin1", meta.Bin),
fmt.Sprintf("%s view file1.txt file2.txt --encode=\"iso-8859-1\"", meta.Bin),
fmt.Sprintf("cat file.txt | %s view", meta.Bin))
s := &strings.Builder{}
fmt.Fprintf(s, " %s view file.txt -e latin1\n", meta.Bin)
fmt.Fprintf(s, " %s view file1.txt file2.txt --encode=\"iso-8859-1\"\n", meta.Bin)
fmt.Fprintf(s, " cat file.txt | %s view", meta.Bin)
return s.String()
}
Loading

0 comments on commit 72e36ee

Please sign in to comment.