diff --git a/gim/line.go b/gim/line.go index 272a90f..c7badd5 100644 --- a/gim/line.go +++ b/gim/line.go @@ -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)) diff --git a/gim/terminal/shape.go b/gim/terminal/shape.go index f483841..8981b81 100644 --- a/gim/terminal/shape.go +++ b/gim/terminal/shape.go @@ -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. //