Skip to content

Commit 7a464ae

Browse files
committed
add graph algorithm
1 parent 927ee63 commit 7a464ae

File tree

6 files changed

+1083
-5
lines changed

6 files changed

+1083
-5
lines changed

pkg/gui/filetree/file_manager_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"github.com/xo/terminfo"
1010
)
1111

12-
func TestRender(t *testing.T) {
12+
func init() {
1313
color.ForceSetColorLevel(terminfo.ColorLevelNone)
14+
}
1415

16+
func TestRender(t *testing.T) {
1517
scenarios := []struct {
1618
name string
1719
root *FileNode

pkg/gui/presentation/graph/cell.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package graph
2+
3+
import (
4+
"github.com/jesseduffield/lazygit/pkg/gui/style"
5+
)
6+
7+
const mergeSymbol = '⏣'
8+
const commitSymbol = '⎔'
9+
10+
type cellType int
11+
12+
const (
13+
CONNECTION cellType = iota
14+
COMMIT
15+
MERGE
16+
)
17+
18+
type Cell struct {
19+
up, down, left, right bool
20+
cellType cellType
21+
rightStyle *style.TextStyle
22+
style style.TextStyle
23+
}
24+
25+
func (cell *Cell) render() string {
26+
up, down, left, right := cell.up, cell.down, cell.left, cell.right
27+
28+
first, second := getBoxDrawingChars(up, down, left, right)
29+
var adjustedFirst rune
30+
switch cell.cellType {
31+
case CONNECTION:
32+
adjustedFirst = first
33+
case COMMIT:
34+
adjustedFirst = commitSymbol
35+
case MERGE:
36+
adjustedFirst = mergeSymbol
37+
}
38+
39+
var rightStyle *style.TextStyle
40+
if cell.rightStyle == nil {
41+
rightStyle = &cell.style
42+
} else {
43+
rightStyle = cell.rightStyle
44+
}
45+
46+
// just doing this for the sake of easy testing, so that we don't need to
47+
// assert on the style of a space given a space has no styling (assuming we
48+
// stick to only using foreground styles)
49+
var styledSecondChar string
50+
if second == ' ' {
51+
styledSecondChar = " "
52+
} else {
53+
styledSecondChar = rightStyle.Sprint(string(second))
54+
}
55+
56+
return cell.style.Sprint(string(adjustedFirst)) + styledSecondChar
57+
}
58+
59+
func (cell *Cell) reset() {
60+
cell.up = false
61+
cell.down = false
62+
cell.left = false
63+
cell.right = false
64+
}
65+
66+
func (cell *Cell) setUp(style style.TextStyle) *Cell {
67+
cell.up = true
68+
cell.style = style
69+
return cell
70+
}
71+
72+
func (cell *Cell) setDown(style style.TextStyle) *Cell {
73+
cell.down = true
74+
cell.style = style
75+
return cell
76+
}
77+
78+
func (cell *Cell) setLeft(style style.TextStyle) *Cell {
79+
cell.left = true
80+
if !cell.up && !cell.down {
81+
// vertical trumps left
82+
cell.style = style
83+
}
84+
return cell
85+
}
86+
87+
func (cell *Cell) setRight(style style.TextStyle, override bool) *Cell {
88+
cell.right = true
89+
if cell.rightStyle == nil || override {
90+
cell.rightStyle = &style
91+
}
92+
return cell
93+
}
94+
95+
func (cell *Cell) setStyle(style style.TextStyle) *Cell {
96+
cell.style = style
97+
return cell
98+
}
99+
100+
func (cell *Cell) setType(cellType cellType) *Cell {
101+
cell.cellType = cellType
102+
return cell
103+
}
104+
105+
func getBoxDrawingChars(up, down, left, right bool) (rune, rune) {
106+
if up && down && left && right {
107+
return '│', '─'
108+
} else if up && down && left && !right {
109+
return '│', ' '
110+
} else if up && down && !left && right {
111+
return '│', '─'
112+
} else if up && down && !left && !right {
113+
return '│', ' '
114+
} else if up && !down && left && right {
115+
return '┴', '─'
116+
} else if up && !down && left && !right {
117+
return '╯', ' '
118+
} else if up && !down && !left && right {
119+
return '╰', '─'
120+
} else if up && !down && !left && !right {
121+
return '╵', ' '
122+
} else if !up && down && left && right {
123+
return '┬', '─'
124+
} else if !up && down && left && !right {
125+
return '╮', ' '
126+
} else if !up && down && !left && right {
127+
return '╭', '─'
128+
} else if !up && down && !left && !right {
129+
return '╷', ' '
130+
} else if !up && !down && left && right {
131+
return '─', '─'
132+
} else if !up && !down && left && !right {
133+
return '─', ' '
134+
} else if !up && !down && !left && right {
135+
return '╶', '─'
136+
} else if !up && !down && !left && !right {
137+
return ' ', ' '
138+
} else {
139+
panic("should not be possible")
140+
}
141+
}

0 commit comments

Comments
 (0)