Skip to content

Commit

Permalink
upgrade promptkit library, generics
Browse files Browse the repository at this point in the history
  • Loading branch information
mazzz1y committed Dec 10, 2022
1 parent 576ba32 commit a0ee884
Show file tree
Hide file tree
Showing 13 changed files with 350 additions and 191 deletions.
84 changes: 84 additions & 0 deletions cmd/mcli/cmd.go
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()
}
94 changes: 17 additions & 77 deletions cmd/mcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,111 +5,51 @@ import (
"log"
"os"

"github.com/dmirubtsov/mcli/pkg/prompt"
"github.com/dmirubtsov/mcli/pkg/shortcuts"
"github.com/dmirubtsov/mcli/pkg/subprocess"

"github.com/dmirubtsov/mcli/pkg/config"
"github.com/erikgeiser/promptkit"
"github.com/urfave/cli/v2"
)

var version = "git"
var version = defaultVersionText
var cfg = config.Config{}

func main() {
var config = config.Config{}
if err := config.Read(); err != nil {
func init() {
cfg = config.Config{}
if err := cfg.Read(); err != nil {
log.Fatal(err)
}
}

func main() {
app := &cli.App{
Name: "mcli",
Version: version,
Usage: "Simple shortcut menu",
Action: func(c *cli.Context) error {
index, err := prompt.SelectionPrompt(config.Shortcuts, config.PromptSize)
if err != nil {
return err
}

return subprocess.Exec(config.Shortcuts[index].Cmd)
},
Usage: usageText,
Action: run,
Commands: []*cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add shortcut",
Action: func(c *cli.Context) error {
nameField, err := prompt.InputPrompt("Name")
if err != nil {
return err
}

commandField, err := prompt.InputPrompt("Command")
if err != nil {
return err
}

config.Shortcuts.Add(shortcuts.Shortcut{Name: nameField, Cmd: commandField})
return config.Write()
},
Usage: addShortcutText,
Action: add,
},
{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edit shortcut",
Action: func(c *cli.Context) error {
index, err := prompt.SelectionPrompt(config.Shortcuts, config.PromptSize)
if err != nil {
return err
}

nameField, err := prompt.InputPrompt("Name")
if err != nil {
return err
}

commandField, err := prompt.InputPrompt("Command")
if err != nil {
return err
}

config.Shortcuts.Delete(shortcuts.Shortcut{Index: index})
config.Shortcuts.Add(shortcuts.Shortcut{Name: nameField, Cmd: commandField})
return config.Write()
},
Usage: editShortcutText,
Action: edit,
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Remove shortcut",
Action: func(c *cli.Context) error {
index, err := prompt.SelectionPrompt(config.Shortcuts, config.PromptSize)
if err != nil {
return err
}

config.Shortcuts.Delete(shortcuts.Shortcut{Index: index})
return config.Write()
},
Usage: removeShortcutText,
Action: delete,
},
{
Name: "prompt-size",
Aliases: []string{"p"},
Usage: "Set prompt size",
Action: func(c *cli.Context) error {
size, err := prompt.InputPrompt("Size")
if err != nil {
return err
}

err = config.SetPromptSize(size)
if err != nil {
return err
}

return config.Write()
},
Usage: setPromptSizeText,
Action: setPromptSize,
},
},
}
Expand Down
13 changes: 13 additions & 0 deletions cmd/mcli/strings.go
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"
)
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/dmirubtsov/mcli
go 1.19

