micro/internal/display/statusline.go

200 lines
4.9 KiB
Go
Raw Normal View History

2018-08-29 01:44:52 +03:00
package display
2016-03-18 01:20:07 +03:00
import (
2018-08-26 06:06:44 +03:00
"bytes"
"fmt"
2018-01-03 06:25:55 +03:00
"path"
2018-08-26 06:06:44 +03:00
"regexp"
2016-03-18 01:20:07 +03:00
"strconv"
"strings"
2018-08-26 06:06:44 +03:00
"unicode/utf8"
2018-08-27 22:53:10 +03:00
luar "layeh.com/gopher-luar"
2019-06-15 23:54:53 +03:00
runewidth "github.com/mattn/go-runewidth"
lua "github.com/yuin/gopher-lua"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/buffer"
"github.com/zyedidia/micro/internal/config"
ulua "github.com/zyedidia/micro/internal/lua"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/screen"
2019-06-15 23:54:53 +03:00
"github.com/zyedidia/micro/internal/util"
2016-03-18 01:20:07 +03:00
)
2018-08-26 06:06:44 +03:00
// StatusLine represents the information line at the bottom
// of each window
2016-03-25 19:14:22 +03:00
// It gives information such as filename, whether the file has been
// modified, filetype, cursor location
2018-08-26 06:06:44 +03:00
type StatusLine struct {
2019-06-15 23:13:04 +03:00
Info map[string]func(*buffer.Buffer) string
2016-03-18 01:20:07 +03:00
2018-08-29 01:44:52 +03:00
win *BufWindow
2018-08-26 06:06:44 +03:00
}
var statusInfo = map[string]func(*buffer.Buffer) string{
"filename": func(b *buffer.Buffer) string {
if b.Settings["basename"].(bool) {
return path.Base(b.GetName())
}
return b.GetName()
},
"line": func(b *buffer.Buffer) string {
return strconv.Itoa(b.GetActiveCursor().Y + 1)
},
"col": func(b *buffer.Buffer) string {
return strconv.Itoa(b.GetActiveCursor().X + 1)
},
"modified": func(b *buffer.Buffer) string {
if b.Modified() {
return "+ "
}
return ""
},
}
func SetStatusInfoFnLua(s string, fn string) {
luaFn := strings.Split(fn, ".")
plName, plFn := luaFn[0], luaFn[1]
2019-08-03 04:29:47 +03:00
pl := config.FindPlugin(plName)
statusInfo[s] = func(b *buffer.Buffer) string {
if pl == nil || !pl.IsEnabled() {
return ""
}
val, err := pl.Call(plFn, luar.New(ulua.L, b))
if err == nil {
if v, ok := val.(lua.LString); !ok {
screen.TermMessage(plFn, "should return a string")
return ""
} else {
return string(v)
}
}
return ""
}
}
2018-08-26 06:06:44 +03:00
// NewStatusLine returns a statusline bound to a window
2018-08-29 01:44:52 +03:00
func NewStatusLine(win *BufWindow) *StatusLine {
2018-08-26 06:06:44 +03:00
s := new(StatusLine)
s.win = win
return s
}
2016-03-25 19:14:22 +03:00
2018-08-26 06:06:44 +03:00
// FindOpt finds a given option in the current buffer's settings
func (s *StatusLine) FindOpt(opt string) interface{} {
if val, ok := s.win.Buf.Settings[opt]; ok {
return val
2016-03-18 01:20:07 +03:00
}
2018-08-26 06:06:44 +03:00
return "null"
}
2016-03-25 19:14:22 +03:00
2018-08-26 06:06:44 +03:00
var formatParser = regexp.MustCompile(`\$\(.+?\)`)
2016-03-25 19:14:22 +03:00
2018-08-26 06:06:44 +03:00
// Display draws the statusline to the screen
func (s *StatusLine) Display() {
// We'll draw the line at the lowest line in the window
2018-08-27 22:53:10 +03:00
y := s.win.Height + s.win.Y - 1
2019-06-16 22:56:39 +03:00
b := s.win.Buf
// autocomplete suggestions (for the buffer, not for the infowindow)
2019-06-16 22:56:39 +03:00
if b.HasSuggestions && len(b.Suggestions) > 1 {
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
for j, sug := range b.Suggestions {
style := statusLineStyle
if b.CurSuggestion == j {
style = style.Reverse(true)
}
for _, r := range sug {
screen.Screen.SetContent(x, y-keymenuOffset, r, nil, style)
x++
if x >= s.win.Width {
return
}
}
screen.Screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
x++
if x >= s.win.Width {
return
}
}
for x < s.win.Width {
screen.Screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
x++
}
return
}
2018-08-26 06:06:44 +03:00
formatter := func(match []byte) []byte {
name := match[2 : len(match)-1]
if bytes.HasPrefix(name, []byte("opt")) {
option := name[4:]
return []byte(fmt.Sprint(s.FindOpt(string(option))))
} else if bytes.HasPrefix(name, []byte("bind")) {
2019-01-11 23:33:16 +03:00
binding := string(name[5:])
for k, v := range config.Bindings {
if v == binding {
return []byte(k)
}
}
2018-08-27 22:53:10 +03:00
return []byte("null")
2018-08-26 06:06:44 +03:00
} else {
if fn, ok := statusInfo[string(name)]; ok {
return []byte(fn(s.win.Buf))
}
return []byte{}
}
2016-04-19 20:58:02 +03:00
}
2016-03-28 00:53:00 +03:00
leftText := []byte(s.win.Buf.Settings["statusformatl"].(string))
2019-06-15 23:13:04 +03:00
leftText = formatParser.ReplaceAllFunc(leftText, formatter)
rightText := []byte(s.win.Buf.Settings["statusformatr"].(string))
2019-06-15 23:13:04 +03:00
rightText = formatParser.ReplaceAllFunc(rightText, formatter)
2018-08-26 06:06:44 +03:00
2018-08-27 22:53:10 +03:00
statusLineStyle := config.DefStyle.Reverse(true)
if style, ok := config.Colorscheme["statusline"]; ok {
statusLineStyle = style
}
2016-03-18 01:20:07 +03:00
2019-06-15 23:54:53 +03:00
leftLen := util.StringWidth(leftText, utf8.RuneCount(leftText), 1)
rightLen := util.StringWidth(rightText, utf8.RuneCount(rightText), 1)
2018-08-26 06:06:44 +03:00
winX := s.win.X
for x := 0; x < s.win.Width; x++ {
if x < leftLen {
r, size := utf8.DecodeRune(leftText)
leftText = leftText[size:]
2019-06-15 23:54:53 +03:00
rw := runewidth.RuneWidth(r)
for j := 0; j < rw; j++ {
c := r
if j > 0 {
c = ' '
x++
}
screen.Screen.SetContent(winX+x, y, c, nil, statusLineStyle)
}
2018-08-26 06:06:44 +03:00
} else if x >= s.win.Width-rightLen && x < rightLen+s.win.Width-rightLen {
r, size := utf8.DecodeRune(rightText)
rightText = rightText[size:]
2019-06-15 23:54:53 +03:00
rw := runewidth.RuneWidth(r)
for j := 0; j < rw; j++ {
c := r
if j > 0 {
c = ' '
x++
}
screen.Screen.SetContent(winX+x, y, c, nil, statusLineStyle)
}
2016-03-18 01:20:07 +03:00
} else {
2018-08-27 22:53:10 +03:00
screen.Screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
2016-03-18 01:20:07 +03:00
}
}
}