2016-09-11 17:55:19 +02:00
|
|
|
package context
|
|
|
|
|
|
|
|
import (
|
2017-12-02 17:35:27 +01:00
|
|
|
"net/http"
|
|
|
|
_ "net/http/pprof"
|
|
|
|
|
2017-09-23 13:56:45 +02:00
|
|
|
"github.com/erroneousboat/termui"
|
2017-03-17 14:36:29 +01:00
|
|
|
termbox "github.com/nsf/termbox-go"
|
2016-09-25 22:34:02 +02:00
|
|
|
|
2016-10-19 09:08:31 +02:00
|
|
|
"github.com/erroneousboat/slack-term/config"
|
|
|
|
"github.com/erroneousboat/slack-term/service"
|
|
|
|
"github.com/erroneousboat/slack-term/views"
|
2016-09-11 17:55:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
CommandMode = "command"
|
|
|
|
InsertMode = "insert"
|
2017-07-15 23:56:39 +02:00
|
|
|
SearchMode = "search"
|
2016-09-11 17:55:19 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AppContext struct {
|
2017-03-17 14:36:29 +01:00
|
|
|
EventQueue chan termbox.Event
|
|
|
|
Service *service.SlackService
|
|
|
|
Body *termui.Grid
|
|
|
|
View *views.View
|
|
|
|
Config *config.Config
|
2017-12-01 23:52:25 +01:00
|
|
|
Debug bool
|
2017-03-17 14:36:29 +01:00
|
|
|
Mode string
|
2016-09-11 17:55:19 +02:00
|
|
|
}
|
|
|
|
|
2016-10-02 16:07:35 +02:00
|
|
|
// CreateAppContext creates an application context which can be passed
|
|
|
|
// and referenced througout the application
|
2017-12-01 23:52:25 +01:00
|
|
|
func CreateAppContext(flgConfig string, flgDebug bool) (*AppContext, error) {
|
2017-12-02 17:35:27 +01:00
|
|
|
if flgDebug {
|
|
|
|
go func() {
|
|
|
|
http.ListenAndServe(":6060", nil)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2016-09-25 22:34:02 +02:00
|
|
|
// Load config
|
|
|
|
config, err := config.NewConfig(flgConfig)
|
|
|
|
if err != nil {
|
2017-08-04 10:22:42 +02:00
|
|
|
return nil, err
|
2016-09-25 22:34:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create Service
|
2017-12-01 11:30:43 +01:00
|
|
|
svc, err := service.NewSlackService(config.SlackToken)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-25 22:34:02 +02:00
|
|
|
|
2017-12-01 23:52:25 +01:00
|
|
|
// Create the main view
|
|
|
|
view := views.CreateView(svc)
|
|
|
|
|
|
|
|
// Setup the interface
|
|
|
|
if flgDebug {
|
|
|
|
termui.Body.AddRows(
|
|
|
|
termui.NewRow(
|
|
|
|
termui.NewCol(config.SidebarWidth, 0, view.Channels),
|
2017-12-02 11:09:01 +01:00
|
|
|
termui.NewCol(config.MainWidth-5, 0, view.Chat),
|
|
|
|
termui.NewCol(config.MainWidth-6, 0, view.Debug),
|
2017-12-01 23:52:25 +01:00
|
|
|
),
|
|
|
|
termui.NewRow(
|
|
|
|
termui.NewCol(config.SidebarWidth, 0, view.Mode),
|
2017-12-02 11:09:01 +01:00
|
|
|
termui.NewCol(config.MainWidth, 0, view.Input),
|
2017-12-01 23:52:25 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
termui.Body.AddRows(
|
|
|
|
termui.NewRow(
|
|
|
|
termui.NewCol(config.SidebarWidth, 0, view.Channels),
|
|
|
|
termui.NewCol(config.MainWidth, 0, view.Chat),
|
|
|
|
),
|
|
|
|
termui.NewRow(
|
|
|
|
termui.NewCol(config.SidebarWidth, 0, view.Mode),
|
|
|
|
termui.NewCol(config.MainWidth, 0, view.Input),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
termui.Body.Align()
|
|
|
|
termui.Render(termui.Body)
|
2016-09-11 17:55:19 +02:00
|
|
|
|
|
|
|
return &AppContext{
|
2017-03-17 14:36:29 +01:00
|
|
|
EventQueue: make(chan termbox.Event, 20),
|
|
|
|
Service: svc,
|
2017-12-01 23:52:25 +01:00
|
|
|
Body: termui.Body,
|
2017-03-17 14:36:29 +01:00
|
|
|
View: view,
|
|
|
|
Config: config,
|
2017-12-01 23:52:25 +01:00
|
|
|
Debug: flgDebug,
|
2017-03-17 14:36:29 +01:00
|
|
|
Mode: CommandMode,
|
2017-08-04 10:22:42 +02:00
|
|
|
}, nil
|
2016-09-11 17:55:19 +02:00
|
|
|
}
|