Skip to content

Commit

Permalink
✨BashCompletionsに対応いたしましたわ~~❗❗❗ #72 (#75)
Browse files Browse the repository at this point in the history
* add bash completions

* fix

* memo

* fix
  • Loading branch information
jiro4989 authored Aug 8, 2022
1 parent 8151958 commit 783e8fe
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
12 changes: 12 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ https://github.com/jiro4989/ojosama/releases[Releases]から実行可能ファ
$ go install github.com/jiro4989/ojosama/cmd/ojosama@latest
----

=== タブ補完の有効化

==== Bash

`bash-completion` がインストール済みであれば、
以下のコマンドでタブ補完を使えるようになります。

[source,bash]
----
$ ojosama -completions bash | sudo tee /usr/share/bash-completion/completions/ojosama
----

== 注意事項

=== 変換結果について
Expand Down
19 changes: 13 additions & 6 deletions cmd/ojosama/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
)

type CmdArgs struct {
Text string
OutFile string
Version bool
CharCode string
Args []string
Text string
OutFile string
Version bool
CharCode string
Completions string
Args []string
}

func ParseArgs() (*CmdArgs, error) {
Expand All @@ -23,6 +24,7 @@ func ParseArgs() (*CmdArgs, error) {
flag.StringVar(&opts.OutFile, "o", "", "output file")
flag.StringVar(&opts.CharCode, "charcode", "utf8", "input text file encoding. (utf8 or sjis)")
flag.BoolVar(&opts.Version, "v", false, "print version")
flag.StringVar(&opts.Completions, "completions", "", "print completions file. (bash)")
flag.Parse()
opts.Args = flag.Args()

Expand All @@ -35,7 +37,7 @@ func ParseArgs() (*CmdArgs, error) {

func flagHelpMessage() {
cmd := os.Args[0]
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s convert text to 'ojosama' style.", cmd))
fmt.Fprintln(os.Stderr, fmt.Sprintf("%s convert text to '%s' style.", cmd, appName))
fmt.Fprintln(os.Stderr, "")
fmt.Fprintln(os.Stderr, "Usage:")
fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s [OPTIONS] [files...]", cmd))
Expand All @@ -56,5 +58,10 @@ func (c *CmdArgs) Validate() error {
err := errors.New("charcode must be 'utf8' or 'sjis'.")
return err
}

if c.Completions != "" && !isSupportedCompletions(c.Completions) {
return fmt.Errorf("illegal completions. completions = %s", c.Completions)
}

return nil
}
53 changes: 53 additions & 0 deletions cmd/ojosama/completions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"strings"
)

var (
completionsBash = strings.ReplaceAll(`# {{APPNAME}}(1) completion -*- shell-script -*-
_{{APPNAME}}_module() {
local cur prev cword
_get_comp_words_by_ref -n : cur prev cword
case "${cword}" in
1)
local opts="-h -help -t -o -charcode -v -completions"
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
;;
2)
case "${prev}" in
-o)
COMPREPLY=($(compgen -f -- "${cur}"))
;;
-charcode)
local opts="utf8 sjis"
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
;;
-completions)
local opts="bash"
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
;;
esac
;;
esac
}
complete -F _{{APPNAME}}_module {{APPNAME}}`, "{{APPNAME}}", appName)

completionsMap = map[string]string{
"bash": completionsBash,
}
)

func isSupportedCompletions(sh string) bool {
_, ok := completionsMap[sh]
return ok
}

func printCompletions(sh string) {
sh = strings.ToLower(sh)
fmt.Println(completionsMap[sh])
}
11 changes: 10 additions & 1 deletion cmd/ojosama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
"golang.org/x/text/transform"
)

const (
appName = "ojosama"
)

// CIでビルド時に値を埋め込む。
// 埋め込む値の設定は .goreleaser.yaml を参照。
var (
Expand All @@ -33,14 +37,19 @@ func main() {
}

if args.Version {
msg := fmt.Sprintf("ojosama %s (%s)", version, revision)
msg := fmt.Sprintf("%s %s (%s)", appName, version, revision)
fmt.Println(msg)
fmt.Println("")
fmt.Println("author: jiro")
fmt.Println("repository: https://github.com/jiro4989/ojosama")
os.Exit(exitStatusOK)
}

if args.Completions != "" {
printCompletions(args.Completions)
return
}

if args.Text != "" {
exitStatus, err := run(args.Text, args)
if err != nil {
Expand Down

0 comments on commit 783e8fe

Please sign in to comment.