Skip to content

Commit

Permalink
add cli
Browse files Browse the repository at this point in the history
  • Loading branch information
pombadev committed Aug 8, 2021
1 parent e21ff55 commit 7d55334
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 98 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*.so
*.dylib
.history

commandlinefu
# ides
.vscode
.idea
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
VERSION := $(shell git describe --tags --abbrev=0)
REVISION := $(shell git rev-parse --short HEAD)
LDFLAGS := -X 'main.version=$(VERSION)' -X 'main.revision=$(REVISION)'
LDFLAGS := -X 'main.AppRevision=$(REVISION)' -X 'main.AppVersion=$(VERSION)'
GO ?= GO111MODULE=on go

.PHONY: build
Expand Down
125 changes: 125 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package main

import (
"flag"
"fmt"
"strings"

"github.com/c-bata/go-prompt"
)

func completer(d prompt.Document) []prompt.Suggest {
sliced := strings.Split(d.Text, " ")

var suggestions []prompt.Suggest

if len(sliced) > 1 {
cmd := sliced[0]
if cmd == "browse" {
suggestions = []prompt.Suggest{
{Text: "sort-by-votes", Description: "All-time Greats"},
{Text: "last-month", Description: "Hot this month"},
{Text: "last-week", Description: "Weekly"},
{Text: "last-day", Description: "Daily"},
{Text: "latest", Description: "Latest"},
}
}

} else {
suggestions = []prompt.Suggest{
{Text: "random", Description: "Random"},
{Text: "forthewicked", Description: "The Wicked"},
{Text: "browse", Description: "browse by many params"},
{Text: "match", Description: "match"},
{Text: "help", Description: "Help"},
{Text: "search", Description: "Search"},
}
}

return prompt.FilterFuzzy(suggestions, d.GetWordBeforeCursor(), true)
}

type Cli struct {
repl bool
query string
version bool
app App
}

func NewCli() Cli {
repl := flag.Bool("repl", true, fmt.Sprintf("Starts a %s repl", AppName))
query := flag.String("query", "", "A query")
version := flag.Bool("version", false, "Prints version information")

flag.Parse()

return Cli{repl: *repl, query: *query, version: *version, app: NewApp()}
}

func (c Cli) Version() {
fmt.Printf("v%s+%s\n", AppVersion, AppRevision)
}

func (c Cli) Repl() {
fmt.Printf("A cli and REPL for %s.com (v%s git+%s)\nPlease use `exit` or `Ctrl-D` to exit this program.\nType help to see all available commands and parameter\n", AppName, AppVersion, AppRevision)
repl := prompt.New(
func(input string) {
var (
cmd string
param string
)

sliced := strings.Split(input, " ")

if len(sliced) > 1 {
cmd = sliced[0]
param = strings.Join(sliced[1:], " ")
} else {
cmd = sliced[0]
}

switch cmd {
case "random":
run(func() error {
return c.app.random()
})
case "forthewicked":
run(func() error {
return c.app.wicked()
})
case "browse":
run(func() error {
return c.app.browse(param)
})
case "match":
run(func() error {
return c.app.matching(param)
})
case "search":
run(func() error {
return c.app.search(param)
})
case "help":
help("")

default:
help(input)
}
},
completer,
prompt.OptionTitle(AppName),
prompt.OptionPrefixTextColor(prompt.DarkGreen),
prompt.OptionInputTextColor(prompt.Green),
prompt.OptionSuggestionBGColor(prompt.Green),
prompt.OptionSuggestionTextColor(prompt.Black),
prompt.OptionSelectedSuggestionTextColor(prompt.White),
)

repl.Run()
}

func (c Cli) Search() {
run(func() error {
return c.app.search(c.query)
})
}
112 changes: 16 additions & 96 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,29 @@
package main

import (
"fmt"
"strings"

"github.com/c-bata/go-prompt"
"os"
)

func completer(d prompt.Document) []prompt.Suggest {
sliced := strings.Split(d.Text, " ")

var suggestions []prompt.Suggest

if len(sliced) > 1 {
cmd := sliced[0]
if cmd == "browse" {
suggestions = []prompt.Suggest{
{Text: "sort-by-votes", Description: "All-time Greats"},
{Text: "last-month", Description: "Hot this month"},
{Text: "last-week", Description: "Weekly"},
{Text: "last-day", Description: "Daily"},
{Text: "latest", Description: "Latest"},
}
}

} else {
suggestions = []prompt.Suggest{
{Text: "random", Description: "Random"},
{Text: "forthewicked", Description: "The Wicked"},
{Text: "browse", Description: "browse by many params"},
{Text: "match", Description: "match"},
{Text: "help", Description: "Help"},
{Text: "search", Description: "Search"},
}
}

return prompt.FilterFuzzy(suggestions, d.GetWordBeforeCursor(), true)
}

// go run -ldflags "-X 'main.version=1'" .
// go build -ldflags "-X 'main.version=1'"
var (
revision string
version string
AppName string = "commandlinefu"
AppRevision string
AppVersion string
AppName string = "commandlinefu"
)

func main() {
app := NewApp()
cli := NewCli()

fmt.Printf("A cli and REPL for %s.com (v%s git+%s)\nPlease use `exit` or `Ctrl-D` to exit this program.\nType help to see all available commands and parameter\n", AppName, version, revision)

repl := prompt.New(
func(input string) {
var (
cmd string
param string
)

sliced := strings.Split(input, " ")

if len(sliced) > 1 {
cmd = sliced[0]
param = strings.Join(sliced[1:], " ")
} else {
cmd = sliced[0]
}

switch cmd {
case "random":
run(func() error {
return app.random()
})
case "forthewicked":
run(func() error {
return app.wicked()
})
case "browse":
run(func() error {
return app.browse(param)
})
case "match":
run(func() error {
return app.matching(param)
})
case "search":
run(func() error {
return app.search(param)
})
case "help":
help("")

default:
help(input)
}
},
completer,
prompt.OptionTitle(AppName),
prompt.OptionPrefixTextColor(prompt.DarkGreen),
prompt.OptionInputTextColor(prompt.Green),
prompt.OptionSuggestionBGColor(prompt.Green),
prompt.OptionSuggestionTextColor(prompt.Black),
prompt.OptionSelectedSuggestionTextColor(prompt.White),
)
if cli.version {
cli.Version()
os.Exit(1)
}

repl.Run()
if len(cli.query) >= 1 {
cli.Search()
os.Exit(0)
}

if cli.repl {
cli.Repl()
}
}

0 comments on commit 7d55334

Please sign in to comment.