-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,7 @@ | |
*.so | ||
*.dylib | ||
.history | ||
|
||
commandlinefu | ||
# ides | ||
.vscode | ||
.idea | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |