]> git.lizzy.rs Git - micro.git/blob - internal/display/statusline.go
c139c8c6ab46f16af930fecc62b8f8632319a5a0
[micro.git] / internal / display / statusline.go
1 package display
2
3 import (
4         "bytes"
5         "fmt"
6         "path"
7         "regexp"
8         "strconv"
9         "strings"
10         "unicode/utf8"
11
12         luar "layeh.com/gopher-luar"
13
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"
21 )
22
23 // StatusLine represents the information line at the bottom
24 // of each window
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
29
30         win *BufWindow
31 }
32
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())
37                 }
38                 return b.GetName()
39         },
40         "line": func(b *buffer.Buffer) string {
41                 return strconv.Itoa(b.GetActiveCursor().Y + 1)
42         },
43         "col": func(b *buffer.Buffer) string {
44                 return strconv.Itoa(b.GetActiveCursor().X + 1)
45         },
46         "modified": func(b *buffer.Buffer) string {
47                 if b.Modified() {
48                         return "+ "
49                 }
50                 return ""
51         },
52 }
53
54 func SetStatusInfoFnLua(fn string) {
55         luaFn := strings.Split(fn, ".")
56         if len(luaFn) <= 1 {
57                 return
58         }
59         plName, plFn := luaFn[0], luaFn[1]
60         pl := config.FindPlugin(plName)
61         if pl == nil {
62                 return
63         }
64         statusInfo[fn] = func(b *buffer.Buffer) string {
65                 if pl == nil || !pl.IsEnabled() {
66                         return ""
67                 }
68                 val, err := pl.Call(plFn, luar.New(ulua.L, b))
69                 if err == nil {
70                         if v, ok := val.(lua.LString); !ok {
71                                 screen.TermMessage(plFn, "should return a string")
72                                 return ""
73                         } else {
74                                 return string(v)
75                         }
76                 }
77                 return ""
78         }
79 }
80
81 // NewStatusLine returns a statusline bound to a window
82 func NewStatusLine(win *BufWindow) *StatusLine {
83         s := new(StatusLine)
84         s.win = win
85         return s
86 }
87
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 {
91                 return val
92         }
93         return "null"
94 }
95
96 var formatParser = regexp.MustCompile(`\$\(.+?\)`)
97
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
102
103         b := s.win.Buf
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
109                 }
110                 keymenuOffset := 0
111                 if config.GetGlobalOption("keymenu").(bool) {
112                         keymenuOffset = len(keydisplay)
113                 }
114                 x := 0
115                 for j, sug := range b.Suggestions {
116                         style := statusLineStyle
117                         if b.CurSuggestion == j {
118                                 style = style.Reverse(true)
119                         }
120                         for _, r := range sug {
121                                 screen.SetContent(x, y-keymenuOffset, r, nil, style)
122                                 x++
123                                 if x >= s.win.Width {
124                                         return
125                                 }
126                         }
127                         screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
128                         x++
129                         if x >= s.win.Width {
130                                 return
131                         }
132                 }
133
134                 for x < s.win.Width {
135                         screen.SetContent(x, y-keymenuOffset, ' ', nil, statusLineStyle)
136                         x++
137                 }
138                 return
139         }
140
141         formatter := func(match []byte) []byte {
142                 name := match[2 : len(match)-1]
143                 if bytes.HasPrefix(name, []byte("opt")) {
144                         option := name[4:]
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 {
149                                 if v == binding {
150                                         return []byte(k)
151                                 }
152                         }
153                         return []byte("null")
154                 } else {
155                         if fn, ok := statusInfo[string(name)]; ok {
156                                 return []byte(fn(s.win.Buf))
157                         }
158                         return []byte{}
159                 }
160         }
161
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)
166
167         statusLineStyle := config.DefStyle.Reverse(true)
168         if style, ok := config.Colorscheme["statusline"]; ok {
169                 statusLineStyle = style
170         }
171
172         leftLen := util.StringWidth(leftText, utf8.RuneCount(leftText), 1)
173         rightLen := util.StringWidth(rightText, utf8.RuneCount(rightText), 1)
174
175         winX := s.win.X
176         for x := 0; x < s.win.Width; x++ {
177                 if x < leftLen {
178                         r, size := utf8.DecodeRune(leftText)
179                         leftText = leftText[size:]
180                         rw := runewidth.RuneWidth(r)
181                         for j := 0; j < rw; j++ {
182                                 c := r
183                                 if j > 0 {
184                                         c = ' '
185                                         x++
186                                 }
187                                 screen.SetContent(winX+x, y, c, nil, statusLineStyle)
188                         }
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++ {
194                                 c := r
195                                 if j > 0 {
196                                         c = ' '
197                                         x++
198                                 }
199                                 screen.SetContent(winX+x, y, c, nil, statusLineStyle)
200                         }
201                 } else {
202                         screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
203                 }
204         }
205 }