Start with gocui implementation

This commit is contained in:
erroneousboat 2017-08-19 23:31:16 +02:00
parent c88dff2cd2
commit bf8e2a78a6
5 changed files with 128 additions and 43 deletions

View File

@ -0,0 +1,44 @@
package components
import (
"fmt"
"github.com/jroimartin/gocui"
)
type ChannelsNew struct {
Component
Items []string
SelectedChannel int // index of which channel is selected from the Items
Offset int // from what offset are channels rendered
CursorPosition int // the y position of the 'cursor'
}
func CreateChannelsComponent(x, y, w, h int) *ChannelsNew {
channels := &ChannelsNew{}
channels.Name = "channels"
channels.Width = 10
channels.Height = h
return channels
}
// ... and implements the gocui.Manager interface
func (c *ChannelsNew) Layout(g *gocui.Gui) error {
if v, err := g.SetView(c.Name, c.X, c.Y, c.X+c.Width, c.Y+c.Height); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Highlight = true
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
for _, item := range c.Items {
fmt.Fprintln(v, item)
}
}
return nil
}

9
components/components.go Normal file
View File

@ -0,0 +1,9 @@
package components
type Component struct {
Name string
X int
Y int
Width int
Height int
}

13
components/debug.go Normal file
View File

@ -0,0 +1,13 @@
package components
import "github.com/jroimartin/gocui"
type Debug struct {
Component
Text string
}
// ... and implements the gocui.Manager interface
func (d *Debug) Layout(g *gocui.Gui) error {
return nil
}

58
main.go
View File

@ -4,15 +4,12 @@ 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/jroimartin/gocui"
"github.com/gizak/termui"
"github.com/erroneousboat/slack-term/context"
)
const (
@ -59,49 +56,24 @@ func init() {
}
func main() {
// Start terminal user interface
err := termui.Init()
// Create context
appCTX, err := context.CreateAppContext(flgConfig)
if err != nil {
log.Fatal(err)
}
defer termui.Close()
defer appCTX.View.GUI.Close()
// Create custom event stream for termui because
// termui's one has data race conditions with its
// event handling. We're circumventing it here until
// it has been fixed.
customEvtStream := &termui.EvtStream{
Handlers: make(map[string]func(termui.Event)),
}
termui.DefaultEvtStream = customEvtStream
// Create context
ctx, err := context.CreateAppContext(flgConfig)
if err != nil {
termbox.Close()
log.Println(err)
os.Exit(0)
}
// Setup body
termui.Body.AddRows(
termui.NewRow(
termui.NewCol(ctx.Config.SidebarWidth, 0, ctx.View.Channels),
termui.NewCol(ctx.Config.MainWidth, 0, ctx.View.Chat),
),
termui.NewRow(
termui.NewCol(ctx.Config.SidebarWidth, 0, ctx.View.Mode),
termui.NewCol(ctx.Config.MainWidth, 0, ctx.View.Input),
),
)
termui.Body.Align()
termui.Render(termui.Body)
// Set body in context
ctx.Body = termui.Body
// Create the view
// view, err := views.CreateView(ctx)
// if err != nil {
// log.Fatal(err)
// }
// defer view.Close()
// Register handlers
handlers.RegisterEventHandlers(ctx)
// handlers.RegisterEventHandlers(app)
termui.Loop()
if err := appCTX.View.GUI.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Fatal(err)
}
}

View File

@ -1,7 +1,10 @@
package views
import (
"log"
"github.com/gizak/termui"
"github.com/jroimartin/gocui"
"github.com/erroneousboat/slack-term/components"
"github.com/erroneousboat/slack-term/service"
@ -12,13 +15,53 @@ type View struct {
Chat *components.Chat
Channels *components.Channels
Mode *components.Mode
Debug *components.Debug
GUI *gocui.Gui
}
func CreateChatView(svc *service.SlackService) *View {
g, err := gocui.NewGui(gocui.OutputNormal)
if err != nil {
log.Fatal(err)
}
view := &View{
GUI: g,
}
_, maxY := g.Size()
// Channels component
channels := components.CreateChannelsComponent(0, 0, 10, maxY-1)
// view.Channels = channels
// TODO Input component
// TODO Mode component
// TODO Chat component
// TODO Debug
g.SetManager(channels)
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Fatal(err)
}
return view
}
func CreateChatViewBKP(svc *service.SlackService) *View {
input := components.CreateInput()
channels := components.CreateChannels(svc, input.Par.Height)
// svc.GetChannels
// channels.SetChannels
// TODO pass through svc.GetMessages, not svc
chat := components.CreateChat(
svc,
input.Par.Height,
@ -46,3 +89,7 @@ func (v *View) Refresh() {
v.Mode,
)
}
func quit(g *gocui.Gui, v *gocui.View) error {
return gocui.ErrQuit
}