Skip to content

Commit

Permalink
Work with local repository
Browse files Browse the repository at this point in the history
  • Loading branch information
HardDie committed Mar 4, 2021
1 parent e57b494 commit 7ad163b
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/HardDie/myTldr

go 1.16

require github.com/fatih/color v1.10.0
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
26 changes: 26 additions & 0 deletions list.go
Original file line number Diff line number Diff line change
@@ -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
}
39 changes: 39 additions & 0 deletions local.go
Original file line number Diff line number Diff line change
@@ -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
}
62 changes: 62 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -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
}
58 changes: 58 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

var (
VERSION = "0.1"
)

func getVersion() string {
return "myTldr " + VERSION
}

0 comments on commit 7ad163b

Please # to comment.