Merge branch 'v0.2.0'

This commit is contained in:
erroneousboat 2016-11-01 17:06:58 +01:00
commit f516ba082e
8 changed files with 336 additions and 116 deletions

View File

@ -27,11 +27,40 @@ Getting started
{
"slack_token": "yourslacktokenhere",
// optional: add the following to use light theme, default is dark
// OPTIONAL: add the following to use light theme, default is dark
"theme": "light",
// optional: set the width of the sidebar (between 1 and 11), default is 1
"sidebar_width": 3
// OPTIONAL: set the width of the sidebar (between 1 and 11), default is 1
"sidebar_width": 3,
// OPTIONAL: define custom key mappings, defaults are:
"key_map": {
"command": {
"i": "mode-insert",
"k": "channel-up",
"j": "channel-down",
"g": "channel-top",
"G": "channel-bottom",
"<previous>": "chat-up",
"C-b": "chat-up",
"C-u": "chat-up",
"<next>": "chat-down",
"C-f": "chat-down",
"C-d": "chat-down",
"q": "quit",
"<f1>": "help"
},
"insert": {
"<left>": "cursor-left",
"<right>": "cursor-right",
"<enter>": "send",
"<escape>": "mode-command",
"<backspace>": "backspace",
"C-8": "backspace",
"<delete>": "delete",
"<space>": "space",
}
}
}
```
@ -44,25 +73,25 @@ Getting started
$ slack-term -config [path-to-config-file]
```
Usage
-----
Default Key Mapping
-------------------
| mode | key | action |
|--------|-----------|----------------------------|
| normal | `i` | insert mode |
| normal | `k` | move channel cursor up |
| normal | `j` | move channel cursor down |
| normal | `gg` | move channel cursor top |
| normal | `G` | move channel cursor bottom |
| normal | `pg-up` | scroll chat pane up |
| normal | `ctrl-b` | scroll chat pane up |
| normal | `ctrl-u` | scroll chat pane up |
| normal | `pg-down` | scroll chat pane down |
| normal | `ctrl-f` | scroll chat pane down |
| normal | `ctrl-d` | scroll chat pane down |
| normal | `pg-down` | scroll chat pane down |
| normal | `q` | quit |
|---------|-----------|----------------------------|
| command | `i` | insert mode |
| command | `k` | move channel cursor up |
| command | `j` | move channel cursor down |
| command | `g` | move channel cursor top |
| command | `G` | move channel cursor bottom |
| command | `pg-up` | scroll chat pane up |
| command | `ctrl-b` | scroll chat pane up |
| command | `ctrl-u` | scroll chat pane up |
| command | `pg-down` | scroll chat pane down |
| command | `ctrl-f` | scroll chat pane down |
| command | `ctrl-d` | scroll chat pane down |
| command | `q` | quit |
| command | `f1` | help |
| insert | `left` | move input cursor left |
| insert | `right` | move input cursor right |
| insert | `enter` | send message |
| insert | `esc` | normal mode |
| insert | `esc` | command mode |

View File

@ -220,3 +220,7 @@ func (c *Channels) ClearNewMessageIndicator() {
c.List.Items[c.SelectedChannel] = channelName[0]
}
}
func (c *Channels) SetReadMark(svc *service.SlackService) {
svc.SetChannelReadMark(svc.SlackChannels[c.SelectedChannel])
}

View File

