gim: give terminal.LineStyle Perp and Arrow fields

This commit is contained in:
Brian Picciano 2018-06-06 06:30:39 +00:00
parent 97be10b03e
commit efa7319555
2 changed files with 51 additions and 18 deletions

View File

@ -5,22 +5,6 @@ import (
"github.com/mediocregopher/ginger/gim/terminal"
)
var edgeSegments = map[geo.XY]rune{
geo.Up: '┴',
geo.Down: '┬',
geo.Left: '┤',
geo.Right: '├',
}
// actual unicode arrows were fucking up my terminal, and they didn't even
// connect properly with the line segments anyway
var arrows = map[geo.XY]rune{
geo.Up: '^',
geo.Down: 'v',
geo.Left: '<',
geo.Right: '>',
}
type line struct {
from, to *box
fromI, toI int
@ -71,9 +55,9 @@ func (l line) draw(buf *terminal.Buffer, flowDir, secFlowDir geo.XY) {
var r rune
switch {
case i == 0:
r = edgeSegments[flowDir]
r = terminal.SingleLine.Perpendicular(flowDir)
case i == len(pts)-1:
r = arrows[flowDir]
r = terminal.SingleLine.Arrow(flowDir)
default:
prev, next := pts[i-1], pts[i+1]
r = terminal.SingleLine.Segment(prev.Sub(pt), next.Sub(pt))

View File

@ -15,14 +15,29 @@ var SingleLine = LineStyle{
TopRight: '┐',
BottomLeft: '└',
BottomRight: '┘',
PerpUp: '┴',
PerpDown: '┬',
PerpLeft: '┤',
PerpRight: '├',
ArrowUp: '^',
ArrowDown: 'v',
ArrowLeft: '<',
ArrowRight: '>',
}
// LineStyle defines a set of characters to use together when drawing lines and
// corners.
type LineStyle struct {
Horiz, Vert rune
// Corner characters, identified as corners of a rectangle
TopLeft, TopRight, BottomLeft, BottomRight rune
// Characters for a straight segment a perpendicular attached
PerpUp, PerpDown, PerpLeft, PerpRight rune
// Characters for pointing arrows
ArrowUp, ArrowDown, ArrowLeft, ArrowRight rune
}
// Segment takes two different directions (i.e. geo.Up/Down/Left/Right) and
@ -57,6 +72,40 @@ func (ls LineStyle) Segment(a, b geo.XY) rune {
panic(fmt.Sprintf("invalid LineStyle.Segment directions: %v, %v", a, b))
}
// Perpendicular returns the line character for a perpendicular segment
// traveling in the given direction.
func (ls LineStyle) Perpendicular(dir geo.XY) rune {
switch dir {
case geo.Up:
return ls.PerpUp
case geo.Down:
return ls.PerpDown
case geo.Left:
return ls.PerpLeft
case geo.Right:
return ls.PerpRight
default:
panic(fmt.Sprintf("invalid LineStyle.Perpendicular direction: %v", dir))
}
}
// Arrow returns the arrow character for an arrow pointing in the given
// direction.
func (ls LineStyle) Arrow(dir geo.XY) rune {
switch dir {
case geo.Up:
return ls.ArrowUp
case geo.Down:
return ls.ArrowDown
case geo.Left:
return ls.ArrowLeft
case geo.Right:
return ls.ArrowRight
default:
panic(fmt.Sprintf("invalid LineStyle.Arrow direction: %v", dir))
}
}
// DrawRect draws the given Rect to the Buffer with the given LineStyle. The
// Rect's TopLeft field is used for its position.
//