diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6ea976e --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/HardDie/myTldr + +go 1.16 + +require github.com/fatih/color v1.10.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..9ee68f3 --- /dev/null +++ b/go.sum @@ -0,0 +1,9 @@ +github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= +github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= +github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= +github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae h1:/WDfKMnPU+m5M4xB+6x4kaepxRw6jWvR5iDRdvjHgy8= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/list.go b/list.go new file mode 100644 index 0000000..745af7d --- /dev/null +++ b/list.go @@ -0,0 +1,26 @@ +package main + +import ( + "log" + "os" + "path/filepath" + "sort" + "strings" +) + +func printList(source, language string) (commands []string) { + path := buildLocalPath(source, language) + err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error { + if info.IsDir() { + return nil + } + commands = append(commands, strings.ReplaceAll(info.Name(), ".md", "")) + return nil + }) + if err != nil { + log.Fatal(err.Error()) + os.Exit(1) + } + sort.Strings(commands) + return +} diff --git a/local.go b/local.go new file mode 100644 index 0000000..b80489d --- /dev/null +++ b/local.go @@ -0,0 +1,39 @@ +package main + +import ( + "io/ioutil" + "os" + "os/user" + "strings" +) + +const ( + FilesDefaultPath = ".my_scripts/.tldr" +) + +func getLocalPath() string { + user, err := user.Current() + if err != nil { + // Application can't continue + os.Exit(1) + } + return user.HomeDir + "/" + FilesDefaultPath +} + +func buildLocalPath(source, language string) string { + folder := "pages" + if language != "en" { + folder += "." + language + } + return source + "/" + folder +} + +func checkLocal(source, language string, name string) (page []string, err error) { + fileName := buildLocalPath(source, language) + "/" + name + ".md" + data, err := ioutil.ReadFile(fileName) + if err != nil { + return + } + page = strings.Split(string(data), "\n") + return +} diff --git a/main.go b/main.go new file mode 100644 index 0000000..1f81f2e --- /dev/null +++ b/main.go @@ -0,0 +1,62 @@ +package main + +import ( + "errors" + "flag" + "fmt" + "os" +) + +func main() { + source, language, err := handleFlags() + if err != nil { + return + } + + command := flag.Args()[0] + page, err := checkLocal(source, language, command) + if err == nil { + fmt.Println(output(page)) + return + } + + fmt.Printf("`%s` documentation is not available. Consider contributing Pull Request to https://github.com/tldr-pages/tldr\n", command) +} + +func handleFlags() (source, language string, err error) { + fVersion := flag.Bool("version", false, "show program's version number and exit") + fUpdateCache := flag.Bool("update_cache", false, "Update the local cache of pages and exit") + fPlatform := flag.String("platform", "linux", "Override the operating system [linux, osx, sunos, windows, common]") + fList := flag.Bool("list", false, "List all available commands for operating system") + fSource := flag.String("source", getLocalPath(), "Override the default page source") + fLanguage := flag.String("language", "en", "Override the default language") + flag.Usage = func() { + fmt.Printf("usage: %s [options] command\n\n", os.Args[0]) + fmt.Println("Go command line client for tldr\n") + fmt.Println("optional arguments:") + flag.PrintDefaults() + } + flag.Parse() + + _ = fUpdateCache + _ = fPlatform + _ = fList + + switch { + case *fVersion: + fmt.Println(getVersion()) + os.Exit(0) + case *fList: + fmt.Printf("%q\n", printList(*fSource, *fLanguage)) + os.Exit(0) + } + + source = *fSource + language = *fLanguage + if len(flag.Args()) != 1 { + flag.Usage() + err = errors.New("not enough arguments") + return + } + return +} diff --git a/output.go b/output.go new file mode 100644 index 0000000..83e89f2 --- /dev/null +++ b/output.go @@ -0,0 +1,58 @@ +package main + +import ( + "regexp" + "strings" + + "github.com/fatih/color" +) + +const ( + LeadingSpaceNum = 2 +) + +var ( + WhiteBoldString = color.New(color.FgWhite, color.Bold).SprintFunc() + WhiteString = color.WhiteString + GreenString = color.GreenString + RedString = color.RedString + + LeadingSpace = strings.Repeat(" ", LeadingSpaceNum) +) + +func output(page []string) (rendered string) { + re1 := regexp.MustCompile("{{") + re2 := regexp.MustCompile("}}") + + rendered += "\n" + for _, line := range page { + switch { + case len(line) == 0: + continue + case line[0] == '#': + rendered += LeadingSpace + WhiteBoldString(strings.ReplaceAll(line, "#", "")) + "\n\n" + case line[0] == '>': + rendered += LeadingSpace + WhiteString(strings.ReplaceAll(strings.ReplaceAll(line, ">", ""), "<", "")) + "\n" + case line[0] == '-': + rendered += "\n" + LeadingSpace + GreenString(line) + "\n" + case line[0] == '`': + line = line[1 : len(line)-1] + res := re1.ReplaceAllString(line, "\n{{") + res = re2.ReplaceAllString(res, "}}\n") + rendered += LeadingSpace + LeadingSpace + for _, item := range strings.Split(res, "\n") { + if len(item) == 0 { + rendered += " " + } else if item[0] == '{' { + // If argument, print without color + rendered += item[2 : len(item)-2] + } else { + rendered += RedString(item) + } + } + rendered += "\n" + } + } + rendered += "\n" + return +} diff --git a/version.go b/version.go new file mode 100644 index 0000000..0ca28aa --- /dev/null +++ b/version.go @@ -0,0 +1,9 @@ +package main + +var ( + VERSION = "0.1" +) + +func getVersion() string { + return "myTldr " + VERSION +}