@ -1,11 +1,14 @@
package components
import (
"fmt"
"html"
"sort"
"strings"
"github.com/gizak/termui"
"github.com/erroneousboat/slack-term/config"
"github.com/erroneousboat/slack-term/service"
)
@ -16,7 +19,7 @@ type Chat struct {
}
// CreateChat is the constructor for the Chat struct
func CreateChat(svc *service.SlackService, inputHeight int, selectedChannel interface{}, selectedChannelName string) *Chat {
func CreateChat(svc *service.SlackService, inputHeight int, selectedSlackChannel interface{}, selectedChannel service.Channel) *Chat {
chat := &Chat{
List: termui.NewList(),
Offset: 0,
@ -25,8 +28,8 @@ func CreateChat(svc *service.SlackService, inputHeight int, selectedChannel inte
chat.List.Height = termui.TermHeight() - inputHeight
chat.List.Overflow = "wrap"
chat.GetMessages(svc, selectedChannel)
chat.SetBorderLabel(selectedChannelName)
chat.GetMessages(svc, selectedSlackChannel)
chat.SetBorderLabel(selectedChannel)
return chat
}
@ -205,6 +208,46 @@ func (c *Chat) ScrollDown() {
}
// SetBorderLabel will set Label of the Chat pane to the specified string
func (c *Chat) SetBorderLabel(label string) {
c.List.BorderLabel = label
func (c *Chat) SetBorderLabel(channel service.Channel) {
var channelName string
if channel.Topic != "" {
channelName = fmt.Sprintf("%s - %s",
channel.Name,
channel.Topic,
)
} else {
channelName = channel.Name
}
c.List.BorderLabel = channelName
}
// Help shows the usage and key bindings in the chat pane
func (c *Chat) Help(cfg *config.Config) {
help := []string{
"slack-term - slack client for your terminal",
"",
"USAGE:",
" slack-term -config [path-to-config]",
"",
"KEY BINDINGS:",
"",
}
for mode, mapping := range cfg.KeyMap {
help = append(help, fmt.Sprintf(" %s", strings.ToUpper(mode)))
help = append(help, "")
var keys []string
for k := range mapping {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
help = append(help, fmt.Sprintf(" %-12s%-15s", k, mapping[k]))
}
help = append(help, "")
}
c.List.Items = help
}

View File

@ -14,14 +14,44 @@ type Config struct {
Theme string `json:"theme"`
SidebarWidth int `json:"sidebar_width"`
MainWidth int `json:"-"`
KeyMap map[string]keyMapping `json:"key_map"`
}
type keyMapping map[string]string
// NewConfig loads the config file and returns a Config struct
func NewConfig(filepath string) (*Config, error) {
cfg := Config{
Theme: "dark",
SidebarWidth: 1,
MainWidth: 11,
KeyMap: map[string]keyMapping{
"command": {
"i": "mode-insert",
"k": "channel-up",
"j": "channel-down",
"g": "channel-top",
"G": "channel-bottom",
"<previous>": "chat-up",
"C-b": "chat-up",
"C-u": "chat-up",
"<next>": "chat-down",
"C-f": "chat-down",
"C-d": "chat-down",
"q": "quit",
"<f1>": "help",
},
"insert": {
"<left>": "cursor-left",
"<right>": "cursor-right",
"<enter>": "send",
"<escape>": "mode-command",
"<backspace>": "backspace",
"C-8": "backspace",
"<delete>": "delete",
"<space>": "space",
},
},
}
file, err := os.Open(filepath)

View File

@ -1,6 +1,9 @@
package handlers
import (
"strconv"
"time"
"github.com/gizak/termui"
"github.com/nlopes/slack"
termbox "github.com/nsf/termbox-go"
@ -9,6 +12,30 @@ import (
"github.com/erroneousboat/slack-term/views"
)
var timer *time.Timer
// actionMap binds specific action names to the function counterparts,
// these action names can then be used to bind them to specific keys
// in the Config.
var actionMap = map[string]func(*context.AppContext){
"space": actionSpace,
"backspace": actionBackSpace,
"delete": actionDelete,
"cursor-right": actionMoveCursorRight,
"cursor-left": actionMoveCursorLeft,
"send": actionSend,
"quit": actionQuit,
"mode-insert": actionInsertMode,
"mode-command": actionCommandMode,
"channel-up": actionMoveCursorUpChannels,
"channel-down": actionMoveCursorDownChannels,
"channel-top": actionMoveCursorTopChannels,
"channel-bottom": actionMoveCursorBottomChannels,
"chat-up": actionScrollUpChat,
"chat-down": actionScrollDownChat,
"help": actionHelp,
}
func RegisterEventHandlers(ctx *context.AppContext) {
anyKeyHandler(ctx)
incomingMessageHandler(ctx)
@ -20,59 +47,28 @@ func anyKeyHandler(ctx *context.AppContext) {
for {
ev := termbox.PollEvent()
if ev.Type == termbox.EventKey {
if ctx.Mode == context.CommandMode {
switch ev.Key {
case termbox.KeyPgup:
actionScrollUpChat(ctx)
case termbox.KeyCtrlB:
actionScrollUpChat(ctx)
case termbox.KeyCtrlU:
actionScrollUpChat(ctx)
case termbox.KeyPgdn:
actionScrollDownChat(ctx)
case termbox.KeyCtrlF:
actionScrollDownChat(ctx)
case termbox.KeyCtrlD:
actionScrollDownChat(ctx)
default:
switch ev.Ch {
case 'q':
actionQuit()
case 'j':
actionMoveCursorDownChannels(ctx)
case 'k':
actionMoveCursorUpChannels(ctx)
case 'g':
actionMoveCursorTopChannels(ctx)
case 'G':
actionMoveCursorBottomChannels(ctx)
case 'i':
actionInsertMode(ctx)
if ev.Type != termbox.EventKey {
continue
}
keyStr := getKeyString(ev)
// Get the action name (actionStr) from the key that
// has been pressed. If this is found try to uncover
// the associated function with this key and execute
// it.
actionStr, ok := ctx.Config.KeyMap[ctx.Mode][keyStr]
if ok {
action, ok := actionMap[actionStr]
if ok {
action(ctx)
}
} else if ctx.Mode == context.InsertMode {
switch ev.Key {
case termbox.KeyEsc:
actionCommandMode(ctx)
case termbox.KeyEnter:
actionSend(ctx)
case termbox.KeySpace:
actionInput(ctx.View, ' ')
case termbox.KeyBackspace, termbox.KeyBackspace2:
actionBackSpace(ctx.View)
case termbox.KeyDelete:
actionDelete(ctx.View)
case termbox.KeyArrowRight:
actionMoveCursorRight(ctx.View)
case termbox.KeyArrowLeft:
actionMoveCursorLeft(ctx.View)
default:
} else {
if ctx.Mode == context.InsertMode && ev.Ch != 0 {
actionInput(ctx.View, ev.Ch)
}
}
}
}
}()
}
@ -135,24 +131,28 @@ func actionInput(view *views.View, key rune) {
termui.Render(view.Input)
}
func actionBackSpace(view *views.View) {
view.Input.Backspace()
termui.Render(view.Input)
func actionSpace(ctx *context.AppContext) {
actionInput(ctx.View, ' ')
}
func actionDelete(view *views.View) {
view.Input.Delete()
termui.Render(view.Input)
func actionBackSpace(ctx *context.AppContext) {
ctx.View.Input.Backspace()
termui.Render(ctx.View.Input)
}
func actionMoveCursorRight(view *views.View) {
view.Input.MoveCursorRight()
termui.Render(view.Input)
func actionDelete(ctx *context.AppContext) {
ctx.View.Input.Delete()
termui.Render(ctx.View.Input)
}
func actionMoveCursorLeft(view *views.View) {
view.Input.MoveCursorLeft()
termui.Render(view.Input)
func actionMoveCursorRight(ctx *context.AppContext) {
ctx.View.Input.MoveCursorRight()
termui.Render(ctx.View.Input)
}
func actionMoveCursorLeft(ctx *context.AppContext) {
ctx.View.Input.MoveCursorLeft()
termui.Render(ctx.View.Input)
}
func actionSend(ctx *context.AppContext) {
@ -172,7 +172,7 @@ func actionSend(ctx *context.AppContext) {
}
}
func actionQuit() {
func actionQuit(*context.AppContext) {
termui.StopLoop()
}
@ -197,19 +197,36 @@ func actionGetMessages(ctx *context.AppContext) {
termui.Render(ctx.View.Chat)
}
func actionGetChannels(ctx *context.AppContext) {
ctx.View.Channels.GetChannels(ctx.Service)
termui.Render(ctx.View.Channels)
}
func actionMoveCursorUpChannels(ctx *context.AppContext) {
go func() {
if timer != nil {
timer.Stop()
}
ctx.View.Channels.MoveCursorUp()
termui.Render(ctx.View.Channels)
timer = time.NewTimer(time.Second / 4)
<-timer.C
actionChangeChannel(ctx)
}()
}
func actionMoveCursorDownChannels(ctx *context.AppContext) {
go func() {
if timer != nil {
timer.Stop()
}
ctx.View.Channels.MoveCursorDown()
termui.Render(ctx.View.Channels)
timer = time.NewTimer(time.Second / 4)
<-timer.C
actionChangeChannel(ctx)
}()
}
func actionMoveCursorTopChannels(ctx *context.AppContext) {
@ -234,9 +251,12 @@ func actionChangeChannel(ctx *context.AppContext) {
// Set channel name for the Chat pane
ctx.View.Chat.SetBorderLabel(
ctx.Service.Channels[ctx.View.Channels.SelectedChannel].Name,
ctx.Service.Channels[ctx.View.Channels.SelectedChannel],
)
// Set read mark
ctx.View.Channels.SetReadMark(ctx.Service)
termui.Render(ctx.View.Channels)
termui.Render(ctx.View.Chat)
}
@ -255,3 +275,58 @@ func actionScrollDownChat(ctx *context.AppContext) {
ctx.View.Chat.ScrollDown()
termui.Render(ctx.View.Chat)
}
func actionHelp(ctx *context.AppContext) {
ctx.View.Chat.Help(ctx.Config)
termui.Render(ctx.View.Chat)
}
// GetKeyString will return a string that resembles the key event from
// termbox. This is blatanly copied from termui because it is an unexported
// function.
//
// See:
// - https://github.com/gizak/termui/blob/a7e3aeef4cdf9fa2edb723b1541cb69b7bb089ea/events.go#L31-L72
// - https://github.com/nsf/termbox-go/blob/master/api_common.go
func getKeyString(e termbox.Event) string {
var ek string
k := string(e.Ch)
pre := ""
mod := ""
if e.Mod == termbox.ModAlt {
mod = "M-"
}
if e.Ch == 0 {
if e.Key > 0xFFFF-12 {
k = "<f" + strconv.Itoa(0xFFFF-int(e.Key)+1) + ">"
} else if e.Key > 0xFFFF-25 {
ks := []string{"<insert>", "<delete>", "<home>", "<end>", "<previous>", "<next>", "<up>", "<down>", "<left>", "<right>"}
k = ks[0xFFFF-int(e.Key)-12]
}
if e.Key <= 0x7F {
pre = "C-"
k = string('a' - 1 + int(e.Key))
kmap := map[termbox.Key][2]string{
termbox.KeyCtrlSpace: {"C-", "<space>"},
termbox.KeyBackspace: {"", "<backspace>"},
termbox.KeyTab: {"", "<tab>"},
termbox.KeyEnter: {"", "<enter>"},
termbox.KeyEsc: {"", "<escape>"},
termbox.KeyCtrlBackslash: {"C-", "\\"},
termbox.KeyCtrlSlash: {"C-", "/"},
termbox.KeySpace: {"", "<space>"},
termbox.KeyCtrl8: {"C-", "8"},
}
if sk, ok := kmap[e.Key]; ok {
pre = sk[0]
k = sk[1]
}
}
}
ek = pre + mod + k
return ek
}

View File

@ -14,6 +14,7 @@ import (
)
const (
VERSION = "v0.2.0"
USAGE = `NAME:
slack-term - slack client for your terminal
@ -31,8 +32,6 @@ GLOBAL OPTIONS:
var (
flgConfig string
flgUsage bool
VERSION = "v0.1.0"
)
func init() {

View File

@ -21,6 +21,7 @@ type SlackService struct {
type Channel struct {
ID string
Name string
Topic string
}
// NewSlackService is the constructor for the SlackService and will initialize
@ -71,7 +72,7 @@ func (s *SlackService) GetChannels() []Channel {
}
for _, chn := range slackChans {
s.SlackChannels = append(s.SlackChannels, chn)
chans = append(chans, Channel{chn.ID, chn.Name})
chans = append(chans, Channel{chn.ID, chn.Name, chn.Topic.Value})
}
// Groups
@ -81,7 +82,7 @@ func (s *SlackService) GetChannels() []Channel {
}
for _, grp := range slackGroups {
s.SlackChannels = append(s.SlackChannels, grp)
chans = append(chans, Channel{grp.ID, grp.Name})
chans = append(chans, Channel{grp.ID, grp.Name, grp.Topic.Value})
}
// IM
@ -97,7 +98,7 @@ func (s *SlackService) GetChannels() []Channel {
// to the UserCache, so we skip it
name, ok := s.UserCache[im.User]
if ok {
chans = append(chans, Channel{im.ID, name})
chans = append(chans, Channel{im.ID, name, ""})
s.SlackChannels = append(s.SlackChannels, im)
}
}
@ -107,6 +108,29 @@ func (s *SlackService) GetChannels() []Channel {
return chans
}
// SetChannelReadMark will set the read mark for a channel, group, and im
// channel based on the current time.
func (s *SlackService) SetChannelReadMark(channel interface{}) {
switch channel := channel.(type) {
case slack.Channel:
s.Client.SetChannelReadMark(
channel.ID, fmt.Sprintf("%f",
float64(time.Now().Unix())),
)
case slack.Group:
s.Client.SetGroupReadMark(
channel.ID, fmt.Sprintf("%f",
float64(time.Now().Unix())),
)
case slack.IM:
s.Client.MarkIMChannel(
channel.ID, fmt.Sprintf("%f",
float64(time.Now().Unix())),
)
}
}
// SendMessage will send a message to a particular channel
func (s *SlackService) SendMessage(channel string, message string) {
// https://godoc.org/github.com/nlopes/slack#PostMessageParameters
postParams := slack.PostMessageParameters{
@ -117,6 +141,8 @@ func (s *SlackService) SendMessage(channel string, message string) {
s.Client.PostMessage(channel, message, postParams)
}
// GetMessages will get messages for a channel, group or im channel delimited
// by a count.
func (s *SlackService) GetMessages(channel interface{}, count int) []string {
// https://api.slack.com/methods/channels.history
historyParams := slack.HistoryParameters{
@ -135,7 +161,6 @@ func (s *SlackService) GetMessages(channel interface{}, count int) []string {
log.Fatal(err) // FIXME
}
case slack.Group:
// TODO: json: cannot unmarshal number into Go value of type string<Paste>
history, err = s.Client.GetGroupHistory(chnType.ID, historyParams)
if err != nil {
log.Fatal(err) // FIXME
@ -235,6 +260,12 @@ func (s *SlackService) CreateMessageFromMessageEvent(message *slack.MessageEvent
var msgs []string
var name string
// Append (edited) when an edited message is received
if message.SubType == "message_changed" {
message = &slack.MessageEvent{Msg: *message.SubMessage}
message.Text = fmt.Sprintf("%s (edited)", message.Text)
}
// Get username from cache
name, ok := s.UserCache[message.User]
@ -304,6 +335,15 @@ func createMessageFromAttachments(atts []slack.Attachment) []string {
),
)
}
if att.Text != "" {
msgs = append(msgs, att.Text)
}
if att.Title != "" {
msgs = append(msgs, att.Title)
}
}
return msgs
}

View File

@ -23,7 +23,7 @@ func CreateChatView(svc *service.SlackService) *View {
svc,
input.Par.Height,
svc.SlackChannels[channels.SelectedChannel],
svc.Channels[channels.SelectedChannel].Name,
svc.Channels[channels.SelectedChannel],
)
mode := components.CreateMode()