]> git.lizzy.rs Git - micro.git/blob - cmd/micro/display/infowindow.go
Split improvements
[micro.git] / cmd / micro / display / infowindow.go
1 package display
2
3 import (
4         "unicode/utf8"
5
6         runewidth "github.com/mattn/go-runewidth"
7         "github.com/zyedidia/micro/cmd/micro/buffer"
8         "github.com/zyedidia/micro/cmd/micro/config"
9         "github.com/zyedidia/micro/cmd/micro/info"
10         "github.com/zyedidia/micro/cmd/micro/screen"
11         "github.com/zyedidia/micro/cmd/micro/util"
12         "github.com/zyedidia/tcell"
13 )
14
15 type InfoWindow struct {
16         *info.InfoBuf
17         *View
18
19         defStyle tcell.Style
20         errStyle tcell.Style
21
22         width int
23         y     int
24 }
25
26 func NewInfoWindow(b *info.InfoBuf) *InfoWindow {
27         iw := new(InfoWindow)
28         iw.InfoBuf = b
29         iw.View = new(View)
30
31         iw.defStyle = config.DefStyle
32
33         if _, ok := config.Colorscheme["message"]; ok {
34                 iw.defStyle = config.Colorscheme["message"]
35         }
36
37         iw.errStyle = config.DefStyle.
38                 Foreground(tcell.ColorBlack).
39                 Background(tcell.ColorMaroon)
40
41         if _, ok := config.Colorscheme["error-message"]; ok {
42                 iw.errStyle = config.Colorscheme["error-message"]
43         }
44
45         iw.width, iw.y = screen.Screen.Size()
46         iw.y--
47
48         return iw
49 }
50
51 func (i *InfoWindow) Resize(w, h int) {
52         i.width = w
53 }
54
55 // func (i *InfoWindow) YesNoPrompt() (bool, bool) {
56 //      for {
57 //              i.Clear()
58 //              i.Display()
59 //              screen.Screen.ShowCursor(utf8.RuneCountInString(i.Msg), i.y)
60 //              screen.Show()
61 //              event := <-events
62 //
63 //              switch e := event.(type) {
64 //              case *tcell.EventKey:
65 //                      switch e.Key() {
66 //                      case tcell.KeyRune:
67 //                              if e.Rune() == 'y' || e.Rune() == 'Y' {
68 //                                      i.HasPrompt = false
69 //                                      return true, false
70 //                              } else if e.Rune() == 'n' || e.Rune() == 'N' {
71 //                                      i.HasPrompt = false
72 //                                      return false, false
73 //                              }
74 //                      case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
75 //                              i.Clear()
76 //                              i.Reset()
77 //                              i.HasPrompt = false
78 //                              return false, true
79 //                      }
80 //              }
81 //      }
82 // }
83
84 func (i *InfoWindow) Relocate() bool   { return false }
85 func (i *InfoWindow) GetView() *View   { return i.View }
86 func (i *InfoWindow) SetView(v *View)  {}
87 func (i *InfoWindow) SetActive(b bool) {}
88
89 func (i *InfoWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc {
90         c := i.Buffer.GetActiveCursor()
91         l := i.Buffer.LineBytes(0)
92         n := utf8.RuneCountInString(i.Msg)
93         return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0}
94 }
95
96 func (i *InfoWindow) Clear() {
97         for x := 0; x < i.width; x++ {
98                 screen.Screen.SetContent(x, i.y, ' ', nil, config.DefStyle)
99         }
100 }
101
102 func (i *InfoWindow) displayBuffer() {
103         b := i.Buffer
104         line := b.LineBytes(0)
105         activeC := b.GetActiveCursor()
106
107         blocX := 0
108         vlocX := utf8.RuneCountInString(i.Msg)
109
110         tabsize := 4
111         line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize)
112         blocX = bslice
113
114         draw := func(r rune, style tcell.Style) {
115                 if nColsBeforeStart <= 0 {
116                         bloc := buffer.Loc{X: blocX, Y: 0}
117                         if activeC.HasSelection() &&
118                                 (bloc.GreaterEqual(activeC.CurSelection[0]) && bloc.LessThan(activeC.CurSelection[1]) ||
119                                         bloc.LessThan(activeC.CurSelection[0]) && bloc.GreaterEqual(activeC.CurSelection[1])) {
120                                 // The current character is selected
121                                 style = config.DefStyle.Reverse(true)
122
123                                 if s, ok := config.Colorscheme["selection"]; ok {
124                                         style = s
125                                 }
126
127                         }
128
129                         screen.Screen.SetContent(vlocX, i.y, r, nil, style)
130                         vlocX++
131                 }
132                 nColsBeforeStart--
133         }
134
135         totalwidth := blocX - nColsBeforeStart
136         for len(line) > 0 {
137                 if activeC.X == blocX {
138                         screen.Screen.ShowCursor(vlocX, i.y)
139                 }
140
141                 r, size := utf8.DecodeRune(line)
142
143                 draw(r, i.defStyle)
144
145                 width := 0
146
147                 char := ' '
148                 switch r {
149                 case '\t':
150                         ts := tabsize - (totalwidth % tabsize)
151                         width = ts
152                 default:
153                         width = runewidth.RuneWidth(r)
154                         char = '@'
155                 }
156
157                 blocX++
158                 line = line[size:]
159
160                 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
161                 if width > 1 {
162                         for j := 1; j < width; j++ {
163                                 draw(char, i.defStyle)
164                         }
165                 }
166                 totalwidth += width
167                 if vlocX >= i.width {
168                         break
169                 }
170         }
171         if activeC.X == blocX {
172                 screen.Screen.ShowCursor(vlocX, i.y)
173         }
174 }
175
176 func (i *InfoWindow) Display() {
177         x := 0
178         if i.HasPrompt || config.GlobalSettings["infobar"].(bool) {
179                 if !i.HasPrompt && !i.HasMessage && !i.HasError {
180                         return
181                 }
182                 style := i.defStyle
183
184                 if i.HasError {
185                         style = i.errStyle
186                 }
187
188                 display := i.Msg
189                 for _, c := range display {
190                         screen.Screen.SetContent(x, i.y, c, nil, style)
191                         x += runewidth.RuneWidth(c)
192                 }
193
194                 if i.HasPrompt {
195                         i.displayBuffer()
196                 }
197         }
198 }