Skip to content

Commit

Permalink
cmd/flatend: migrating ui from blanc
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed Jun 4, 2020
1 parent 9da2c63 commit 601a42b
Show file tree
Hide file tree
Showing 8 changed files with 377 additions and 246 deletions.
50 changes: 0 additions & 50 deletions cmd/flatend/draw.go

This file was deleted.

122 changes: 68 additions & 54 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,87 +3,101 @@ package main
import (
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/encoding"
"github.com/lithdew/blanc/layout"
"log"
"time"
)

var err error

var screen tcell.Screen
var screenRect layout.Rect

func check(err error) {
if err != nil {
panic(err)
if screen != nil {
screen.Fini()
}
log.Panic(err)
}
}

var focused int
var widgets []widget
func init() {
encoding.Register()

func currentFocus() widget {
if focused == len(widgets) {
return nil
}
return widgets[focused]
initScreen()
initHeader()
initBody()
initFooter()
}

func eventLoop() {
defer close(ch)
func initScreen() {
screen, err = tcell.NewScreen()
check(err)
}

input := &textbox{h: 2, tpx: 1, tpy: 1, fw: true, ba: alignBottom, bs: styleWhite}
widgets = append(widgets, input)
func resizeScreen() {
screen.Sync()

for {
for _, w := range widgets {
w.render(s)
}
w, h := screen.Size()
screenRect = screenRect.Width(w)
screenRect = screenRect.Height(h)

text(s, 1, 1, styleWhite, "flatend ::")
resizeHeader()
resizeBody()
resizeFooter()
}

s.Show()
func renderScreen() {
renderHeader()
renderBody()
renderFooter()

switch e := s.PollEvent().(type) {
case *tcell.EventResize:
s.Clear()
s.Sync()
screen.Show()
}

var ch chan struct{}

func eventLoop() {
defer close(ch)
for {
e := screen.PollEvent()
switch e := e.(type) {
case *tcell.EventKey:
switch e.Key() {
case tcell.KeyTAB:
focused = (focused + 1) % (len(widgets) + 1)
case tcell.KeyEscape, tcell.KeyExit, tcell.KeyCtrlC:
case tcell.KeyCtrlC:
return
case tcell.KeyCtrlL:
s.Clear()
s.Sync()
default:
w := currentFocus()
if w != nil {
w.keyPress(e)
}
resizeScreen()
}
case *tcell.EventResize:
resizeScreen()
}

if footerInput.HandleEvent(e) {
//if footerInput.Text() == "http" {
// fmt.Println("ready")
//}
}
}
}

var (
styleDefault = tcell.StyleDefault.Foreground(tcell.ColorBlack)
styleWhite = tcell.StyleDefault.Foreground(tcell.ColorWhite)
)

var (
s tcell.Screen
err error
ch chan struct{}
)

func main() {
encoding.Register()
check(screen.Init())
defer screen.Fini()

s, err = tcell.NewScreen()
check(err)

check(s.Init())
defer s.Fini()

s.SetStyle(styleDefault)
s.EnableMouse()
s.Clear()
resizeScreen()

ch = make(chan struct{})
go eventLoop()
<-ch

for {
select {
case <-ch:
return
case <-time.After(40 * time.Millisecond):
}

renderScreen()
}
}
152 changes: 152 additions & 0 deletions cmd/flatend/section_body.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package main

import (
"github.com/gdamore/tcell"
"github.com/lithdew/blanc"
"github.com/lithdew/blanc/layout"
"github.com/mattn/go-runewidth"
"strconv"
)

var bodyStyle tcell.Style
var bodyRect layout.Rect

type ListItem struct {
ID int
Type string
Status string
Params string
}

var list struct {
items []ListItem
selected int

idRect layout.Rect
typeRect layout.Rect
statusRect layout.Rect
paramsRect layout.Rect
}

func initBody() {
bodyStyle = tcell.StyleDefault
bodyStyle = bodyStyle.Background(tcell.ColorBlack)
bodyStyle = bodyStyle.Foreground(tcell.ColorWhite)

// example data

list.items = []ListItem{
{ID: 0, Type: "http", Status: "ready", Params: "9000"},
{ID: 1, Type: "post", Status: "ready", Params: "/post/new"},
{ID: 2, Type: "get", Status: "ready", Params: "/post/:id"},
{ID: 3, Type: "jsonrpc", Status: "ready", Params: "hello_world"},
{ID: 4, Type: "cron", Status: "ready", Params: "@every 10s"},
{ID: 5, Type: "sql", Status: "ready", Params: "select * from posts where id = :id"},
{ID: 6, Type: "csv", Status: "ready", Params: "/home/kenta/Desktop/database.csv"},
}

list.selected = 4
}

func resizeBody() {
bodyRect = screenRect.PadVertical(1)

for i := 0; i < len(list.items); i++ {
item := list.items[i]

if idWidth := runewidth.StringWidth(strconv.FormatInt(int64(item.ID), 10)); list.idRect.W < idWidth {
list.idRect.W = idWidth
}

if typeWidth := runewidth.StringWidth(item.Type); list.typeRect.W < typeWidth {
list.typeRect.W = typeWidth
}

if statusWidth := runewidth.StringWidth(item.Status); list.statusRect.W < statusWidth {
list.statusRect.W = statusWidth
}

if paramsWidth := runewidth.StringWidth(item.Params); list.paramsRect.W < paramsWidth {
list.paramsRect.W = paramsWidth
}
}

switch {
case list.idRect.W > 8:
list.idRect.W = 8
case list.idRect.W < 6:
list.idRect.W = 6
}

switch {
case list.typeRect.W > 16:
list.typeRect.W = 16
case list.typeRect.W < 10:
list.typeRect.W = 10
}

switch {
case list.statusRect.W > 16:
list.statusRect.W = 16
case list.statusRect.W < 10:
list.statusRect.W = 10
}

switch {
case list.paramsRect.W < 16:
list.paramsRect.W = 16
}

list.idRect.X = 2
list.typeRect = list.typeRect.AlignTo(list.idRect, layout.Right)
list.statusRect = list.statusRect.AlignTo(list.typeRect, layout.Right)
list.paramsRect = list.paramsRect.AlignTo(list.statusRect, layout.Right)

list.idRect.Y = bodyRect.Y + 1
list.typeRect.Y = bodyRect.Y + 1
list.statusRect.Y = bodyRect.Y + 1
list.paramsRect.Y = bodyRect.Y + 1
}

func renderBody() {
blanc.Clear(screen, bodyStyle, bodyRect)

var col blanc.Text
col.SetStyle(bodyStyle.Underline(true))

col.SetText("ID")
col.Draw(screen, list.idRect)

col.SetText("TYPE")
col.Draw(screen, list.typeRect)

col.SetText("STATUS")
col.Draw(screen, list.statusRect)

col.SetText("PARAMS")
col.Draw(screen, list.paramsRect)

for i := 0; i < len(list.items); i++ {
item := list.items[i]

if i == list.selected {
selectedStyle := tcell.StyleDefault.Background(tcell.ColorTeal).Foreground(tcell.ColorBlack)
blanc.Clear(screen, selectedStyle, layout.Rect{X: 0, Y: bodyRect.Y + 1 + i + 1, W: screenRect.W, H: 1})
col.SetStyle(selectedStyle)
} else {
col.SetStyle(bodyStyle)
}

col.SetText(strconv.FormatInt(int64(item.ID), 10))
col.Draw(screen, list.idRect.MoveDown(i+1))

col.SetText(item.Type)
col.Draw(screen, list.typeRect.MoveDown(i+1))

col.SetText(item.Status)
col.Draw(screen, list.statusRect.MoveDown(i+1))

col.SetText(item.Params)
col.Draw(screen, list.paramsRect.MoveDown(i+1))
}
}
Loading

0 comments on commit 601a42b

Please sign in to comment.