12 luar "layeh.com/gopher-luar"
14 runewidth "github.com/mattn/go-runewidth"
15 lua "github.com/yuin/gopher-lua"
16 "github.com/zyedidia/micro/internal/buffer"
17 "github.com/zyedidia/micro/internal/config"
18 ulua "github.com/zyedidia/micro/internal/lua"
19 "github.com/zyedidia/micro/internal/screen"
20 "github.com/zyedidia/micro/internal/util"
23 // StatusLine represents the information line at the bottom
25 // It gives information such as filename, whether the file has been
26 // modified, filetype, cursor location
27 type StatusLine struct {
28 Info map[string]func(*buffer.Buffer) string
33 var statusInfo = map[string]func(*buffer.Buffer) string{
34 "filename": func(b *buffer.Buffer) string {
35 if b.Settings["basename"].(bool) {
36 return path.Base(b.GetName())
40 "line": func(b *buffer.Buffer) string {
41 return strconv.Itoa(b.GetActiveCursor().Y + 1)
43 "col": func(b *buffer.Buffer) string {
44 return strconv.Itoa(b.GetActiveCursor().X + 1)
46 "modified": func(b *buffer.Buffer) string {
54 func SetStatusInfoFnLua(fn string) {
55 luaFn := strings.Split(fn, ".")
59 plName, plFn := luaFn[0], luaFn[1]
60 pl := config.FindPlugin(plName)
64 statusInfo[fn] = func(b *buffer.Buffer) string {
65 if pl == nil || !pl.IsEnabled() {
68 val, err := pl.Call(plFn, luar.New(ulua.L, b))
70 if v, ok := val.(lua.LString); !ok {
71 screen.TermMessage(plFn, "should return a string")
81 // NewStatusLine returns a statusline bound to a window
82 func NewStatusLine(win *BufWindow) *StatusLine {
88 // FindOpt finds a given option in the current buffer's settings
89 func (s *StatusLine) FindOpt(opt string) interface{} {
90 if val, ok := s.win.Buf.Settings[opt]; ok {
96 var formatParser = regexp.MustCompile(`\$\(.+?\)`)
98 // Display draws the statusline to the screen
99 func (s *StatusLine) Display() {
100 // We'll draw the line at the lowest line in the window
101 y := s.win.Height + s.win.Y - 1
104 // autocomplete suggestions (for the buffer, not for the infowindow)
105 if b.HasSuggestions && len(b.Suggestions) > 1 {
106 statusLineStyle := config.DefStyle.Reverse(true)
107 if style, ok := config.Colorscheme["statusline"]; ok {
108 statusLineStyle = style
111 if config.GetGlobalOption("keymenu").(bool) {
112 keymenuOffset = len(keydisplay)
115 for j, sug := range b.Suggestions {
116 style := statusLineStyle
117 if b.CurSuggestion == j {
118 style = style.Reverse(true)
120 for _, r := range sug {
121 screen.SetContent(x, y-keymenuOffset, r, nil, style)
123 if x >= s.win.Width {
127 screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
129 if x >= s.win.Width {
134 for x < s.win.Width {
135 screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
141 formatter := func(match []byte) []byte {
142 name := match[2 : len(match)-1]
143 if bytes.HasPrefix(name, []byte("opt")) {
145 return []byte(fmt.Sprint(s.FindOpt(string(option))))
146 } else if bytes.HasPrefix(name, []byte("bind")) {
147 binding := string(name[5:])
148 for k, v := range config.Bindings {
153 return []byte("null")
155 if fn, ok := statusInfo[string(name)]; ok {
156 return []byte(fn(s.win.Buf))
162 leftText := []byte(s.win.Buf.Settings["statusformatl"].(string))
163 leftText = formatParser.ReplaceAllFunc(leftText, formatter)
164 rightText := []byte(s.win.Buf.Settings["statusformatr"].(string))
165 rightText = formatParser.ReplaceAllFunc(rightText, formatter)
167 statusLineStyle := config.DefStyle.Reverse(true)
168 if style, ok := config.Colorscheme["statusline"]; ok {
169 statusLineStyle = style
172 leftLen := util.StringWidth(leftText, utf8.RuneCount(leftText), 1)
173 rightLen := util.StringWidth(rightText, utf8.RuneCount(rightText), 1)
176 for x := 0; x < s.win.Width; x++ {
178 r, size := utf8.DecodeRune(leftText)
179 leftText = leftText[size:]
180 rw := runewidth.RuneWidth(r)
181 for j := 0; j < rw; j++ {
187 screen.SetContent(winX+x, y, c, nil, statusLineStyle)
189 } else if x >= s.win.Width-rightLen && x < rightLen+s.win.Width-rightLen {
190 r, size := utf8.DecodeRune(rightText)
191 rightText = rightText[size:]
192 rw := runewidth.RuneWidth(r)
193 for j := 0; j < rw; j++ {
199 screen.SetContent(winX+x, y, c, nil, statusLineStyle)
202 screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)