]> git.lizzy.rs Git - micro.git/blob - internal/display/uiwindow.go
Allow divider customization with divchars option
[micro.git] / internal / display / uiwindow.go
1 package display
2
3 import (
4         "github.com/zyedidia/micro/v2/internal/buffer"
5         "github.com/zyedidia/micro/v2/internal/config"
6         "github.com/zyedidia/micro/v2/internal/screen"
7         "github.com/zyedidia/micro/v2/internal/util"
8         "github.com/zyedidia/micro/v2/internal/views"
9 )
10
11 type UIWindow struct {
12         root *views.Node
13 }
14
15 func NewUIWindow(n *views.Node) *UIWindow {
16         uw := new(UIWindow)
17         uw.root = n
18         return uw
19 }
20
21 func (w *UIWindow) drawNode(n *views.Node) {
22         cs := n.Children()
23         dividerStyle := config.DefStyle
24         if style, ok := config.Colorscheme["divider"]; ok {
25                 dividerStyle = style
26         }
27
28         divchars := config.GetGlobalOption("divchars").(string)
29         if util.CharacterCountInString(divchars) != 2 {
30                 divchars = "|-"
31         }
32
33         divchar, combc, _ := util.DecodeCharacterInString(divchars)
34
35         divreverse := config.GetGlobalOption("divreverse").(bool)
36         if divreverse {
37                 dividerStyle = dividerStyle.Reverse(true)
38         }
39
40         for i, c := range cs {
41                 if c.IsLeaf() && c.Kind == views.STVert {
42                         if i != len(cs)-1 {
43                                 for h := 0; h < c.H; h++ {
44                                         screen.SetContent(c.X+c.W, c.Y+h, divchar, combc, dividerStyle)
45                                 }
46                         }
47                 } else {
48                         w.drawNode(c)
49                 }
50         }
51 }
52
53 func (w *UIWindow) Display() {
54         w.drawNode(w.root)
55 }
56
57 func (w *UIWindow) GetMouseSplitID(vloc buffer.Loc) uint64 {
58         var mouseLoc func(*views.Node) uint64
59         mouseLoc = func(n *views.Node) uint64 {
60                 cs := n.Children()
61                 for i, c := range cs {
62                         if c.Kind == views.STVert {
63                                 if i != len(cs)-1 {
64                                         if vloc.X == c.X+c.W && vloc.Y >= c.Y && vloc.Y < c.Y+c.H {
65                                                 return c.ID()
66                                         }
67                                 }
68                         } else if c.Kind == views.STHoriz {
69                                 if i != len(cs)-1 {
70                                         if vloc.Y == c.Y+c.H-1 && vloc.X >= c.X && vloc.X < c.X+c.W {
71                                                 return c.ID()
72                                         }
73                                 }
74                         }
75                 }
76                 for _, c := range cs {
77                         m := mouseLoc(c)
78                         if m != 0 {
79                                 return m
80                         }
81                 }
82                 return 0
83         }
84         return mouseLoc(w.root)
85 }
86 func (w *UIWindow) Resize(width, height int) {}
87 func (w *UIWindow) SetActive(b bool)         {}