mirror of
https://github.com/mum4k/termdash.git
synced 2025-04-25 13:48:50 +08:00
Self review.
This commit is contained in:
parent
3bfc5b554c
commit
c89ed6042b
@ -49,8 +49,6 @@ func HVLineStyle(ls LineStyle) HVLineOption {
|
||||
}
|
||||
|
||||
// HVLineCellOpts sets options on the cells that contain the line.
|
||||
// Where two lines cross, the cell representing the crossing point inherits
|
||||
// options set on the line that was drawn last.
|
||||
func HVLineCellOpts(cOpts ...cell.Option) HVLineOption {
|
||||
return hVLineOption(func(opts *hVLineOptions) {
|
||||
opts.cellOpts = cOpts
|
||||
@ -65,7 +63,7 @@ type HVLine struct {
|
||||
|
||||
// HVLines draws horizontal or vertical lines. Handles drawing of the correct
|
||||
// characters for locations where any two lines cross (e.g. a corner, a T shape
|
||||
// a cross). Each line must be at least one cell long. Both start and end
|
||||
// or a cross). Each line must be at least one cell long. Both start and end
|
||||
// must be on the same horizontal (same X coordinate) or same vertical (same Y
|
||||
// coordinate) line.
|
||||
func HVLines(c *canvas.Canvas, lines []HVLine, opts ...HVLineOption) error {
|
||||
@ -130,10 +128,6 @@ type hVLine struct {
|
||||
// end is the ending point of the line.
|
||||
end image.Point
|
||||
|
||||
// parts are characters that represent parts of the line of the style
|
||||
// chosen in the options.
|
||||
parts map[linePart]rune
|
||||
|
||||
// mainPart is either parts[vLine] or parts[hLine] depending on whether
|
||||
// this is horizontal or vertical line.
|
||||
mainPart rune
|
||||
@ -143,7 +137,7 @@ type hVLine struct {
|
||||
}
|
||||
|
||||
// newHVLine creates a new hVLine instance.
|
||||
// Swaps start and end iof necessary, so that horizontal drawing is always left
|
||||
// Swaps start and end if necessary, so that horizontal drawing is always left
|
||||
// to right and vertical is always top down.
|
||||
func newHVLine(c *canvas.Canvas, start, end image.Point, opts *hVLineOptions) (*hVLine, error) {
|
||||
if ar := c.Area(); !start.In(ar) || !end.In(ar) {
|
||||
@ -180,7 +174,6 @@ func newHVLine(c *canvas.Canvas, start, end image.Point, opts *hVLineOptions) (*
|
||||
return &hVLine{
|
||||
start: start,
|
||||
end: end,
|
||||
parts: parts,
|
||||
mainPart: mainPart,
|
||||
opts: opts,
|
||||
}, nil
|
||||
@ -188,10 +181,10 @@ func newHVLine(c *canvas.Canvas, start, end image.Point, opts *hVLineOptions) (*
|
||||
|
||||
// horizontal determines if this is a horizontal line.
|
||||
func (hvl *hVLine) horizontal() bool {
|
||||
return hvl.mainPart == hvl.parts[hLine]
|
||||
return hvl.mainPart == lineStyleChars[hvl.opts.lineStyle][hLine]
|
||||
}
|
||||
|
||||
// vertical determines if this is a vertical line.
|
||||
func (hvl *hVLine) vertical() bool {
|
||||
return hvl.mainPart == hvl.parts[vLine]
|
||||
return hvl.mainPart == lineStyleChars[hvl.opts.lineStyle][vLine]
|
||||
}
|
||||
|
@ -23,8 +23,12 @@ import (
|
||||
|
||||
// hVLineEdge is an edge between two points on the graph.
|
||||
type hVLineEdge struct {
|
||||
// from is the starting node of this edge.
|
||||
// From is guaranteed to be less than to.
|
||||
from image.Point
|
||||
to image.Point
|
||||
|
||||
// to is the ending point of this edge.
|
||||
to image.Point
|
||||
}
|
||||
|
||||
// newHVLineEdge returns a new edge between the two points.
|
||||
@ -41,7 +45,9 @@ type hVLineNode struct {
|
||||
// p is the point where this node is.
|
||||
p image.Point
|
||||
|
||||
// edges are the edges from this node to the neighboring nodes.
|
||||
// edges are the edges between this node and the surrounding nodes.
|
||||
// The code only supports horizontal and vertical lines so there can only
|
||||
// ever be edges to nodes on these planes.
|
||||
edges map[hVLineEdge]bool
|
||||
}
|
||||
|
||||
@ -83,22 +89,28 @@ func (n *hVLineNode) hasRight() bool {
|
||||
|
||||
// rune, given the selected line style returns the correct line character to
|
||||
// represent this node.
|
||||
// Only handles nodes with two or more edges, as returned by multiEdgeNodes().
|
||||
func (n *hVLineNode) rune(ls LineStyle) (rune, error) {
|
||||
parts, err := lineParts(ls)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
switch len(n.edges) {
|
||||
case 2:
|
||||
switch {
|
||||
case n.hasLeft() && n.hasRight():
|
||||
return lineStyleChars[ls][hLine], nil
|
||||
return parts[hLine], nil
|
||||
case n.hasUp() && n.hasDown():
|
||||
return lineStyleChars[ls][vLine], nil
|
||||
return parts[vLine], nil
|
||||
case n.hasDown() && n.hasRight():
|
||||
return lineStyleChars[ls][topLeftCorner], nil
|
||||
return parts[topLeftCorner], nil
|
||||
case n.hasDown() && n.hasLeft():
|
||||
return lineStyleChars[ls][topRightCorner], nil
|
||||
return parts[topRightCorner], nil
|
||||
case n.hasUp() && n.hasRight():
|
||||
return lineStyleChars[ls][bottomLeftCorner], nil
|
||||
return parts[bottomLeftCorner], nil
|
||||
case n.hasUp() && n.hasLeft():
|
||||
return lineStyleChars[ls][bottomRightCorner], nil
|
||||
return parts[bottomRightCorner], nil
|
||||
default:
|
||||
return -1, fmt.Errorf("unexpected two edges in node representing point %v: %v", n.p, n.edges)
|
||||
}
|
||||
@ -106,20 +118,20 @@ func (n *hVLineNode) rune(ls LineStyle) (rune, error) {
|
||||
case 3:
|
||||
switch {
|
||||
case n.hasUp() && n.hasLeft() && n.hasRight():
|
||||
return lineStyleChars[ls][hAndUp], nil
|
||||
return parts[hAndUp], nil
|
||||
case n.hasDown() && n.hasLeft() && n.hasRight():
|
||||
return lineStyleChars[ls][hAndDown], nil
|
||||
return parts[hAndDown], nil
|
||||
case n.hasUp() && n.hasDown() && n.hasRight():
|
||||
return lineStyleChars[ls][vAndRight], nil
|
||||
return parts[vAndRight], nil
|
||||
case n.hasUp() && n.hasDown() && n.hasLeft():
|
||||
return lineStyleChars[ls][vAndLeft], nil
|
||||
return parts[vAndLeft], nil
|
||||
|
||||
default:
|
||||
return -1, fmt.Errorf("unexpected three edges in node representing point %v: %v", n.p, n.edges)
|
||||
}
|
||||
|
||||
case 4:
|
||||
return lineStyleChars[ls][vAndH], nil
|
||||
return parts[vAndH], nil
|
||||
default:
|
||||
return -1, fmt.Errorf("unexpected number of edges(%d) in node representing point %v", len(n.edges), n.p)
|
||||
}
|
||||
|
@ -212,6 +212,12 @@ func TestNodeRune(t *testing.T) {
|
||||
ls: LineStyleLight,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
desc: "fails on unsupported line style",
|
||||
node: &hVLineNode{},
|
||||
ls: LineStyle(-1),
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
desc: "horizontal line",
|
||||
node: &hVLineNode{
|
||||
|
@ -53,13 +53,11 @@ func init() {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(mum4k): Check inside init() that all of these are half-width runes.
|
||||
|
||||
// lineParts returns the line component characters for the provided line style.
|
||||
func lineParts(ls LineStyle) (map[linePart]rune, error) {
|
||||
parts, ok := lineStyleChars[ls]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unsupported line style %v", ls)
|
||||
return nil, fmt.Errorf("unsupported line style %d", ls)
|
||||
}
|
||||
return parts, nil
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user