Skip to content

Commit

Permalink
cmd/flatend: initial exploration on tui
Browse files Browse the repository at this point in the history
  • Loading branch information
lithdew committed May 25, 2020
1 parent 7181f48 commit 957e153
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 4 deletions.
198 changes: 198 additions & 0 deletions cmd/flatend/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package main

import (
"github.com/gdamore/tcell"
"github.com/gdamore/tcell/encoding"
"github.com/mattn/go-runewidth"
)

func check(err error) {
if err != nil {
panic(err)
}
}

func text(s tcell.Screen, x, y int, style tcell.Style, txt string) {
for _, c := range txt {
var comb []rune
w := runewidth.RuneWidth(c)
if w == 0 {
comb = []rune{c}
c = ' '
w = 1
}
s.SetContent(x, y, c, comb, style)
x += w
}
}

func box(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, r rune) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}

for col := x1; col <= x2; col++ {
s.SetContent(col, y1, tcell.RuneHLine, nil, style)
s.SetContent(col, y2, tcell.RuneHLine, nil, style)
}
for row := y1 + 1; row < y2; row++ {
s.SetContent(x1, row, tcell.RuneVLine, nil, style)
s.SetContent(x2, row, tcell.RuneVLine, nil, style)
}
if y1 != y2 && x1 != x2 {
// Only add corners if we need to
s.SetContent(x1, y1, tcell.RuneULCorner, nil, style)
s.SetContent(x2, y1, tcell.RuneURCorner, nil, style)
s.SetContent(x1, y2, tcell.RuneLLCorner, nil, style)
s.SetContent(x2, y2, tcell.RuneLRCorner, nil, style)
}
for row := y1 + 1; row < y2; row++ {
for col := x1 + 1; col < x2; col++ {
s.SetContent(col, row, r, nil, style)
}
}
}

type inputtable interface {
keyPress(e *tcell.EventKey)
}

type renderable interface {
render(s tcell.Screen)
}

type widget interface {
renderable
inputtable
}

type alignType int

const (
alignNone alignType = iota
alignTop
alignBottom
)

type textbox struct {
x, y, w, h int
style tcell.Style

align alignType
fw bool
fh bool

buf string
}

func (t *textbox) keyPress(e *tcell.EventKey) {
switch e.Key() {
case tcell.KeyBS, tcell.KeyDEL:
if len(t.buf) > 0 {
t.buf = t.buf[:len(t.buf)-1]
}
default:
t.buf += string(e.Rune())
}

}

func (t *textbox) render(s tcell.Screen) {
x, y, w, h := t.x, t.y, t.w, t.h

sw, sh := s.Size()
if t.fw {
w = sw
}
if t.fh {
h = sh
}
switch t.align {
case alignNone:
case alignTop:
x, y = 0, 0
case alignBottom:
x, y = 0, sh-1-h
}

box(s, x, y, x+w-1, y+h, t.style, ' ')

text(s, x+1, y+1, t.style, t.buf)
}

var focused int
var widgets []widget

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

func eventLoop() {
defer close(ch)

input := &textbox{h: 2, fw: true, align: alignBottom, style: styleWhite}
widgets = append(widgets, input)

for {
for _, w := range widgets {
w.render(s)
}
text(s, 1, 1, styleWhite, "flatend ::")

s.Show()

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

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()

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

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

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

ch = make(chan struct{})
go eventLoop()
<-ch
}
5 changes: 5 additions & 0 deletions flathttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ func (c *Config) Parse() error {
}
c.addrs = append(c.addrs, Addr{Addr: addr, Scheme: u.Scheme, Host: u.Host})
}

if len(c.addrs) == 0 {
c.addrs = append(c.addrs, Addr{Addr: "tcp://:0", Scheme: "tcp", Host: ":0"})
}

return nil
}

Expand Down
4 changes: 0 additions & 4 deletions flathttp/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,6 @@ func (s *Server) init() error {
ReadHeaderTimeout: s.cfg.ReadHeaderTimeout,
}

if len(s.cfg.addrs) == 0 {
s.cfg.addrs = append(s.cfg.addrs, Addr{Addr: "tcp://:0", Scheme: "tcp", Host: ":0"})
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ go 1.14

require (
github.com/davecgh/go-spew v1.1.0
github.com/gdamore/tcell v1.3.0
github.com/julienschmidt/httprouter v1.3.0
github.com/mattn/go-runewidth v0.0.4
github.com/stretchr/testify v1.4.0
github.com/tidwall/gjson v1.6.0 // indirect
github.com/tidwall/sjson v1.1.1
Expand Down
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.3.0 h1:r35w0JBADPZCVQijYebl6YMWWtHRqVEGt7kL2eBADRM=
github.com/gdamore/tcell v1.3.0/go.mod h1:Hjvr+Ofd+gLglo7RYKxxnzCBmev3BzsS67MebKS4zMM=
github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down Expand Up @@ -43,6 +52,9 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756 h1:9nuHUbU8dRnRRfj9KjWUVrJeoexdbeMjttk6Oh1rD10=
golang.org/x/sys v0.0.0-20190626150813-e07cf5db2756/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 h1:NusfzzA6yGQ+ua51ck7E3omNUX/JuqbFSaRGqU8CcLI=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down

0 comments on commit 957e153

Please sign in to comment.