Skip to content

Commit

Permalink
Add input flag
Browse files Browse the repository at this point in the history
  • Loading branch information
dogancelik committed Sep 1, 2017
1 parent 6dad2ed commit 5defbf8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func cliSelect() string {
}
defer keyboard.Close()

fmt.Println(genSelectText())
fmt.Println(genSelectText(false))
ret := ""

for {
Expand Down Expand Up @@ -75,6 +75,16 @@ func cliSearch(c *cli.Context) error {
}
}

// Input flag
if c.Bool("input") {
fmt.Print(genSelectText(true))
fmt.Scanln(&targets)
targets = initialsToTargets(targets)
if targets == "" {
return cli.NewExitError("No target is specified", 5)
}
}

// Upload
if isUrl(srcPath) == true {
debug("Start URL upload")
Expand Down Expand Up @@ -135,6 +145,10 @@ func main() {
Name: "select, s",
Usage: "Show a list of targets to select from",
},
cli.BoolFlag{
Name: "input, i",
Usage: "Type the targets you want to open",
},
cli.BoolFlag{
Name: "return, r",
Usage: "Output the result URL",
Expand Down
37 changes: 34 additions & 3 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,43 @@ func getQueryList(s string) []string {

// Create a nice select list

func genSelectText() string {
text := "Select a target:\n\n"
func genSelectText(inputMode bool) string {
text := ""

if inputMode {
text += "Available targets:\n\n"
} else {
text += "Select a target:\n\n"
}

text += " > [i]mgops\n"
for _, target := range availableTargets {
text += " > " + strings.Replace(target.Name, string(target.Key), "["+string(target.Key)+"]", 1) + "\n"
}
text += "\n(Press ESC to cancel)"

if inputMode {
text += "\nType initials: "
} else {
text += "\n(Press ESC to cancel)"
}

return text
}

// Initials to targets with comma

func initialsToTargets(initials string) string {
targets := ""
m := getKeyToNameTargets(availableTargets)

for i := range initials {
if val, ok := m[rune(initials[i])]; ok {
targets += val
if len(initials)-1 != i {
targets += ","
}
}
}

return targets
}

0 comments on commit 5defbf8

Please sign in to comment.