Skip to content

Commit

Permalink
add promptui and remove the parameter from del
Browse files Browse the repository at this point in the history
  • Loading branch information
akymos committed Aug 16, 2022
1 parent 100b857 commit a4bc60f
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 50 deletions.
100 changes: 55 additions & 45 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"errors"
"fmt"
"github.com/akymos/ff/internal"
"github.com/chzyer/readline"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"strings"
)

type aliasList struct {
Expand All @@ -15,7 +18,7 @@ type aliasList struct {

var (
deleteCmd = &cobra.Command{
Use: "delete [alias]",
Use: "delete",
Aliases: []string{"del", "d", "-"},
Short: "Delete a directory alias.",
Long: `Delete a directory alias.
Expand All @@ -25,56 +28,63 @@ Arguments:
RunE: func(cmd *cobra.Command, args []string) error {
defer internal.BaseConfig.Db.Close()
alias := ""
if len(args) < 1 {
list := internal.FindAll()
if len(list) == 0 {
return errors.New("no aliases found")
}
aliasesList := make([]aliasList, 0)
for k, v := range list {
aliasesList = append(aliasesList, aliasList{Key: k, Val: v})
}
aliasesList = append(aliasesList, aliasList{Key: "Exit", Val: "No delete any alias"})
list := internal.FindAll()
if len(list) == 0 {
return errors.New("no aliases found")
}
aliasesList := make([]aliasList, 0)
for k, v := range list {
aliasesList = append(aliasesList, aliasList{Key: k, Val: v})
}

prompt := promptui.Select{
Label: "Select an alias to delete:",
Items: aliasesList,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "-> {{ .Key }} ({{ .Val }})",
Inactive: "{{ .Key }} ({{ .Val }})",
Selected: "-> {{ .Key }} ({{ .Val }})",
},
}
_, height, err := terminal.GetSize(0)
if err != nil {
return err
}

i, _, err := prompt.Run()
prompt := promptui.Select{
Label: "Select an alias to delete: (ctrl-c to exit)",
Items: aliasesList,
Size: height - 3,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}",
Active: "-> {{ .Key }} ({{ .Val }})",
Inactive: "{{ .Key }} ({{ .Val }})",
Selected: "-> {{ .Key }} ({{ .Val }})",
},
Searcher: func(input string, index int) bool {
alias := aliasesList[index]
name := fmt.Sprintf("%s %s", alias.Key, alias.Val)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

if err != nil {
return err
}
return strings.Contains(name, input)
},
Keys: &promptui.SelectKeys{
Prev: promptui.Key{Code: promptui.KeyPrev, Display: promptui.KeyPrevDisplay},
Next: promptui.Key{Code: promptui.KeyNext, Display: promptui.KeyNextDisplay},
PageUp: promptui.Key{Code: promptui.KeyBackward, Display: promptui.KeyBackwardDisplay},
PageDown: promptui.Key{Code: promptui.KeyForward, Display: promptui.KeyForwardDisplay},
Search: promptui.Key{Code: readline.CharTab, Display: "TAB"},
},
}

if i == len(aliasesList)-1 {
i, _, err := prompt.Run()

if err != nil {
if err == promptui.ErrInterrupt {
return nil
}
alias = aliasesList[i].Key
err = internal.Del(alias)
if err != nil {
return err
}
err = internal.PopulateAlias()
if err != nil {
return err
}
} else {
alias = args[0]
err := internal.Del(alias)
if err != nil {
return err
}
err = internal.PopulateAlias()
if err != nil {
return err
}
return err
}

alias = aliasesList[i].Key
err = internal.Del(alias)
if err != nil {
return err
}
err = internal.PopulateAlias()
if err != nil {
return err
}
fmt.Printf("Alias %s deleted.\n", alias)
fmt.Printf("Now run \n%s\nor restart the shell.\n", "source \"$(ff alias)\"")
Expand Down
48 changes: 45 additions & 3 deletions cmd/list.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package cmd

import (
"errors"
"fmt"
"github.com/akymos/ff/internal"
"github.com/chzyer/readline"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"golang.org/x/crypto/ssh/terminal"
"strings"
)

var (
Expand All @@ -15,12 +20,49 @@ var (
defer internal.BaseConfig.Db.Close()
list := internal.FindAll()
if len(list) == 0 {
fmt.Println("No aliases found.")
return nil
return errors.New("no aliases found")
}
aliasesList := make([]aliasList, 0)
for k, v := range list {
fmt.Printf("Alias: %s -> Folder: %s\n", k, v)
aliasesList = append(aliasesList, aliasList{Key: k, Val: v})
}

_, height, err := terminal.GetSize(0)
if err != nil {
return err
}

prompt := promptui.Select{
Label: "All aliases: (ctrl-c to exit)",
Items: aliasesList,
Size: height - 3,
Templates: &promptui.SelectTemplates{
Label: "{{ . }}",
Active: " {{ .Key }} ({{ .Val }})",
Inactive: "{{ .Key }} ({{ .Val }})",
Selected: " {{ .Key }} ({{ .Val }})",
},
Searcher: func(input string, index int) bool {
alias := aliasesList[index]
name := fmt.Sprintf("%s %s", alias.Key, alias.Val)
input = strings.Replace(strings.ToLower(input), " ", "", -1)

return strings.Contains(name, input)
},
Keys: &promptui.SelectKeys{
Prev: promptui.Key{Code: promptui.KeyPrev, Display: promptui.KeyPrevDisplay},
Next: promptui.Key{Code: promptui.KeyNext, Display: promptui.KeyNextDisplay},
PageUp: promptui.Key{Code: promptui.KeyBackward, Display: promptui.KeyBackwardDisplay},
PageDown: promptui.Key{Code: promptui.KeyForward, Display: promptui.KeyForwardDisplay},
Search: promptui.Key{Code: readline.CharTab, Display: "TAB"},
},
}

_, _, err = prompt.Run()
if err != nil && err != promptui.ErrInterrupt {
return err
}

internal.CheckNewVersion()
return nil
},
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ module github.com/akymos/ff
go 1.18

require (
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
github.com/creativeprojects/go-selfupdate v0.6.1
github.com/manifoldco/promptui v0.9.0
github.com/spf13/cobra v1.5.0
go.etcd.io/bbolt v1.3.6
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
)

require (
code.gitea.io/sdk/gitea v0.13.2 // indirect
github.com/Masterminds/semver/v3 v3.1.0 // indirect
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/hashicorp/go-version v1.2.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/ulikunitz/xz v0.5.8 // indirect
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/oauth2 v0.0.0-20201109201403-9fd604954f58 // indirect
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab // indirect
Expand Down

0 comments on commit a4bc60f

Please sign in to comment.