From f02620e2d038ceca68d6585c7199d9276c8dcd44 Mon Sep 17 00:00:00 2001 From: k-furukawa Date: Tue, 1 Aug 2017 22:21:57 +0900 Subject: [PATCH] Support bash_complete --- .goreleaser.yml | 1 + README.ja.md | 4 +++ README.md | 4 +++ bash_complete | 14 ++++++++ internal/text.go | 62 +++++++++++++++++++++++++++++++++++ main.go | 84 ++++++++++++++---------------------------------- 6 files changed, 109 insertions(+), 60 deletions(-) create mode 100755 bash_complete create mode 100644 internal/text.go diff --git a/.goreleaser.yml b/.goreleaser.yml index 0e30ad2..f6c4beb 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -29,6 +29,7 @@ archive: name_template: '{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' files: + - bash_complete - LICENSE* - README* - CHANGELOG* diff --git a/README.ja.md b/README.ja.md index 50bfbcc..cacc89a 100644 --- a/README.ja.md +++ b/README.ja.md @@ -99,3 +99,7 @@ $ slack-status set -w home itunes [10:30:16] 🏠 æœŹæ—„ćœšćź…äœœæ„­ đŸŽ” Satellite Young - Geeky Boyfriend (from "Satellite Young") [10:33:51] 🏠 æœŹæ—„ćœšćź…äœœæ„­ đŸŽ” Satellite Young - AI Threnody (from "Satellite Young") ``` + +# bash_complete ă‚”ăƒăƒŒăƒˆ + +é…ćžƒç‰©ă«ć«ăŸă‚ŒăŠă„ă‚‹ `bash_complete` をどこかに移拕し、 `.bashrc` 䞭で `source` しどください。 diff --git a/README.md b/README.md index 33b5f86..4646595 100644 --- a/README.md +++ b/README.md @@ -99,3 +99,7 @@ $ slack-status set -w home itunes [10:30:16] 🏠 Working remotely today đŸŽ” Satellite Young - Geeky Boyfriend (from "Satellite Young") [10:33:51] 🏠 Working remotely today đŸŽ” Satellite Young - AI Threnody (from "Satellite Young") ``` + +# bash_complete support + +Move `bash_complete` included in the distribution to somewhere and `source` it in your `.bashrc`. diff --git a/bash_complete b/bash_complete new file mode 100755 index 0000000..6b56973 --- /dev/null +++ b/bash_complete @@ -0,0 +1,14 @@ +#!/bin/bash + +PROG=$(which slack-status) + +_cli_bash_autocomplete() { + local cur opts base + COMPREPLY=() + cur="${COMP_WORDS[COMP_CWORD]}" + opts=$( "$PROG" ${COMP_WORDS[@]:1:$COMP_CWORD} --generate-bash-completion ) + COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) + return 0 +} + +complete -F _cli_bash_autocomplete slack-status diff --git a/internal/text.go b/internal/text.go new file mode 100644 index 0000000..f98b7ba --- /dev/null +++ b/internal/text.go @@ -0,0 +1,62 @@ +package internal + +import ( + "fmt" + "regexp" + "strings" + + "os" + + "github.com/fatih/color" + "github.com/kyokomi/emoji" +) + +func WrapEmoji(e string) string { + if e == "" { + return e + } + return ":" + e + ":" +} + +func LimitStringByLength(str string, maxlen int) string { + r := []rune(str) + if len(r) <= maxlen { + return str + } + return string(r[:maxlen-1]) + "
" +} + +var splitEmojiRegexp = regexp.MustCompile(`^:[^: ]+: *`) + +func SplitEmoji(text string) (string, string) { + text = strings.Trim(text, " ") + e := "" + m := splitEmojiRegexp.FindString(text) + if m != "" { + e = strings.Trim(m, " ") + text = text[len(m):] + } + return e, text +} + +var bggray = color.New(color.BgHiBlack) + +func PrintStatus(e, t string) { + if e != "" { + bggray.Print(emoji.Sprint(e)) + fmt.Print(" ") + } + emoji.Println(t) +} + +var yellow = color.New(color.FgYellow) + +func Warn(msgs ...string) { + for _, msg := range msgs { + lines := strings.Split(msg, "\n") + for _, line := range lines { + yellow.Fprintln(os.Stderr, `[warning] `+line) + } + } + fmt.Fprintln(os.Stderr, "") +} diff --git a/main.go b/main.go index 78e1af0..995c86e 100644 --- a/main.go +++ b/main.go @@ -21,29 +21,10 @@ import ( "github.com/townewgokgok/slack-status/internal" ) -var version = "1.1.0" +var version = "1.2.0" var cyan = color.New(color.FgCyan) -var yellow = color.New(color.FgYellow) var red = color.New(color.FgRed) -var bggray = color.New(color.BgHiBlack) - -func warn(msgs ...string) { - for _, msg := range msgs { - lines := strings.Split(msg, "\n") - for _, line := range lines { - yellow.Fprintln(os.Stderr, `[warning] `+line) - } - } - fmt.Fprintln(os.Stderr, "") -} - -func wrapEmoji(e string) string { - if e == "" { - return e - } - return ":" + e + ":" -} var settings = internal.Settings @@ -80,6 +61,7 @@ func listTemplates(indent string) string { func main() { // Parse arguments app := cli.NewApp() + app.EnableBashCompletion = true app.Name = "slack-status" app.Version = version app.Usage = "Updates your Slack user status from CLI" @@ -112,7 +94,7 @@ func main() { if err != nil { return cli.NewExitError(err, 1) } - printStatus(e, t) + internal.PrintStatus(e, t) return nil }, }, @@ -157,6 +139,17 @@ func main() { Usage: `watch changes (with "` + settings.ITunes.TemplateID + `" or "` + settings.LastFM.TemplateID + `" template)`, }, }, + BashComplete: func(c *cli.Context) { + ids := []string{ + settings.ITunes.TemplateID, + settings.LastFM.TemplateID, + } + for id := range internal.Settings.Templates { + ids = append(ids, id) + } + sort.Strings(ids) + fmt.Println(strings.Join(ids, "\n")) + }, Action: func(ctx *cli.Context) error { flags.dryRun = ctx.Bool("dryrun") flags.watch = ctx.Bool("watch") @@ -170,14 +163,14 @@ func main() { withInfo++ interval = settings.ITunes.WatchIntervalSec if interval < 1 { - warn(`itunes.watch_interval_sec must be >= 1`) + internal.Warn(`itunes.watch_interval_sec must be >= 1`) interval = 5 } case settings.LastFM.TemplateID: withInfo++ interval = settings.LastFM.WatchIntervalSec if interval < 15 { - warn(`lastfm.watch_interval_sec must be >= 15`) + internal.Warn(`lastfm.watch_interval_sec must be >= 15`) interval = 15 } } @@ -206,7 +199,7 @@ func main() { time.Sleep(interval * time.Second) err = update(&flags, ids) if err != nil { - warn(err.Error()) + internal.Warn(err.Error()) } } @@ -233,7 +226,7 @@ func main() { `Try "slack-status example" to show an example settings schema.`, ``, }, internal.SettingsWarnings...) - warn(w...) + internal.Warn(w...) } app.Run(os.Args) @@ -247,7 +240,7 @@ func appendInfo(text, textToAppend string) string { return text } -func appendMusicInfo(settings *internal.MusicSettings, status *internal.MusicStatus) string { +func MusicInfo(settings *internal.MusicSettings, status *internal.MusicStatus) string { r := regexp.MustCompile(`%\w`) return r.ReplaceAllStringFunc(settings.Format, func(m string) string { switch m[1] { @@ -264,35 +257,6 @@ func appendMusicInfo(settings *internal.MusicSettings, status *internal.MusicSta }) } -func limitStringByLength(str string, maxlen int) string { - r := []rune(str) - if len(r) <= maxlen { - return str - } - return string(r[:maxlen-1]) + "
" -} - -var splitEmojiRegexp = regexp.MustCompile(`^:[^: ]+: *`) - -func splitEmoji(text string) (string, string) { - text = strings.Trim(text, " ") - e := "" - m := splitEmojiRegexp.FindString(text) - if m != "" { - e = strings.Trim(m, " ") - text = text[len(m):] - } - return e, text -} - -func printStatus(e, t string) { - if e != "" { - bggray.Print(emoji.Sprint(e)) - fmt.Print(" ") - } - emoji.Println(t) -} - var lastText string var lastEmoji string var updatedCount int @@ -312,7 +276,7 @@ func update(flags *Flags, templateIDs []string) error { case settings.ITunes.TemplateID: status := &internal.GetITunesStatus().MusicStatus if status.Ok { - tmpls = append(tmpls, appendMusicInfo(&settings.ITunes.MusicSettings, status)) + tmpls = append(tmpls, MusicInfo(&settings.ITunes.MusicSettings, status)) continue } n := "iTunes seems to be stopped" @@ -323,7 +287,7 @@ func update(flags *Flags, templateIDs []string) error { case settings.LastFM.TemplateID: status := &internal.GetLastFMStatus().MusicStatus if status.Ok { - tmpls = append(tmpls, appendMusicInfo(&settings.LastFM.MusicSettings, status)) + tmpls = append(tmpls, MusicInfo(&settings.LastFM.MusicSettings, status)) continue } n := "Failed to fetch music information from last.fm" @@ -340,8 +304,8 @@ func update(flags *Flags, templateIDs []string) error { } t := strings.Join(tmpls, " ") - e, t := splitEmoji(t) - t = limitStringByLength(t, internal.SlackUserStatusMaxLength) + e, t := internal.SplitEmoji(t) + t = internal.LimitStringByLength(t, internal.SlackUserStatusMaxLength) changed := updatedCount == 0 || !(t == lastText && e == lastEmoji) if changed { @@ -358,7 +322,7 @@ func update(flags *Flags, templateIDs []string) error { if flags.watch { cyan.Print(now) } - printStatus(e, t) + internal.PrintStatus(e, t) if err != nil { return err }