micro/internal/display/infowindow.go

240 lines
5.2 KiB
Go
Raw Normal View History

2019-01-01 06:07:01 +03:00
package display
import (
2019-01-01 07:47:24 +03:00
"unicode/utf8"
2019-01-01 06:07:01 +03:00
runewidth "github.com/mattn/go-runewidth"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/buffer"
"github.com/zyedidia/micro/internal/config"
"github.com/zyedidia/micro/internal/info"
"github.com/zyedidia/micro/internal/screen"
"github.com/zyedidia/micro/internal/util"
2019-01-01 06:07:01 +03:00
"github.com/zyedidia/tcell"
)
type InfoWindow struct {
2019-01-02 06:36:12 +03:00
*info.InfoBuf
2019-01-01 06:07:01 +03:00
*View
}
2019-06-16 19:45:39 +03:00
func (i *InfoWindow) errStyle() tcell.Style {
errStyle := config.DefStyle.
Foreground(tcell.ColorBlack).
Background(tcell.ColorMaroon)
2019-01-01 06:07:01 +03:00
2019-06-16 19:45:39 +03:00
if _, ok := config.Colorscheme["error-message"]; ok {
errStyle = config.Colorscheme["error-message"]
}
return errStyle
}
func (i *InfoWindow) defStyle() tcell.Style {
defStyle := config.DefStyle
2019-01-01 06:07:01 +03:00
if _, ok := config.Colorscheme["message"]; ok {
2019-06-16 19:45:39 +03:00
defStyle = config.Colorscheme["message"]
2019-01-01 06:07:01 +03:00
}
2019-06-16 19:45:39 +03:00
return defStyle
}
2019-01-01 06:07:01 +03:00
2019-06-16 19:45:39 +03:00
func NewInfoWindow(b *info.InfoBuf) *InfoWindow {
iw := new(InfoWindow)
iw.InfoBuf = b
iw.View = new(View)
2019-01-01 06:07:01 +03:00
2019-01-17 02:37:45 +03:00
iw.Width, iw.Y = screen.Screen.Size()
iw.Y--
2019-01-01 06:07:01 +03:00
return iw
}
2019-01-05 01:40:56 +03:00
func (i *InfoWindow) Resize(w, h int) {
2019-01-17 02:37:45 +03:00
i.Width = w
i.Y = h
2019-01-05 01:40:56 +03:00
}
2019-01-14 08:57:39 +03:00
func (i *InfoWindow) SetBuffer(b *buffer.Buffer) {
i.InfoBuf.Buffer = b
}
2019-01-05 02:08:11 +03:00
func (i *InfoWindow) Relocate() bool { return false }
func (i *InfoWindow) GetView() *View { return i.View }
func (i *InfoWindow) SetView(v *View) {}
func (i *InfoWindow) SetActive(b bool) {}
2019-01-01 06:07:01 +03:00
2019-01-03 01:39:50 +03:00
func (i *InfoWindow) GetMouseLoc(vloc buffer.Loc) buffer.Loc {
c := i.Buffer.GetActiveCursor()
l := i.Buffer.LineBytes(0)
n := utf8.RuneCountInString(i.Msg)
return buffer.Loc{c.GetCharPosInLine(l, vloc.X-n), 0}
}
2019-01-01 06:07:01 +03:00
func (i *InfoWindow) Clear() {
2019-01-17 02:37:45 +03:00
for x := 0; x < i.Width; x++ {
2019-06-16 19:45:39 +03:00
screen.Screen.SetContent(x, i.Y, ' ', nil, i.defStyle())
2019-01-01 06:07:01 +03:00
}
}
2019-01-01 07:47:24 +03:00
func (i *InfoWindow) displayBuffer() {
b := i.Buffer
line := b.LineBytes(0)
activeC := b.GetActiveCursor()
blocX := 0
vlocX := utf8.RuneCountInString(i.Msg)
tabsize := 4
2019-01-03 04:07:48 +03:00
line, nColsBeforeStart, bslice := util.SliceVisualEnd(line, blocX, tabsize)
blocX = bslice
2019-01-01 07:47:24 +03:00
draw := func(r rune, style tcell.Style) {
if nColsBeforeStart <= 0 {
bloc := buffer.Loc{X: blocX, Y: 0}
if activeC.HasSelection() &&
(bloc.GreaterEqual(activeC.CurSelection[0]) && bloc.LessThan(activeC.CurSelection[1]) ||
bloc.LessThan(activeC.CurSelection[0]) && bloc.GreaterEqual(activeC.CurSelection[1])) {
// The current character is selected
2019-06-16 19:45:39 +03:00
style = i.defStyle().Reverse(true)
2019-01-01 07:47:24 +03:00
if s, ok := config.Colorscheme["selection"]; ok {
style = s
}
}
2019-06-15 23:54:53 +03:00
rw := runewidth.RuneWidth(r)
for j := 0; j < rw; j++ {
c := r
if j > 0 {
c = ' '
}
screen.Screen.SetContent(vlocX, i.Y, c, nil, style)
}
2019-01-01 07:47:24 +03:00
vlocX++
}
nColsBeforeStart--
}
totalwidth := blocX - nColsBeforeStart
for len(line) > 0 {
if activeC.X == blocX {
2019-01-17 02:37:45 +03:00
screen.Screen.ShowCursor(vlocX, i.Y)
2019-01-01 07:47:24 +03:00
}
r, size := utf8.DecodeRune(line)
2019-06-16 19:45:39 +03:00
draw(r, i.defStyle())
2019-01-01 07:47:24 +03:00
width := 0
char := ' '
switch r {
case '\t':
ts := tabsize - (totalwidth % tabsize)
width = ts
default:
width = runewidth.RuneWidth(r)
char = '@'
}
blocX++
line = line[size:]
// Draw any extra characters either spaces for tabs or @ for incomplete wide runes
if width > 1 {
for j := 1; j < width; j++ {
2019-06-16 19:45:39 +03:00
draw(char, i.defStyle())
2019-01-01 07:47:24 +03:00
}
}
totalwidth += width
2019-01-17 02:37:45 +03:00
if vlocX >= i.Width {
2019-01-01 07:47:24 +03:00
break
}
}
if activeC.X == blocX {
2019-01-17 02:37:45 +03:00
screen.Screen.ShowCursor(vlocX, i.Y)
}
}
2019-01-21 01:49:20 +03:00
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"}
2019-01-17 02:37:45 +03:00
func (i *InfoWindow) displayKeyMenu() {
// TODO: maybe make this based on the actual keybindings
2019-01-21 01:49:20 +03:00
for y := 0; y < len(keydisplay); y++ {
2019-01-17 02:37:45 +03:00
for x := 0; x < i.Width; x++ {
2019-01-21 01:49:20 +03:00
if x < len(keydisplay[y]) {
2019-06-16 19:45:39 +03:00
screen.Screen.SetContent(x, i.Y-len(keydisplay)+y, rune(keydisplay[y][x]), nil, i.defStyle())
2019-01-17 02:37:45 +03:00
} else {
2019-06-16 19:45:39 +03:00
screen.Screen.SetContent(x, i.Y-len(keydisplay)+y, ' ', nil, i.defStyle())
2019-01-17 02:37:45 +03:00
}
}
2019-01-01 07:47:24 +03:00
}
}
2019-01-01 06:07:01 +03:00
func (i *InfoWindow) Display() {
x := 0
2019-01-17 02:37:45 +03:00
if config.GetGlobalOption("keymenu").(bool) {
i.displayKeyMenu()
}
2019-01-01 06:07:01 +03:00
if i.HasPrompt || config.GlobalSettings["infobar"].(bool) {
2019-01-01 07:47:24 +03:00
if !i.HasPrompt && !i.HasMessage && !i.HasError {
return
}
2019-01-15 06:16:44 +03:00
i.Clear()
2019-06-16 19:45:39 +03:00
style := i.defStyle()
2019-01-01 06:07:01 +03:00
if i.HasError {
2019-06-16 19:45:39 +03:00
style = i.errStyle()
2019-01-01 06:07:01 +03:00
}
2019-01-01 07:47:24 +03:00
display := i.Msg
2019-01-01 06:07:01 +03:00
for _, c := range display {
2019-01-17 02:37:45 +03:00
screen.Screen.SetContent(x, i.Y, c, nil, style)
2019-01-01 06:07:01 +03:00
x += runewidth.RuneWidth(c)
}
2019-01-01 07:47:24 +03:00
if i.HasPrompt {
i.displayBuffer()
}
2019-01-01 06:07:01 +03:00
}
2019-01-21 01:49:20 +03:00
2019-01-25 02:09:57 +03:00
if i.HasSuggestions && len(i.Suggestions) > 1 {
2019-01-21 01:49:20 +03:00
statusLineStyle := config.DefStyle.Reverse(true)
if style, ok := config.Colorscheme["statusline"]; ok {
statusLineStyle = style
}
keymenuOffset := 0
if config.GetGlobalOption("keymenu").(bool) {
keymenuOffset = len(keydisplay)
}
x := 0
2019-01-25 02:09:57 +03:00
for j, s := range i.Suggestions {
style := statusLineStyle
if i.CurSuggestion == j {
style = style.Reverse(true)
}
2019-01-21 01:49:20 +03:00
for _, r := range s {
2019-01-25 02:09:57 +03:00
screen.Screen.SetContent(x, i.Y-keymenuOffset-1, r, nil, style)
2019-01-21 01:49:20 +03:00
x++
if x >= i.Width {
return
}
}
screen.Screen.SetContent(x, i.Y-keymenuOffset-1, ' ', nil, statusLineStyle)
x++
if x >= i.Width {
return
}
}
for x < i.Width {
screen.Screen.SetContent(x, i.Y-keymenuOffset-1, ' ', nil, statusLineStyle)
x++
}
}
2019-01-01 06:07:01 +03:00
}