Skip to content

Commit

Permalink
fix completion.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhadfield committed Jan 7, 2024
1 parent 89c8546 commit 56684e4
Show file tree
Hide file tree
Showing 15 changed files with 186 additions and 31 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,14 @@ then add the following to ~/.bash_profile:
``
#### installing completion script ([found here](https://github.com/jonhadfield/sn-cli/tree/master/autocomplete/bash_autocomplete))
##### macOS
``
$ cp bash_autocomplete /usr/local/etc/bash_completion.d/sn
``
```
$ cp bash_autocomplete /usr/local/etc/bash_completion.d/sn
$ echo "source /usr/local/etc/bash_completion.d/sn" | tee -a ~/.bashrc
```
##### Linux
``
$ cp bash_autocomplete /etc/bash_completion.d/sn
$ echo "source /etc/bash_completion.d/sn" | tee -a ~/.bashrc
``

##### autocomplete commands
Expand Down
30 changes: 24 additions & 6 deletions autocomplete/bash_autocomplete
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,34 @@

: ${PROG:=$(basename ${BASH_SOURCE})}

# Macs have bash3 for which the bash-completion package doesn't include
# _init_completion. This is a minimal version of that function.
_cli_init_completion() {
COMPREPLY=()
_get_comp_words_by_ref "$@" cur prev words cword
}

_cli_bash_autocomplete() {
local cur opts base
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts base words
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
opts=$( ${COMP_WORDS[@]:0:$COMP_CWORD} --generate-bash-completion )
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
if declare -F _init_completion >/dev/null 2>&1; then
_init_completion -n "=:" || return
else
_cli_init_completion -n "=:" || return
fi
words=("${words[@]:0:$cword}")
if [[ "$cur" == "-"* ]]; then
requestComp="${words[*]} ${cur} --generate-bash-completion"
else
requestComp="${words[*]} --generate-bash-completion"
fi
opts=$(eval "${requestComp}" 2>/dev/null)
COMPREPLY=($(compgen -W "${opts}" -- ${cur}))
return 0
fi
}

complete -F _cli_bash_autocomplete $PROG

complete -o bashdefault -o default -o nospace -F _cli_bash_autocomplete $PROG
unset PROG

9 changes: 9 additions & 0 deletions autocomplete/powershell_autocomplete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$fn = $($MyInvocation.MyCommand.Name)
$name = $fn -replace "(.*)\.ps1$", '$1'
Register-ArgumentCompleter -Native -CommandName $name -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$other = "$wordToComplete --generate-bash-completion"
Invoke-Expression $other | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
}
}
20 changes: 20 additions & 0 deletions autocomplete/zsh_autocomplete
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#compdef $PROG

_cli_zsh_autocomplete() {
local -a opts
local cur
cur=${words[-1]}
if [[ "$cur" == "-"* ]]; then
opts=("${(@f)$(${words[@]:0:#words[@]-1} ${cur} --generate-bash-completion)}")
else
opts=("${(@f)$(${words[@]:0:#words[@]-1} --generate-bash-completion)}")
fi

if [[ "${opts[1]}" != "" ]]; then
_describe 'values' opts
else
_files
fi
}

