Skip to content

Commit

Permalink
Update: introduced cli (https://github.com/urfave/cli)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hiroki Konishi committed Oct 21, 2019
1 parent 592eb1b commit e685586
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 42 deletions.
2 changes: 1 addition & 1 deletion _shell/_pmy.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ _pmy_main() {
local buffer_right=${2:-""}
local test_flag=${3:-""}

local out="$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right})"
local out="$(pmy main --buffer-left=${buffer_left} --buffer-right=${buffer_right})"

if [[ -z $out ]] ; then
echo "No rule was matched"
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ go 1.13

require (
github.com/fatih/color v1.7.0
github.com/k0kubun/pp v3.0.1+incompatible
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/mattn/go-zglob v0.0.1
github.com/rakyll/statik v0.1.6
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c
github.com/urfave/cli v1.22.1
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 // indirect
gopkg.in/yaml.v2 v2.2.4
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/k0kubun/pp v3.0.1+incompatible h1:3tqvf7QgUnZ5tXO6pNAZlrvHgl6DvifjDrd9g2S9Z40=
github.com/k0kubun/pp v3.0.1+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
Expand All @@ -10,16 +15,25 @@ github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW1
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mattn/go-zglob v0.0.1 h1:xsEx/XUoVlI6yXjqBK062zYhRTZltCNmYPx6v+8DNaY=
github.com/mattn/go-zglob v0.0.1/go.mod h1:9fxibJccNxU2cnpIKLRRFA7zX7qhkJIQWBb449FYHOo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/statik v0.1.6 h1:uICcfUXpgqtw2VopbIncslhAmE5hwc4g20TEyEENBNs=
github.com/rakyll/statik v0.1.6/go.mod h1:OEi9wJV/fMUAGx1eNjq75DKDsJVuEv1U0oYdX6GX8Zs=
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c h1:+rQG0CRZFZw5xK4A9RCLxRcWdROI4DzCU66iWZx/0HE=
github.com/relastle/colorflag v0.0.0-20190926071630-d4c845e34e8c/go.mod h1:zCHW+H3mqOZ35829qC8zau+dnhUgsqKObIApF0m0KdM=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.22.1 h1:+mkCCcOFKPnCmVYVcURKps1Xe+3zP90gSYGNfRkjoIY=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY=
golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4 h1:/eiJrUcujPVeJ3xlSWaiNi3uSVmDGBK1pDHUHAnao1I=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
99 changes: 60 additions & 39 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package main

import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"time"

"github.com/rakyll/statik/fs"
_ "github.com/relastle/pmy/statik"
"github.com/urfave/cli"

"github.com/relastle/colorflag"
pmy "github.com/relastle/pmy/src"
)

Expand Down Expand Up @@ -44,60 +45,80 @@ func initCmdRoutine() {
fmt.Printf("%s", string(bs))
}

func debug() {
}

func ruleListCmdRoutine() {
pmy.SetConfigs()
ruleFiles := pmy.GetAllRuleFiles()
for _, ruleFile := range ruleFiles {
fmt.Println(ruleFile.Path)
}
}

func snippetListCmdRoutine() {
pmy.SetConfigs()
snippetFiles := pmy.GetAllSnippetFiles()
for _, snippetFile := range snippetFiles {
fmt.Println(snippetFile.Path)
}
}

func main() {
flag.StringVar(&bufferLeft, "bufferLeft", "", "Current left buffer string of zsh prompt")
flag.StringVar(&bufferRight, "bufferRight", "", "Current right buffer string of zsh prompt")

// Subcommand for dumping init zsh script to stdout
initFlagSet := flag.NewFlagSet("init", flag.ExitOnError)

// Subcommandd for listing the all loaded rule json paths
ruleListFlagSet := flag.NewFlagSet("rule-list", flag.ExitOnError)

// Subcommand for listing the all loaded snippet json paths
snippetsListFlagSet := flag.NewFlagSet("snippet-list", flag.ExitOnError)

// Subcommand for debuggin
debugFlagSet := flag.NewFlagSet("debug", flag.ExitOnError)

pmy.SetConfigs()

subCommand := colorflag.Parse([]*flag.FlagSet{
initFlagSet,
ruleListFlagSet,
snippetsListFlagSet,
debugFlagSet,
})
start := time.Now()
app := cli.NewApp()
app.Version = "0.4.0"

app.Commands = []cli.Command{
{
Name: "main",
Usage: "Run main task of pmy. It dumps zsh script necessary to invoke fuzzy finder with appropriate source and edit current zsh line.",
Action: func(c *cli.Context) error {
mainRoutine()
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
Name: "buffer-left, l",
Usage: "Current `left` buffer of zsh line",
Destination: &bufferLeft,
Required: true,
},
cli.StringFlag{
Name: "buffer-right, r",
Usage: "Current `right` buffer of zsh line",
Destination: &bufferRight,
Required: true,
},
},
},
{
Name: "init",
Usage: "Dump zsh script to be sourced in order to activate pmy.",
Action: func(c *cli.Context) error {
initCmdRoutine()
return nil
},
},
{
Name: "rule-list",
Usage: "List all rule files loaded.",
Action: func(c *cli.Context) error {
ruleListCmdRoutine()
return nil
},
},
{
Name: "snippet-list",
Usage: "List all snippet files loaded.",
Action: func(c *cli.Context) error {
snippetListCmdRoutine()
return nil
},
},
}

switch subCommand {
case "init":
initCmdRoutine()
case "rule-list":
ruleListCmdRoutine()
case "snippet-list":
snippetListCmdRoutine()
case "debug":
debug()
default:
mainRoutine()
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
pmy.MeasureElapsedTime(start, "cli arg parse")

}
2 changes: 1 addition & 1 deletion shell/pmy.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ _pmy_main() {

# get output from pmy
# local out="$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right} 2>/dev/null)"
local out="$(pmy --bufferLeft=${buffer_left} --bufferRight=${buffer_right})"
local out="$(pmy main --buffer-left=${buffer_left} --buffer-right=${buffer_right})"

# ${out} is empty, which indicates
# there was no matching
Expand Down
2 changes: 1 addition & 1 deletion statik/statik.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e685586

Please sign in to comment.