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"
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2018-06-03 08:26:42 +00:00
|
|
|
// TODO this is utterly broken, the terminal.Buffer should be used for this
|
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
|
|
|
}
|
|
|
|
|
2018-06-03 08:26:42 +00:00
|
|
|
func (b box) draw(buf *terminal.Buffer) {
|
|
|
|
bodyBuf := terminal.NewBuffer()
|
|
|
|
bodyBuf.WriteString(b.body)
|
|
|
|
bodyBufRect := geo.Rect{Size: bodyBuf.Size()}
|
2017-11-04 21:29:15 +00:00
|
|
|
|
2018-06-03 08:26:42 +00:00
|
|
|
rect := b.rect()
|
|
|
|
buf.DrawRect(rect, terminal.SingleLine)
|
2017-11-19 21:39:56 +00:00
|
|
|
|
2018-06-03 08:26:42 +00:00
|
|
|
center := rect.Center(rounder)
|
|
|
|
buf.DrawBuffer(bodyBufRect.Centered(center, rounder).TopLeft, bodyBuf)
|
2017-11-04 21:29:15 +00:00
|
|
|
}
|