Skip to content

Commit

Permalink
better architecture implementation
Browse files Browse the repository at this point in the history
Former-commit-id: b8c41d7
  • Loading branch information
isacikgoz committed Dec 21, 2018
1 parent 8bd3aef commit be148b6
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 64 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
[![Build Status](https://travis-ci.com/isacikgoz/tldr.svg?branch=master)](https://travis-ci.com/isacikgoz/tldr) [![MIT License](https://img.shields.io/badge/license-MIT-brightgreen.svg)](/LICENSE)

# tldr++
community driven man pages improved
community driven man pages improved with smart user interaction. tldr++ seperates itself with convenient user guidance and accelerates command generating.

![screenplay](img/screenplay.gif)

## Features
- Interactive (see the screencast)
- Smart file suggestions (further suggestions will be added)
- Simple implementation
- One of the fastest clients can be benchmarked with static option
- Multi-Platform
- *Pure-go (even git itself)*

## Installation
Refer to [Release Page](https://github.com/isacikgoz/tldr/releases)

## Credits
- [tldr-pages](https://github.com/tldr-pages/tldr)
- [survey](https://github.com/AlecAivazis/survey)
- [go-prompt](https://github.com/c-bata/go-prompt)
- [go-git](https://github.com/src-d/go-git)
- [kingpin](https://github.com/alecthomas/kingpin)
2 changes: 1 addition & 1 deletion img/screenplay.gif.REMOVED.git-id
Original file line number Diff line number Diff line change
@@ -1 +1 @@
a8fa8f110c0416fe7eeea6c12687abfaf6cd8469
0d942db9be4b6c5ef781645c1231159385cc805a
42 changes: 18 additions & 24 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,20 @@ import (
)

var (
clear = kingpin.Flag("clear-cache", "clear local repository then clone github.com/tldr-pages/tldr").Short('c').Bool()
update = kingpin.Flag("update", "pulls the latest commits from github.com/tldr-pages/tldr").Short('u').Bool()
interactive = kingpin.Flag("interactive", "interactive mode.").Short('i').Default("true").Bool()
clear = kingpin.Flag("clear-cache", "clear local repository then clone github.com/tldr-pages/tldr").Short('c').Bool()
update = kingpin.Flag("update", "pulls the latest commits from github.com/tldr-pages/tldr").Short('u').Bool()
static = kingpin.Flag("static", "static mode.").Short('s').Default("false").Bool()

page = kingpin.Arg("command", "Name of the command.").String()
page = kingpin.Arg("command", "Name of the command.").Strings()
)

func main() {
// start := time.Now()
kingpin.Version("tldr++ version 0.0.1 (pre-release)")
// parse the command line flag and options

kingpin.Version("tldr++ version 0.1 (pre-release)")
kingpin.Parse()
config.StartUp()
if *clear {
err := config.Clear()
if err != nil {

}
return
}
if *update {
err := config.PullSource()
if err != nil {
config.StartUp(*clear, *update)

}
return
}
if len(*page) == 0 {
kingpin.Usage()
return
Expand All @@ -48,23 +35,30 @@ func main() {
}

prompter := prompt.New(p)

if err = prompter.RenderPage(); err != nil {
fmt.Printf("%s\n", err.Error())
if err = prompter.RenderPage(*static); err != nil {
fmt.Printf("%s", err.Error())
return
}

t, err := prompter.Selection()
if err != nil {
fmt.Printf("%s\n", err.Error())
fmt.Printf("%s", err.Error())
return
}

if t == nil {
return
}

cmd, err := prompter.GenerateCommand(t)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}

if err = prompter.Run(cmd); err != nil {
fmt.Printf("%s\n", err.Error())
return
}

}
29 changes: 23 additions & 6 deletions pkg/config/configurer.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package config

import (
"fmt"
"os"
"runtime"

log "github.com/sirupsen/logrus"
)

func StartUp() error {
// StartUp
func StartUp(clear, update bool) error {
ok, _ := exists(SourceDir)
if !ok {
return Clear()
if !ok || staled() {
if err := Clear(); err != nil {
return err
}
}
if clear {
err := Clear()
if err != nil {

}
os.Exit(0)
} else if update {
err := PullSource()
if err != nil {

}
os.Exit(0)
}
return nil
}
Expand All @@ -27,11 +42,13 @@ func OSName() (n string) {
case "solaris":
n = "sunos"
default:
log.Warn("Operating system couldn't be recognized")
fmt.Println("Operating system couldn't be recognized")
os.Exit(1)
}
return n
}

// exists checks if the file exists
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
Expand Down
13 changes: 13 additions & 0 deletions pkg/config/source.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"fmt"
"os"
"time"

"gopkg.in/src-d/go-git.v4"
)
Expand Down Expand Up @@ -71,3 +72,15 @@ func DataDir() (d string) {
}
return d
}

func staled() bool {
file, _ := os.Open(SourceDir)
fstat, _ := file.Stat()
// now := time.Now()

diff := time.Now().Sub(fstat.ModTime())
if diff > 24*7*2*time.Hour {
return true
}
return false
}
4 changes: 3 additions & 1 deletion pkg/pages/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"strings"
)

// Command is the representation of a tip's command suggestion
type Command struct {
Command string
Args []string
Expand All @@ -14,12 +15,13 @@ func (c *Command) String() string {
return s
}

// Display returns colored and indented text for rendering output
func (c *Command) Display() string {
s := c.Command
for _, arg := range c.Args {
t := arg[2:]
t = t[:len(t)-2]
s = strings.Replace(s, arg, cyan.Sprint(t), 1)
}
return " " + s + "\n"
return " " + s + "\n"
}
7 changes: 6 additions & 1 deletion pkg/pages/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import (
)

var (
// used for matching to args in a command
rarg = regexp.MustCompile(`{{.[^}}]+}}`)
)

// Page is the representation of a tldr page itself
type Page struct {
Name string
Desc string
Tips []*Tip
}

// Parse page from bare markdown string. Rather than parsing markdown itself
// initial implementation approach is stripping from a single string
func ParsePage(s string) *Page {
l := strings.Split(s, "\n")

Expand Down Expand Up @@ -69,7 +73,8 @@ func (p *Page) String() string {
return s
}

// Display returns colored and indented text for rendering output
func (p *Page) Display() string {
s := bold.Sprint(p.Name) + "\n\n" + p.Desc + "\n"
s := bold.Sprint(p.Name) + "\n\n" + p.Desc
return s
}
19 changes: 17 additions & 2 deletions pkg/pages/pages.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

var (
sep = string(os.PathSeparator)
// a page should have md file extension
ext = ".md"

bold = color.New(color.Bold)
Expand All @@ -20,18 +21,31 @@ var (
white = color.New(color.FgWhite)
)

func Read(page string) (p *Page, err error) {
// Read finds and creates the Page, if it does not find, simply returns abstract
// contribution guide
func Read(seq []string) (p *Page, err error) {
page := ""
for i, l := range seq {
if len(seq)-1 == i {
page = page + l
break
} else {
page = page + l + "-"
}
}
// Common pages are more, so we have better luck there
p, err = queryCommon(page)
if err != nil {
p, err = queryOS(page)
if err != nil {
return p, errors.New("This page doesn't exist yet!\n" +
return p, errors.New("This page (" + page + ") doesn't exist yet!\n" +
"Submit new pages here: https://github.com/tldr-pages/tldr")
}
}
return p, nil
}

// Queries from common folder
func queryCommon(page string) (p *Page, err error) {
d := config.SourceDir + sep + "pages" + sep + "common" + sep
b, err := ioutil.ReadFile(d + page + ".md")
Expand All @@ -42,6 +56,7 @@ func queryCommon(page string) (p *Page, err error) {
return p, nil
}

// Queries from os specific folder
func queryOS(page string) (p *Page, err error) {
d := config.SourceDir + sep + "pages" + sep + config.OSName() + sep
b, err := ioutil.ReadFile(d + page + ".md")
Expand Down
4 changes: 3 additions & 1 deletion pkg/pages/tip.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pages

import ()

// Tip is the list item of a tldr page
type Tip struct {
Desc string
Cmd *Command
Expand All @@ -12,7 +13,8 @@ func (t *Tip) String() string {
return s
}

// Display returns colored and indented text for rendering output
func (t *Tip) Display() string {
s := "- " + blue.Sprint(t.Desc) + "\n" + t.Cmd.Display()
s := " " + blue.Sprint(t.Desc) + "\n" + t.Cmd.Display()
return s
}
Loading

0 comments on commit be148b6

Please sign in to comment.