diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2019-03-29 02:11:03 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2019-03-29 02:11:03 +0900 |
commit | 75972d59a8c772028bd6c57bc1c18c993bf1967a (patch) | |
tree | 11b7da79f873afdb2665723053f01244a64fb14a /src/tui | |
parent | e7d60aac9cb7bd3c98d8ffd544f653278313bb56 (diff) |
Add --no-unicode option to draw borders in ASCII characters
Close ##1533
Diffstat (limited to 'src/tui')
-rw-r--r-- | src/tui/light.go | 26 | ||||
-rw-r--r-- | src/tui/tcell.go | 32 | ||||
-rw-r--r-- | src/tui/tui.go | 37 |
3 files changed, 65 insertions, 30 deletions
diff --git a/src/tui/light.go b/src/tui/light.go index edeb621c..2c2dc004 100644 --- a/src/tui/light.go +++ b/src/tui/light.go @@ -163,9 +163,9 @@ func (r *LightRenderer) findOffset() (row int, col int) { return -1, -1 } -func repeat(s string, times int) string { +func repeat(r rune, times int) string { if times > 0 { - return strings.Repeat(s, times) + return strings.Repeat(string(r), times) } return "" } @@ -676,7 +676,7 @@ func (r *LightRenderer) NewWindow(top int, left int, width int, height int, bord } func (w *LightWindow) drawBorder() { - switch w.border { + switch w.border.shape { case BorderAround: w.drawBorderAround() case BorderHorizontal: @@ -686,22 +686,24 @@ func (w *LightWindow) drawBorder() { func (w *LightWindow) drawBorderHorizontal() { w.Move(0, 0) - w.CPrint(ColBorder, AttrRegular, repeat("─", w.width)) + w.CPrint(ColBorder, AttrRegular, repeat(w.border.horizontal, w.width)) w.Move(w.height-1, 0) - w.CPrint(ColBorder, AttrRegular, repeat("─", w.width)) + w.CPrint(ColBorder, AttrRegular, repeat(w.border.horizontal, w.width)) } func (w *LightWindow) drawBorderAround() { w.Move(0, 0) - w.CPrint(ColBorder, AttrRegular, "┌"+repeat("─", w.width-2)+"┐") + w.CPrint(ColBorder, AttrRegular, + string(w.border.topLeft)+repeat(w.border.horizontal, w.width-2)+string(w.border.topRight)) for y := 1; y < w.height-1; y++ { w.Move(y, 0) - w.CPrint(ColBorder, AttrRegular, "│") - w.cprint2(colDefault, w.bg, AttrRegular, repeat(" ", w.width-2)) - w.CPrint(ColBorder, AttrRegular, "│") + w.CPrint(ColBorder, AttrRegular, string(w.border.vertical)) + w.cprint2(colDefault, w.bg, AttrRegular, repeat(' ', w.width-2)) + w.CPrint(ColBorder, AttrRegular, string(w.border.vertical)) } w.Move(w.height-1, 0) - w.CPrint(ColBorder, AttrRegular, "└"+repeat("─", w.width-2)+"┘") + w.CPrint(ColBorder, AttrRegular, + string(w.border.bottomLeft)+repeat(w.border.horizontal, w.width-2)+string(w.border.bottomRight)) } func (w *LightWindow) csi(code string) { @@ -762,7 +764,7 @@ func (w *LightWindow) MoveAndClear(y int, x int) { w.Move(y, x) // We should not delete preview window on the right // csi("K") - w.Print(repeat(" ", w.width-x)) + w.Print(repeat(' ', w.width-x)) w.Move(y, x) } @@ -858,7 +860,7 @@ func wrapLine(input string, prefixLength int, max int, tabstop int) []wrappedLin width += w str := string(r) if r == '\t' { - str = repeat(" ", w) + str = repeat(' ', w) } if prefixLength+width <= max { line += str diff --git a/src/tui/tcell.go b/src/tui/tcell.go index d337385c..098e8a18 100644 --- a/src/tui/tcell.go +++ b/src/tui/tcell.go @@ -61,12 +61,8 @@ func (w *TcellWindow) Refresh() { } w.lastX = 0 w.lastY = 0 - switch w.borderStyle { - case BorderAround: - w.drawBorder(true) - case BorderHorizontal: - w.drawBorder(false) - } + + w.drawBorder() } func (w *TcellWindow) FinishFill() { @@ -570,7 +566,11 @@ func (w *TcellWindow) CFill(fg Color, bg Color, a Attr, str string) FillReturn { return w.fillString(str, NewColorPair(fg, bg), a) } -func (w *TcellWindow) drawBorder(around bool) { +func (w *TcellWindow) drawBorder() { + if w.borderStyle.shape == BorderNone { + return + } + left := w.left right := left + w.width top := w.top @@ -584,19 +584,19 @@ func (w *TcellWindow) drawBorder(around bool) { } for x := left; x < right; x++ { - _screen.SetContent(x, top, tcell.RuneHLine, nil, style) - _screen.SetContent(x, bot-1, tcell.RuneHLine, nil, style) + _screen.SetContent(x, top, w.borderStyle.horizontal, nil, style) + _screen.SetContent(x, bot-1, w.borderStyle.horizontal, nil, style) } - if around { + if w.borderStyle.shape == BorderAround { for y := top; y < bot; y++ { - _screen.SetContent(left, y, tcell.RuneVLine, nil, style) - _screen.SetContent(right-1, y, tcell.RuneVLine, nil, style) + _screen.SetContent(left, y, w.borderStyle.vertical, nil, style) + _screen.SetContent(right-1, y, w.borderStyle.vertical, nil, style) } - _screen.SetContent(left, top, tcell.RuneULCorner, nil, style) - _screen.SetContent(right-1, top, tcell.RuneURCorner, nil, style) - _screen.SetContent(left, bot-1, tcell.RuneLLCorner, nil, style) - _screen.SetContent(right-1, bot-1, tcell.RuneLRCorner, nil, style) + _screen.SetContent(left, top, w.borderStyle.topLeft, nil, style) + _screen.SetContent(right-1, top, w.borderStyle.topRight, nil, style) + _screen.SetContent(left, bot-1, w.borderStyle.bottomLeft, nil, style) + _screen.SetContent(right-1, bot-1, w.borderStyle.bottomRight, nil, style) } } diff --git a/src/tui/tui.go b/src/tui/tui.go index 5d0035d7..be5bd26a 100644 --- a/src/tui/tui.go +++ b/src/tui/tui.go @@ -201,14 +201,47 @@ type MouseEvent struct { Mod bool } -type BorderStyle int +type BorderShape int const ( - BorderNone BorderStyle = iota + BorderNone BorderShape = iota BorderAround BorderHorizontal ) +type BorderStyle struct { + shape BorderShape + horizontal rune + vertical rune + topLeft rune + topRight rune + bottomLeft rune + bottomRight rune +} + +func MakeBorderStyle(shape BorderShape, unicode bool) BorderStyle { + if unicode { + return BorderStyle{ + shape: shape, + horizontal: '─', + vertical: '│', + topLeft: '┌', + topRight: '┐', + bottomLeft: '└', + bottomRight: '┘', + } + } + return BorderStyle{ + shape: shape, + horizontal: '-', + vertical: '|', + topLeft: '+', + topRight: '+', + bottomLeft: '+', + bottomRight: '+', + } +} + type Renderer interface { Init() Pause(clear bool) |