| 1 |
package display |
| 2 |
|
| 3 |
import ( |
| 4 |
runewidth "github.com/mattn/go-runewidth" |
| 5 |
"github.com/micro-editor/tcell/v2" |
| 6 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 7 |
"github.com/zyedidia/micro/v2/internal/config" |
| 8 |
"github.com/zyedidia/micro/v2/internal/info" |
| 9 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 10 |
"github.com/zyedidia/micro/v2/internal/util" |
| 11 |
) |
| 12 |
|
| 13 |
type InfoWindow struct { |
| 14 |
*info.InfoBuf |
| 15 |
*View |
| 16 |
|
| 17 |
hscroll int |
| 18 |
} |
| 19 |
|
| 20 |
func (i *InfoWindow) errStyle() tcell.Style { |
| 21 |
errStyle := config.DefStyle. |
| 22 |
Foreground(tcell.ColorBlack). |
| 23 |
Background(tcell.ColorMaroon) |
| 24 |
|
| 25 |
if _, ok := config.Colorscheme["error-message"]; ok { |
| 26 |
errStyle = config.Colorscheme["error-message"] |
| 27 |
} |
| 28 |
|
| 29 |
return errStyle |
| 30 |
} |
| 31 |
|
| 32 |
func (i *InfoWindow) defStyle() tcell.Style { |
| 33 |
defStyle := config.DefStyle |
| 34 |
|
| 35 |
if _, ok := config.Colorscheme["message"]; ok { |
| 36 |
defStyle = config.Colorscheme["message"] |
| 37 |
} |
| 38 |
|
| 39 |
return defStyle |
| 40 |
} |
| 41 |
|
| 42 |
func NewInfoWindow(b *info.InfoBuf) *InfoWindow { |
| 43 |
iw := new(InfoWindow) |
| 44 |
iw.InfoBuf = b |
| 45 |
iw.View = new(View) |
| 46 |
|
| 47 |
iw.Width, iw.Y = screen.Screen.Size() |
| 48 |
iw.Y-- |
| 49 |
|
| 50 |
return iw |
| 51 |
} |
| 52 |
|
| 53 |
func (i *InfoWindow) Resize(w, h int) { |
| 54 |
i.Width = w |
| 55 |
i.Y = h |
| 56 |
} |
| 57 |
|
| 58 |
func (i *InfoWindow) SetBuffer(b *buffer.Buffer) { |
| 59 |
i.InfoBuf.Buffer = b |
| 60 |
} |
| 61 |
|
| 62 |
func (i *InfoWindow) Relocate() bool { return false } |
| 63 |
func (i *InfoWindow) GetView() *View { return i.View } |
| 64 |
func (i *InfoWindow) SetView(v *View) {} |
| 65 |
func (i *InfoWindow) SetActive(b bool) {} |
| 66 |
func (i *InfoWindow) IsActive() bool { return true } |
| 67 |
|
| 68 |
func (i *InfoWindow) LocFromVisual(vloc buffer.Loc) buffer.Loc { |
| 69 |
c := i.Buffer.GetActiveCursor() |
| 70 |
l := i.Buffer.LineBytes(0) |
| 71 |
n := util.CharacterCountInString(i.Msg) |
| 72 |
return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0} |
| 73 |
} |
| 74 |
|
| 75 |
func (i *InfoWindow) BufView() View { |
| 76 |
return View{ |
| 77 |
X: 0, |
| 78 |
Y: i.Y, |
| 79 |
Width: i.Width, |
| 80 |
Height: 1, |
| 81 |
StartLine: SLoc{0, 0}, |
| 82 |
StartCol: 0, |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func (i *InfoWindow) Scroll(s SLoc, n int) SLoc { return s } |
| 87 |
func (i *InfoWindow) Diff(s1, s2 SLoc) int { return 0 } |
| 88 |
func (i *InfoWindow) SLocFromLoc(loc buffer.Loc) SLoc { return SLoc{0, 0} } |
| 89 |
func (i *InfoWindow) VLocFromLoc(loc buffer.Loc) VLoc { return VLoc{SLoc{0, 0}, loc.X} } |
| 90 |
func (i *InfoWindow) LocFromVLoc(vloc VLoc) buffer.Loc { return buffer.Loc{vloc.VisualX, 0} } |
| 91 |
|
| 92 |
func (i *InfoWindow) Clear() { |
| 93 |
for x := 0; x < i.Width; x++ { |
| 94 |
screen.SetContent(x, i.Y, ' ', nil, i.defStyle()) |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
func (i *InfoWindow) displayBuffer() { |
| 99 |
b := i.Buffer |
| 100 |
line := b.LineBytes(0) |
| 101 |
activeC := b.GetActiveCursor() |
| 102 |
|
| 103 |
blocX := 0 |
| 104 |
vlocX := util.CharacterCountInString(i.Msg) |
| 105 |
|
| 106 |
tabsize := 4 |
| 107 |
line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize) |
| 108 |
blocX = bslice |
| 109 |
|
| 110 |
draw := func(r rune, combc []rune, style tcell.Style) { |
| 111 |
if nColsBeforeStart <= 0 { |
| 112 |
bloc := buffer.Loc{X: blocX, Y: 0} |
| 113 |
if activeC.HasSelection() && |
| 114 |
(bloc.GreaterEqual(activeC.CurSelection[0]) && bloc.LessThan(activeC.CurSelection[1]) || |
| 115 |
bloc.LessThan(activeC.CurSelection[0]) && bloc.GreaterEqual(activeC.CurSelection[1])) { |
| 116 |
// The current character is selected |
| 117 |
style = i.defStyle().Reverse(true) |
| 118 |
|
| 119 |
if s, ok := config.Colorscheme["selection"]; ok { |
| 120 |
style = s |
| 121 |
} |
| 122 |
|
| 123 |
} |
| 124 |
|
| 125 |
screen.SetContent(vlocX, i.Y, r, combc, style) |
| 126 |
vlocX += runewidth.RuneWidth(r) |
| 127 |
} |
| 128 |
nColsBeforeStart-- |
| 129 |
} |
| 130 |
|
| 131 |
totalwidth := blocX - nColsBeforeStart |
| 132 |
for len(line) > 0 { |
| 133 |
curVX := vlocX |
| 134 |
curBX := blocX |
| 135 |
r, combc, size := util.DecodeCharacter(line) |
| 136 |
|
| 137 |
width := 0 |
| 138 |
|
| 139 |
switch r { |
| 140 |
case '\t': |
| 141 |
width = tabsize - (totalwidth % tabsize) |
| 142 |
for j := 0; j < width; j++ { |
| 143 |
draw(' ', nil, i.defStyle()) |
| 144 |
} |
| 145 |
default: |
| 146 |
width = runewidth.RuneWidth(r) |
| 147 |
draw(r, combc, i.defStyle()) |
| 148 |
} |
| 149 |
|
| 150 |
blocX++ |
| 151 |
line = line[size:] |
| 152 |
|
| 153 |
if activeC.X == curBX { |
| 154 |
screen.ShowCursor(curVX, i.Y) |
| 155 |
} |
| 156 |
totalwidth += width |
| 157 |
if vlocX >= i.Width { |
| 158 |
break |
| 159 |
} |
| 160 |
} |
| 161 |
if activeC.X == blocX { |
| 162 |
screen.ShowCursor(vlocX, i.Y) |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
var keydisplay = []string{"^Q Quit, ^S Save, ^O Open, ^G Help, ^E Command Bar, ^K Cut Line", "^F Find, ^Z Undo, ^Y Redo, ^A Select All, ^D Duplicate Line, ^T New Tab"} |
| 167 |
|
| 168 |
func (i *InfoWindow) displayKeyMenu() { |
| 169 |
// TODO: maybe make this based on the actual keybindings |
| 170 |
|
| 171 |
for y := 0; y < len(keydisplay); y++ { |
| 172 |
for x := 0; x < i.Width; x++ { |
| 173 |
if x < len(keydisplay[y]) { |
| 174 |
screen.SetContent(x, i.Y-len(keydisplay)+y, rune(keydisplay[y][x]), nil, i.defStyle()) |
| 175 |
} else { |
| 176 |
screen.SetContent(x, i.Y-len(keydisplay)+y, ' ', nil, i.defStyle()) |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func (i *InfoWindow) totalSize() int { |
| 183 |
sum := 0 |
| 184 |
for _, n := range i.Suggestions { |
| 185 |
sum += runewidth.StringWidth(n) + 1 |
| 186 |
} |
| 187 |
return sum |
| 188 |
} |
| 189 |
|
| 190 |
func (i *InfoWindow) scrollToSuggestion() { |
| 191 |
x := 0 |
| 192 |
s := i.totalSize() |
| 193 |
|
| 194 |
for j, n := range i.Suggestions { |
| 195 |
c := util.CharacterCountInString(n) |
| 196 |
if j == i.CurSuggestion { |
| 197 |
if x+c >= i.hscroll+i.Width { |
| 198 |
i.hscroll = util.Clamp(x+c+1-i.Width, 0, s-i.Width) |
| 199 |
} else if x < i.hscroll { |
| 200 |
i.hscroll = util.Clamp(x-1, 0, s-i.Width) |
| 201 |
} |
| 202 |
break |
| 203 |
} |
| 204 |
x += c + 1 |
| 205 |
} |
| 206 |
|
| 207 |
if s-i.Width <= 0 { |
| 208 |
i.hscroll = 0 |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
func (i *InfoWindow) Display() { |
| 213 |
if i.HasPrompt || config.GlobalSettings["infobar"].(bool) { |
| 214 |
i.Clear() |
| 215 |
x := 0 |
| 216 |
if config.GetGlobalOption("keymenu").(bool) { |
| 217 |
i.displayKeyMenu() |
| 218 |
} |
| 219 |
|
| 220 |
if !i.HasPrompt && !i.HasMessage && !i.HasError { |
| 221 |
return |
| 222 |
} |
| 223 |
i.Clear() |
| 224 |
style := i.defStyle() |
| 225 |
|
| 226 |
if i.HasError { |
| 227 |
style = i.errStyle() |
| 228 |
} |
| 229 |
|
| 230 |
display := i.Msg |
| 231 |
for _, c := range display { |
| 232 |
screen.SetContent(x, i.Y, c, nil, style) |
| 233 |
x += runewidth.RuneWidth(c) |
| 234 |
} |
| 235 |
|
| 236 |
if i.HasPrompt { |
| 237 |
i.displayBuffer() |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
if i.HasSuggestions && len(i.Suggestions) > 1 { |
| 242 |
i.scrollToSuggestion() |
| 243 |
|
| 244 |
x := -i.hscroll |
| 245 |
done := false |
| 246 |
|
| 247 |
statusLineStyle := config.DefStyle.Reverse(true) |
| 248 |
if style, ok := config.Colorscheme["statusline.suggestions"]; ok { |
| 249 |
statusLineStyle = style |
| 250 |
} else if style, ok := config.Colorscheme["statusline"]; ok { |
| 251 |
statusLineStyle = style |
| 252 |
} |
| 253 |
keymenuOffset := 0 |
| 254 |
if config.GetGlobalOption("keymenu").(bool) { |
| 255 |
keymenuOffset = len(keydisplay) |
| 256 |
} |
| 257 |
|
| 258 |
draw := func(r rune, s tcell.Style) { |
| 259 |
y := i.Y - keymenuOffset - 1 |
| 260 |
rw := runewidth.RuneWidth(r) |
| 261 |
for j := 0; j < rw; j++ { |
| 262 |
c := r |
| 263 |
if j > 0 { |
| 264 |
c = ' ' |
| 265 |
} |
| 266 |
|
| 267 |
if x == i.Width-1 && !done { |
| 268 |
screen.SetContent(i.Width-1, y, '>', nil, s) |
| 269 |
x++ |
| 270 |
break |
| 271 |
} else if x == 0 && i.hscroll > 0 { |
| 272 |
screen.SetContent(0, y, '<', nil, s) |
| 273 |
} else if x >= 0 && x < i.Width { |
| 274 |
screen.SetContent(x, y, c, nil, s) |
| 275 |
} |
| 276 |
x++ |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
for j, s := range i.Suggestions { |
| 281 |
style := statusLineStyle |
| 282 |
if i.CurSuggestion == j { |
| 283 |
style = style.Reverse(true) |
| 284 |
} |
| 285 |
for _, r := range s { |
| 286 |
draw(r, style) |
| 287 |
// screen.SetContent(x, i.Y-keymenuOffset-1, r, nil, style) |
| 288 |
} |
| 289 |
draw(' ', statusLineStyle) |
| 290 |
} |
| 291 |
|
| 292 |
for x < i.Width { |
| 293 |
draw(' ', statusLineStyle) |
| 294 |
} |
| 295 |
} |
| 296 |
} |