]> git.lizzy.rs Git - micro.git/blob - cmd/micro/display/infowindow.go
Start working on splits
[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
88 func (i *InfoWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc {
89         c := i.Buffer.GetActiveCursor()
90         l := i.Buffer.LineBytes(0)
91         n := utf8.RuneCountInString(i.Msg)
92         return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0}
93 }
94
95 func (i *InfoWindow) Clear() {
96         for x := 0; x < i.width; x++ {
97                 screen.Screen.SetContent(x, i.y, ' ', nil, config.DefStyle)
98         }
99 }
100
101 func (i *InfoWindow) displayBuffer() {
102         b := i.Buffer
103         line := b.LineBytes(0)
104         activeC := b.GetActiveCursor()
105
106         blocX := 0
107         vlocX := utf8.RuneCountInString(i.Msg)
108
109         tabsize := 4
110         line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize)
111         blocX = bslice
112
113         draw := func(r rune, style tcell.Style) {
114                 if nColsBeforeStart <= 0 {
115                         bloc := buffer.Loc{X: blocX, Y: 0}
116                         if activeC.HasSelection() &&
117                                 (bloc.GreaterEqual(activeC.CurSelection[0]) && bloc.LessThan(activeC.CurSelection[1]) ||
118                                         bloc.LessThan(activeC.CurSelection[0]) && bloc.GreaterEqual(activeC.CurSelection[1])) {
119                                 // The current character is selected
120                                 style = config.DefStyle.Reverse(true)
121
122                                 if s, ok := config.Colorscheme["selection"]; ok {
123                                         style = s
124                                 }
125
126                         }
127
128                         screen.Screen.SetContent(vlocX, i.y, r, nil, style)
129                         vlocX++
130                 }
131                 nColsBeforeStart--
132         }
133
134         totalwidth := blocX - nColsBeforeStart
135         for len(line) > 0 {
136                 if activeC.X == blocX {
137                         screen.Screen.ShowCursor(vlocX, i.y)
138                 }
139
140                 r, size := utf8.DecodeRune(line)
141
142                 draw(r, i.defStyle)
143
144                 width := 0
145
146                 char := ' '
147                 switch r {
148                 case '\t':
149                         ts := tabsize - (totalwidth % tabsize)
150                         width = ts
151                 default:
152                         width = runewidth.RuneWidth(r)
153                         char = '@'
154                 }
155
156                 blocX++
157                 line = line[size:]
158
159                 // Draw any extra characters either spaces for tabs or @ for incomplete wide runes
160                 if width > 1 {
161                         for j := 1; j < width; j++ {
162                                 draw(char, i.defStyle)
163                         }
164                 }
165                 totalwidth += width
166                 if vlocX >= i.width {
167                         break
168                 }
169         }
170         if activeC.X == blocX {
171                 screen.Screen.ShowCursor(vlocX, i.y)
172         }
173 }
174
175 func (i *InfoWindow) Display() {
176         x := 0
177         if i.HasPrompt || config.GlobalSettings["infobar"].(bool) {
178                 if !i.HasPrompt && !i.HasMessage && !i.HasError {
179                         return
180                 }
181                 style := i.defStyle
182
183                 if i.HasError {
184                         style = i.errStyle
185                 }
186
187                 display := i.Msg
188                 for _, c := range display {
189                         screen.Screen.SetContent(x, i.y, c, nil, style)
190                         x += runewidth.RuneWidth(c)
191                 }
192
193                 if i.HasPrompt {
194                         i.displayBuffer()
195                 }
196         }
197 }