-
Notifications
You must be signed in to change notification settings - Fork 1
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
13 changed files
with
350 additions
and
191 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 |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/dmirubtsov/mcli/pkg/prompt" | ||
"github.com/dmirubtsov/mcli/pkg/shortcuts" | ||
"github.com/dmirubtsov/mcli/pkg/subprocess" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
func run(*cli.Context) error { | ||
if len(cfg.Shortcuts) == 0 { | ||
err := cfg.WriteDemo() | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
index, err := prompt.SelectionPrompt(cfg.Shortcuts, cfg.PromptSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return subprocess.Exec(cfg.Shortcuts[index].Cmd) | ||
} | ||
|
||
func add(*cli.Context) error { | ||
nameField, err := prompt.InputPromptString(nameFieldText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
commandField, err := prompt.InputPromptString(commandFieldText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cfg.Shortcuts.Add(shortcuts.Shortcut{Name: nameField, Cmd: commandField}) | ||
return cfg.Write() | ||
} | ||
|
||
func edit(*cli.Context) error { | ||
index, err := prompt.SelectionPrompt(cfg.Shortcuts, cfg.PromptSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
nameField, err := prompt.InputPromptString(nameFieldText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
commandField, err := prompt.InputPromptString(commandFieldText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cfg.Shortcuts.Delete(shortcuts.Shortcut{Index: index}) | ||
cfg.Shortcuts.Add(shortcuts.Shortcut{Name: nameField, Cmd: commandField}) | ||
return cfg.Write() | ||
} | ||
|
||
func delete(*cli.Context) error { | ||
index, err := prompt.SelectionPrompt(cfg.Shortcuts, cfg.PromptSize) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cfg.Shortcuts.Delete(shortcuts.Shortcut{Index: index}) | ||
return cfg.Write() | ||
} | ||
|
||
func setPromptSize(*cli.Context) error { | ||
size, err := prompt.InputPromptInt(sizeFieldText) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = cfg.SetPromptSize(size) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cfg.Write() | ||
} |
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,13 @@ | ||
package main | ||
|
||
const ( | ||
nameFieldText = "Name" | ||
commandFieldText = "Command" | ||
sizeFieldText = "Size" | ||
addShortcutText = "Add shortcut" | ||
editShortcutText = "Edit shortcut" | ||
removeShortcutText = "Remove shortcut" | ||
setPromptSizeText = "Set prompt size" | ||
usageText = "Simple shortcut menu" | ||
defaultVersionText = "git" | ||
) |
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
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,31 @@ | ||
package prompt | ||
|
||
import ( | ||
"github.com/dmirubtsov/mcli/pkg/shortcuts" | ||
"github.com/erikgeiser/promptkit/selection" | ||
"strings" | ||
) | ||
|
||
func selectionFilter(filter string, c *selection.Choice[shortcuts.Shortcut]) bool { | ||
name := strings.ToLower(c.Value.Name) | ||
cmd := strings.ToLower(c.Value.Cmd) | ||
filter = strings.ToLower(filter) | ||
|
||
for _, in := range strings.Split(filter, " ") { | ||
match := false | ||
|
||
if strings.Contains(name, in) { | ||
name = strings.ReplaceAll(name, in, "") | ||
match = true | ||
} else if strings.Contains(cmd, in) { | ||
cmd = strings.ReplaceAll(cmd, in, "") | ||
match = true | ||
} | ||
|
||
if !match { | ||
return match | ||
} | ||
} | ||
|
||
return true | ||
} |
Oops, something went wrong.