94 lines
2.0 KiB
Go
Raw Normal View History

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"
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 {
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
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 {
return nil, err
2016-09-25 22:34:02 +02:00
}
// Create Service
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{
EventQueue: make(chan termbox.Event, 20),
Service: svc,
2017-12-01 23:52:25 +01:00
Body: termui.Body,
View: view,
Config: config,
2017-12-01 23:52:25 +01:00
Debug: flgDebug,
Mode: CommandMode,
}, nil
2016-09-11 17:55:19 +02:00
}