2017-11-04 21:29:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
"github.com/mediocregopher/ginger/gg"
|
2017-11-04 21:29:15 +00:00
|
|
|
"github.com/mediocregopher/ginger/gim/geo"
|
|
|
|
"github.com/mediocregopher/ginger/gim/terminal"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
boxBorderHoriz = iota
|
|
|
|
boxBorderVert
|
|
|
|
boxBorderTL
|
|
|
|
boxBorderTR
|
|
|
|
boxBorderBL
|
|
|
|
boxBorderBR
|
|
|
|
)
|
|
|
|
|
|
|
|
var boxDefault = []string{
|
|
|
|
"─",
|
|
|
|
"│",
|
|
|
|
"┌",
|
|
|
|
"┐",
|
|
|
|
"└",
|
|
|
|
"┘",
|
|
|
|
}
|
|
|
|
|
|
|
|
type box struct {
|
2017-11-19 21:39:56 +00:00
|
|
|
topLeft geo.XY
|
|
|
|
flowDir geo.XY
|
|
|
|
numIn, numOut int
|
|
|
|
body string
|
2017-11-04 21:29:15 +00:00
|
|
|
|
|
|
|
transparent bool
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
func boxFromVertex(v *gg.Vertex, flowDir geo.XY) box {
|
|
|
|
b := box{
|
|
|
|
flowDir: flowDir,
|
|
|
|
numIn: len(v.In),
|
|
|
|
numOut: len(v.Out),
|
|
|
|
}
|
2018-01-21 15:39:25 +00:00
|
|
|
if v.VertexType == gg.ValueVertex {
|
|
|
|
b.body = v.Value.V.(string)
|
2017-11-19 21:39:56 +00:00
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b box) bodyLines() []string {
|
2017-11-04 21:29:15 +00:00
|
|
|
lines := strings.Split(b.body, "\n")
|
|
|
|
// if the last line is empty don't include it, it means there was a trailing
|
|
|
|
// newline (or the whole string is empty)
|
|
|
|
if lines[len(lines)-1] == "" {
|
|
|
|
lines = lines[:len(lines)-1]
|
|
|
|
}
|
|
|
|
return lines
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
func (b box) bodySize() geo.XY {
|
2017-11-04 21:29:15 +00:00
|
|
|
var size geo.XY
|
2017-11-19 21:39:56 +00:00
|
|
|
for _, line := range b.bodyLines() {
|
2017-11-04 21:29:15 +00:00
|
|
|
size[1]++
|
|
|
|
if l := len(line); l > size[0] {
|
|
|
|
size[0] = l
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 21:39:56 +00:00
|
|
|
|
2017-11-04 21:29:15 +00:00
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
func (b box) rect() geo.Rect {
|
|
|
|
bodyRect := geo.Rect{
|
|
|
|
Size: b.bodySize().Add(geo.XY{2, 2}),
|
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
var edgesRect geo.Rect
|
|
|
|
{
|
|
|
|
var neededByEdges int
|
|
|
|
if b.numIn > b.numOut {
|
|
|
|
neededByEdges = b.numIn*2 + 1
|
|
|
|
} else {
|
|
|
|
neededByEdges = b.numOut*2 + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
switch b.flowDir {
|
|
|
|
case geo.Left, geo.Right:
|
|
|
|
edgesRect.Size = geo.XY{2, neededByEdges}
|
2017-12-03 19:38:53 +00:00
|
|
|
case geo.Up, geo.Down:
|
|
|
|
edgesRect.Size = geo.XY{neededByEdges, 2}
|
2017-11-19 21:39:56 +00:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown flowDir: %#v", b.flowDir))
|
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
2017-11-19 21:39:56 +00:00
|
|
|
|
|
|
|
return bodyRect.Union(edgesRect).Translate(b.topLeft)
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
func (b box) bodyRect() geo.Rect {
|
|
|
|
center := b.rect().Center(rounder)
|
|
|
|
return geo.Rect{Size: b.bodySize()}.Centered(center, rounder)
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b box) draw(term *terminal.Terminal) {
|
|
|
|
chars := boxDefault
|
2017-11-19 21:39:56 +00:00
|
|
|
rect := b.rect()
|
|
|
|
pos := rect.TopLeft
|
|
|
|
w, h := rect.Size[0], rect.Size[1]
|
2017-11-04 21:29:15 +00:00
|
|
|
|
|
|
|
// draw top line
|
|
|
|
term.MoveCursorTo(pos)
|
|
|
|
term.Printf(chars[boxBorderTL])
|
2017-11-19 21:39:56 +00:00
|
|
|
for i := 0; i < w-2; i++ {
|
2017-11-04 21:29:15 +00:00
|
|
|
term.Printf(chars[boxBorderHoriz])
|
|
|
|
}
|
|
|
|
term.Printf(chars[boxBorderTR])
|
2017-11-19 21:39:56 +00:00
|
|
|
pos[1]++
|
2017-11-04 21:29:15 +00:00
|
|
|
|
2017-11-19 21:39:56 +00:00
|
|
|
// draw vertical lines
|
|
|
|
for i := 0; i < h-2; i++ {
|
2017-11-04 21:29:15 +00:00
|
|
|
term.MoveCursorTo(pos)
|
|
|
|
term.Printf(chars[boxBorderVert])
|
|
|
|
if b.transparent {
|
2017-11-19 21:39:56 +00:00
|
|
|
term.MoveCursorTo(pos.Add(geo.XY{w, 0}))
|
2017-11-04 21:29:15 +00:00
|
|
|
} else {
|
2017-11-19 21:39:56 +00:00
|
|
|
term.Printf(strings.Repeat(" ", w-2))
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
term.Printf(chars[boxBorderVert])
|
2017-11-19 21:39:56 +00:00
|
|
|
pos[1]++
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// draw bottom line
|
|
|
|
term.MoveCursorTo(pos)
|
|
|
|
term.Printf(chars[boxBorderBL])
|
2017-11-19 21:39:56 +00:00
|
|
|
for i := 0; i < w-2; i++ {
|
2017-11-04 21:29:15 +00:00
|
|
|
term.Printf(chars[boxBorderHoriz])
|
|
|
|
}
|
|
|
|
term.Printf(chars[boxBorderBR])
|
2017-11-19 21:39:56 +00:00
|
|
|
|
|
|
|
// write out inner lines
|
|
|
|
pos = b.bodyRect().TopLeft
|
|
|
|
for _, line := range b.bodyLines() {
|
|
|
|
term.MoveCursorTo(pos)
|
|
|
|
term.Printf(line)
|
|
|
|
pos[1]++
|
|
|
|
}
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|