require (
github.com/charmbracelet/lipgloss v0.5.0
github.com/muesli/termenv v0.12.0
github.com/riywo/loginshell v0.0.0-20200815045211-7d26008be1ab
github.com/stretchr/testify v1.8.0
Expand All @@ -12,14 +11,15 @@ require (

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/charmbracelet/bubbles v0.11.0 // indirect
github.com/charmbracelet/bubbletea v0.21.0 // indirect
github.com/charmbracelet/bubbles v0.13.0 // indirect
github.com/charmbracelet/bubbletea v0.22.0 // indirect
github.com/charmbracelet/lipgloss v0.5.0 // indirect
github.com/containerd/console v1.0.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 // indirect
github.com/muesli/cancelreader v0.2.0 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 // indirect
Expand All @@ -28,10 +28,10 @@ require (
require (
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/erikgeiser/promptkit v0.7.0
github.com/erikgeiser/promptkit v0.7.1-0.20220721185625-1f33bc73d091
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
20 changes: 11 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/charmbracelet/bubbles v0.11.0 h1:fBLyY0PvJnd56Vlu5L84JJH6f4axhgIJ9P3NET78f0Q=
github.com/charmbracelet/bubbles v0.11.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWoiNibae+1yCMtcc=
github.com/charmbracelet/bubbletea v0.21.0 h1:f3y+kanzgev5PA916qxmDybSHU3N804uOnKnhRPXTcI=
github.com/charmbracelet/bubbles v0.13.0 h1:zP/ROH3wJEBqZWKIsD50ZKKlx3ydLInq3LdD/Nrlb8w=
github.com/charmbracelet/bubbles v0.13.0/go.mod h1:bbeTiXwPww4M031aGi8UK2HT9RDWoiNibae+1yCMtcc=
github.com/charmbracelet/bubbletea v0.21.0/go.mod h1:GgmJMec61d08zXsOhqRC/AiOx4K4pmz+VIcRIm1FKr4=
github.com/charmbracelet/bubbletea v0.22.0 h1:E1BTNSE3iIrq0G0X6TjGAmrQ32cGCbFDPcIuImikrUc=
github.com/charmbracelet/bubbletea v0.22.0/go.mod h1:aoVIwlNlr5wbCB26KhxfrqAn0bMp4YpJcoOelbxApjs=
github.com/charmbracelet/harmonica v0.2.0/go.mod h1:KSri/1RMQOZLbw7AHqgcBycp8pgJnQMYYT8QZRqZ1Ao=
github.com/charmbracelet/lipgloss v0.5.0 h1:lulQHuVeodSgDez+3rGiuxlPVXSnhth442DATR2/8t8=
github.com/charmbracelet/lipgloss v0.5.0/go.mod h1:EZLha/HbzEt7cYqdFPovlqy5FZPj0xFhg5SaqxScmgs=
Expand All @@ -14,8 +15,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikgeiser/promptkit v0.7.0 h1:Yi28iN6JRs8/0x+wjQRPfWb+vWz1pFmZ5fu2uoFipD8=
github.com/erikgeiser/promptkit v0.7.0/go.mod h1:Jj9bhN+N8RbMjB1jthkr9A4ydmczZ1WZJ8xTXnP12dg=
github.com/erikgeiser/promptkit v0.7.1-0.20220721185625-1f33bc73d091 h1:A6KY23PMnntvafsBxa3NB4X0TA4fNUNLVWnpq2goQxo=
github.com/erikgeiser/promptkit v0.7.1-0.20220721185625-1f33bc73d091/go.mod h1:MWvZWFLFfUTG24wr9Y6BGCcyGE/lBP3RsMC+sjWQl1c=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
Expand All @@ -28,8 +29,10 @@ github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70 h1:kMlmsLSbjkikxQJ1IPwaM+7LJ9ltFu/fi8CRzvSnQmA=
github.com/muesli/ansi v0.0.0-20211031195517-c9f0611b6c70/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/cancelreader v0.2.0 h1:SOpr+CfyVNce341kKqvbhhzQhBPyJRXQaCtn03Pae1Q=
github.com/muesli/cancelreader v0.2.0/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/cancelreader v0.2.1/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/reflow v0.2.1-0.20210115123740-9e1d0d53df68/go.mod h1:Xk+z4oIWdQqJzsxyjgl3P22oYZnHdZ8FFTHAQQt5BMQ=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
Expand Down Expand Up @@ -61,9 +64,8 @@ golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d h1:/m5NbqQelATgoSPVC2Z23sR4kVNokFwDDyWh/3rGY+I=
golang.org/x/sys v0.0.0-20220708085239-5a0f0661e09d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467 h1:CBpWXWQpIRjzmkkA+M7q9Fqnwd2mZr3AFqexg8YTfoM=
golang.org/x/term v0.0.0-20220526004731-065cf7ba2467/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
30 changes: 29 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ func (c *Config) Read() error {
return c.Write()
}

return json.Unmarshal(cBytes, &c)
err = json.Unmarshal(cBytes, &c)
if err != nil {
return err
}

for i := range c.Shortcuts {
c.Shortcuts[i].Index = i
}

return nil
}

func (c *Config) Write() error {
Expand All @@ -87,3 +96,22 @@ func (c *Config) Write() error {

return os.WriteFile(configFilePath, file, filePerm)
}

func (c *Config) WriteDemo() error {
c.Shortcuts = []shortcuts.Shortcut{
{
Name: "Demo Command 1",
Cmd: "echo test1",
},
{
Name: "Demo Command 2",
Cmd: "echo test2",
},
{
Name: "Demo Command 3",
Cmd: "echo test3",
},
}

return c.Write()
}
31 changes: 31 additions & 0 deletions pkg/prompt/filters.go
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
}
Loading

0 comments on commit a0ee884

Please sign in to comment.