diff --git a/components/input.go b/components/input.go index db1e61f..38bb38d 100644 --- a/components/input.go +++ b/components/input.go @@ -74,8 +74,11 @@ func (i *Input) SendMessage(svc *service.SlackService, channel string, message s func (i *Input) Insert(key rune) { if len(i.Text) < i.Par.InnerBounds().Dx()-1 { - first := append(i.Text[0:i.CursorPosition], key) - i.Text = append(first, i.Text[i.CursorPosition:]...) + left := make([]rune, len(i.Text[0:i.CursorPosition])) + copy(left, i.Text[0:i.CursorPosition]) + left = append(left, key) + + i.Text = append(left, i.Text[i.CursorPosition:]...) i.Par.Text = string(i.Text) i.MoveCursorRight() diff --git a/context/context.go b/context/context.go index cd770ae..2a6309b 100644 --- a/context/context.go +++ b/context/context.go @@ -1,8 +1,6 @@ package context import ( - "log" - "github.com/gizak/termui" termbox "github.com/nsf/termbox-go" @@ -28,11 +26,11 @@ type AppContext struct { // CreateAppContext creates an application context which can be passed // and referenced througout the application -func CreateAppContext(flgConfig string) *AppContext { +func CreateAppContext(flgConfig string) (*AppContext, error) { // Load config config, err := config.NewConfig(flgConfig) if err != nil { - log.Fatalf("ERROR: not able to load config file (%s): %s", flgConfig, err) + return nil, err } // Create Service @@ -47,5 +45,5 @@ func CreateAppContext(flgConfig string) *AppContext { View: view, Config: config, Mode: CommandMode, - } + }, nil } diff --git a/handlers/event.go b/handlers/event.go index c7a0fe7..ef38d1c 100644 --- a/handlers/event.go +++ b/handlers/event.go @@ -41,7 +41,7 @@ var actionMap = map[string]func(*context.AppContext){ func RegisterEventHandlers(ctx *context.AppContext) { eventHandler(ctx) - incomingMessageHandler(ctx) + messageHandler(ctx) } func eventHandler(ctx *context.AppContext) { @@ -54,18 +54,38 @@ func eventHandler(ctx *context.AppContext) { go func() { for { ev := <-ctx.EventQueue - - switch ev.Type { - case termbox.EventKey: - actionKeyEvent(ctx, ev) - case termbox.EventResize: - actionResizeEvent(ctx, ev) - } + handleTermboxEvents(ctx, ev) + handleMoreTermboxEvents(ctx, ev) } }() } -func incomingMessageHandler(ctx *context.AppContext) { +func handleTermboxEvents(ctx *context.AppContext, ev termbox.Event) bool { + switch ev.Type { + case termbox.EventKey: + actionKeyEvent(ctx, ev) + case termbox.EventResize: + actionResizeEvent(ctx, ev) + } + + return true +} + +func handleMoreTermboxEvents(ctx *context.AppContext, ev termbox.Event) bool { + for { + select { + case ev := <- ctx.EventQueue: + ok := handleTermboxEvents(ctx, ev) + if !ok { + return false + } + default: + return true + } + } +} + +func messageHandler(ctx *context.AppContext) { go func() { for { select { diff --git a/main.go b/main.go index 59329f0..dd2678f 100644 --- a/main.go +++ b/main.go @@ -4,17 +4,19 @@ import ( "flag" "fmt" "log" + "os" "os/user" "path" "github.com/erroneousboat/slack-term/context" "github.com/erroneousboat/slack-term/handlers" + termbox "github.com/nsf/termbox-go" "github.com/gizak/termui" ) const ( - VERSION = "v0.2.2" + VERSION = "v0.2.3" USAGE = `NAME: slack-term - slack client for your terminal @@ -74,7 +76,12 @@ func main() { termui.DefaultEvtStream = customEvtStream // Create context - ctx := context.CreateAppContext(flgConfig) + ctx, err := context.CreateAppContext(flgConfig) + if err != nil { + termbox.Close() + log.Println(err) + os.Exit(0) + } // Setup body termui.Body.AddRows( diff --git a/vendor/vendor.json b/vendor/vendor.json index 442aa53..e3e8848 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -5,20 +5,20 @@ { "checksumSHA1": "0orwvPL96wFckVJyPl39fz2QsgA=", "path": "github.com/gizak/termui", - "revision": "991cd3d3809135dc24daf6188dc6edcaf3d7d2d9", - "revisionTime": "2017-01-17T22:23:42Z" + "revision": "72304ddb9b4e9838426a021aad64a5a860e98324", + "revisionTime": "2017-05-02T14:12:00Z" }, { "checksumSHA1": "7hln62oZPZmyqEmgXaybf9WxQ7A=", "path": "github.com/maruel/panicparse/stack", - "revision": "25bcac0d793cf4109483505a0d66e066a3a90a80", - "revisionTime": "2017-02-27T22:23:42Z" + "revision": "868abbf1ebac0fb2760cd9a410a5bd2f5afb2f76", + "revisionTime": "2017-07-16T23:31:26Z" }, { - "checksumSHA1": "MNkKJyk2TazKMJYbal5wFHybpyA=", + "checksumSHA1": "cJE7dphDlam/i7PhnsyosNWtbd4=", "path": "github.com/mattn/go-runewidth", - "revision": "14207d285c6c197daabb5c9793d63e7af9ab2d50", - "revisionTime": "2017-02-01T02:35:40Z" + "revision": "97311d9f7767e3d6f422ea06661bc2c7a19e8a5d", + "revisionTime": "2017-05-10T07:48:58Z" }, { "checksumSHA1": "L3leymg2RT8hFl5uL+5KP/LpBkg=", @@ -27,22 +27,22 @@ "revisionTime": "2015-03-14T17:03:34Z" }, { - "checksumSHA1": "oNrSJkPTYsPtwH9/pUXOTIjVuks=", + "checksumSHA1": "HYgTWn4FgVbvSBYVO4DxUPWfCz0=", "path": "github.com/nlopes/slack", - "revision": "6519657c021b7add19c4ef48220140cca0b1657b", - "revisionTime": "2017-02-11T11:26:27Z" + "revision": "5cde21b8b96a43fc3435a1f514123d14fd7eabdc", + "revisionTime": "2017-07-25T12:17:30Z" }, { "checksumSHA1": "2si62NpJ4Rw8vVb7+LOfnKJ3y2Q=", "path": "github.com/nsf/termbox-go", - "revision": "91bae1bb5fa9ee504905ecbe7043fa30e92feaa3", - "revisionTime": "2017-03-01T01:43:43Z" + "revision": "4ed959e0540971545eddb8c75514973d670cf739", + "revisionTime": "2017-07-10T10:34:07Z" }, { "checksumSHA1": "7EZyXN0EmZLgGxZxK01IJua4c8o=", "path": "golang.org/x/net/websocket", - "revision": "906cda9512f77671ab44f8c8563b13a8e707b230", - "revisionTime": "2017-02-18T20:36:51Z" + "revision": "f5079bd7f6f74e23c4d65efa0f4ce14cbd6a3c0f", + "revisionTime": "2017-07-19T21:11:51Z" } ], "rootPath": "github.com/erroneousboat/slack-term"