compdef _cli_zsh_autocomplete $PROG
7 changes: 4 additions & 3 deletions cmd/sncli/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ func cmdAdd() *cli.Command {
Name: "add",
Usage: "add items",
BashComplete: func(c *cli.Context) {
addTasks := []string{"tag", "note"}
if c.NArg() > 0 {
return
}
for _, t := range addTasks {
for _, t := range []string{"tag", "note"} {
fmt.Println(t)
}
},
Expand All @@ -27,7 +26,9 @@ func cmdAdd() *cli.Command {
if c.NArg() > 0 {
return
}
fmt.Println("--title", "--parent", "--parent-uuid")
for _, t := range []string{"--title", "--parent", "--parent-uuid"} {
fmt.Println(t)
}
},
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down
24 changes: 22 additions & 2 deletions cmd/sncli/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func cmdGet() *cli.Command {
Name: "get",
Usage: "get items",
BashComplete: func(c *cli.Context) {
addTasks := []string{"tag", "note", "settings"}
addTasks := []string{"tag", "note"}
if c.NArg() > 0 {
return
}
Expand Down Expand Up @@ -177,6 +177,17 @@ func cmdGet() *cli.Command {
Name: "tag",
Aliases: []string{"tags"},
Usage: "get tags",
BashComplete: func(c *cli.Context) {
tagTasks := []string{
"--title", "--uuid", "--regex", "--match-all", "--count", "--output",
}
if c.NArg() > 0 {
return
}
for _, t := range tagTasks {
fmt.Println(t)
}
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "title",
Expand Down Expand Up @@ -217,6 +228,15 @@ func cmdGet() *cli.Command {
Name: "note",
Aliases: []string{"notes"},
Usage: "get notes",
BashComplete: func(c *cli.Context) {
addTasks := []string{"--title", "--text", "--tag", "--uuid", "--editor", "--include-trash", "--count"}
if c.NArg() > 0 {
return
}
for _, t := range addTasks {
fmt.Println(t)
}
},
Flags: []cli.Flag{
&cli.StringFlag{
Name: "title",
Expand Down Expand Up @@ -244,7 +264,7 @@ func cmdGet() *cli.Command {
},
&cli.BoolFlag{
Name: "count",
Usage: "useStdOut countonly",
Usage: "number of notes",
},
&cli.StringFlag{
Name: "output",
Expand Down
8 changes: 7 additions & 1 deletion cmd/sncli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,13 @@ func appSetup() (app *cli.App) {
app.HelpName = "-"
app.Usage = "Standard Notes CLI"
app.Description = ""

app.BashComplete = func(c *cli.Context) {
for _, cmd := range c.App.Commands {
if !cmd.Hidden {
fmt.Fprintln(c.App.Writer, cmd.Name)
}
}
}
app.Flags = []cli.Flag{
&cli.BoolFlag{Name: "debug", Value: viper.GetBool("debug")},
&cli.StringFlag{Name: "server", Value: viper.GetString("server")},
Expand Down
17 changes: 13 additions & 4 deletions cmd/sncli/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,12 @@ func outputNotes(c *cli.Context, count bool, output string, getNoteConfig sncli.
return nil
}

if count {
_, _ = fmt.Fprintf(c.App.Writer, fmt.Sprintf("%d\n", len(rawNotes)))

return nil
}

var numResults int

var notesYAML []sncli.NoteYAML
Expand All @@ -518,7 +524,7 @@ func outputNotes(c *cli.Context, count bool, output string, getNoteConfig sncli.
for _, rt := range rawNotes {
numResults++

if !count && sncli.StringInSlice(output, yamlAbbrevs, false) {
if sncli.StringInSlice(output, yamlAbbrevs, false) {
noteContentOrgStandardNotesSNDetailYAML := sncli.OrgStandardNotesSNDetailYAML{
ClientUpdatedAt: rt.(*items.Note).Content.GetAppData().OrgStandardNotesSN.ClientUpdatedAt,
}
Expand Down Expand Up @@ -549,7 +555,7 @@ func outputNotes(c *cli.Context, count bool, output string, getNoteConfig sncli.
})
}

if !count && strings.ToLower(output) == "json" {
if strings.ToLower(output) == "json" {
noteContentOrgStandardNotesSNDetailJSON := sncli.OrgStandardNotesSNDetailJSON{
ClientUpdatedAt: rt.(*items.Note).Content.GetAppData().OrgStandardNotesSN.ClientUpdatedAt,
Pinned: rt.(*items.Note).Content.GetAppData().OrgStandardNotesSN.Pinned,
Expand Down Expand Up @@ -590,13 +596,16 @@ func outputNotes(c *cli.Context, count bool, output string, getNoteConfig sncli.
}

output = c.String("output")

var bOutput []byte

switch strings.ToLower(output) {
case "json":
bOutput, err = json.MarshalIndent(notesJSON, "", " ")
case "yaml":
bOutput, err = yaml.Marshal(notesYAML)
}

if len(bOutput) > 0 {
if output == "json" {
fmt.Print("{\n \"items\": ")
Expand Down Expand Up @@ -656,10 +665,10 @@ func processAddNotes(c *cli.Context, opts configOptsOutput) (err error) {
}

if err = AddNoteInput.Run(); err != nil {
return fmt.Errorf("failed to add note. %+v", err)
return fmt.Errorf("failed to add note: %+v", err)
}

_, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprintf("%s: %s", msgNoteAdded, title))
_, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprintf("%s: %s\n", msgNoteAdded, title))

return nil
}
Expand Down
5 changes: 4 additions & 1 deletion cmd/sncli/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ func cmdRegister() *cli.Command {
if c.NArg() > 0 {
return
}
fmt.Println("--email")

for _, t := range []string{"--email"} {
fmt.Println(t)
}
},
Flags: []cli.Flag{
&cli.StringFlag{
Expand Down
3 changes: 3 additions & 0 deletions cmd/sncli/resync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ func cmdResync() *cli.Command {
return &cli.Command{
Name: "resync",
Usage: "purge cache and resync content",
BashComplete: func(c *cli.Context) {
return
},
Action: func(c *cli.Context) error {
opts := getOpts(c)

Expand Down
13 changes: 11 additions & 2 deletions cmd/sncli/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ func cmdSession() *cli.Command {
Required: false,
},
},
Hidden: false,
BashComplete: func(c *cli.Context) {
if c.NArg() > 0 {
return
}

for _, t := range []string{"--add", "--remove", "--status", "--session-key"} {
fmt.Println(t)
}
},
Action: func(c *cli.Context) error {
opts := getOpts(c)

Expand Down Expand Up @@ -81,12 +89,13 @@ func processSession(c *cli.Context, opts configOptsOutput) (err error) {

if sStatus {
var msg string

msg, err = session.SessionStatus(sessKey, nil)
if err != nil {
return err
}

_, _ = fmt.Fprint(c.App.Writer, msg)
_, _ = fmt.Fprint(c.App.Writer, msg+"\n")
}

return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/sncli/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ func cmdStats() *cli.Command {
return &cli.Command{
Name: "stats",
Usage: "show statistics",
BashComplete: func(c *cli.Context) {
if c.NArg() > 0 {
return
}
},
Action: func(c *cli.Context) error {
opts := getOpts(c)

Expand Down
17 changes: 13 additions & 4 deletions cmd/sncli/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func processAddTags(c *cli.Context, opts configOptsOutput) (err error) {
var msg string
// present results
if len(ato.Added) > 0 {
_, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprint(msgTagAdded+": ", strings.Join(ato.Added, ", ")))
_, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprint(msgTagAdded+": ", strings.Join(ato.Added, ", "), "\n"))

return err
}
Expand All @@ -436,7 +436,7 @@ func processAddTags(c *cli.Context, opts configOptsOutput) (err error) {
msg += "\n"
}

_, _ = fmt.Fprintf(c.App.Writer, color.Yellow.Sprint(msgTagAlreadyExists+": "+strings.Join(ato.Existing, ", ")))
_, _ = fmt.Fprintf(c.App.Writer, color.Yellow.Sprint(msgTagAlreadyExists+": "+strings.Join(ato.Existing, ", "), "\n"))
}

_, _ = fmt.Fprintf(c.App.Writer, "%s\n", msg)
Expand Down Expand Up @@ -545,7 +545,6 @@ func cmdTag() *cli.Command {
return &cli.Command{
Name: "tag",
Usage: "tag items",

Flags: []cli.Flag{
&cli.StringFlag{
Name: "find-title",
Expand All @@ -572,14 +571,24 @@ func cmdTag() *cli.Command {
Usage: "ignore case when matching",
},
},
BashComplete: func(c *cli.Context) {
if c.NArg() > 0 {
return
}
for _, t := range []string{
"--find-title", "--find-text", "--find-tag", "--title", "--purge", "--ignore-case",
} {
fmt.Println(t)
}
},
Action: func(c *cli.Context) error {
opts := getOpts(c)

if err := processTagItems(c, opts); err != nil {
return err
}

_, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprintf("%s", msgTagSuccess))
_, _ = fmt.Fprintf(c.App.Writer, color.Green.Sprint(msgTagSuccess, "\n"))

return nil
},
Expand Down
Loading

0 comments on commit 56684e4

Please sign in to comment.