Skip to content

Commit

Permalink
Support bash_complete
Browse files Browse the repository at this point in the history
  • Loading branch information
townewgokgok committed Aug 1, 2017
1 parent 2f7dd48 commit f02620e
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 60 deletions.
1 change: 1 addition & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ archive:
name_template: '{{ .Binary }}_{{.Version}}_{{ .Os }}_{{ .Arch }}{{ if .Arm }}v{{
.Arm }}{{ end }}'
files:
- bash_complete
- LICENSE*
- README*
- CHANGELOG*
Expand Down
4 changes: 4 additions & 0 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` してください。
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
14 changes: 14 additions & 0 deletions bash_complete
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions internal/text.go
Original file line number Diff line number Diff line change
@@ -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, "")
}
84 changes: 24 additions & 60 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -112,7 +94,7 @@ func main() {
if err != nil {
return cli.NewExitError(err, 1)
}
printStatus(e, t)
internal.PrintStatus(e, t)
return nil
},
},
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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())
}
}

Expand All @@ -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)
Expand All @@ -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] {
Expand All @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit f02620e

Please sign in to comment.