10 luar "layeh.com/gopher-luar"
12 runewidth "github.com/mattn/go-runewidth"
13 lua "github.com/yuin/gopher-lua"
14 "github.com/zyedidia/micro/v2/internal/buffer"
15 "github.com/zyedidia/micro/v2/internal/config"
16 ulua "github.com/zyedidia/micro/v2/internal/lua"
17 "github.com/zyedidia/micro/v2/internal/screen"
18 "github.com/zyedidia/micro/v2/internal/util"
21 // StatusLine represents the information line at the bottom
23 // It gives information such as filename, whether the file has been
24 // modified, filetype, cursor location
25 type StatusLine struct {
26 Info map[string]func(*buffer.Buffer) string
31 var statusInfo = map[string]func(*buffer.Buffer) string{
32 "filename": func(b *buffer.Buffer) string {
35 "line": func(b *buffer.Buffer) string {
36 return strconv.Itoa(b.GetActiveCursor().Y + 1)
38 "col": func(b *buffer.Buffer) string {
39 return strconv.Itoa(b.GetActiveCursor().X + 1)
41 "modified": func(b *buffer.Buffer) string {
52 func SetStatusInfoFnLua(fn string) {
53 luaFn := strings.Split(fn, ".")
57 plName, plFn := luaFn[0], luaFn[1]
58 pl := config.FindPlugin(plName)
62 statusInfo[fn] = func(b *buffer.Buffer) string {
63 if pl == nil || !pl.IsEnabled() {
66 val, err := pl.Call(plFn, luar.New(ulua.L, b))
68 if v, ok := val.(lua.LString); !ok {
69 screen.TermMessage(plFn, "should return a string")
79 // NewStatusLine returns a statusline bound to a window
80 func NewStatusLine(win *BufWindow) *StatusLine {
86 // FindOpt finds a given option in the current buffer's settings
87 func (s *StatusLine) FindOpt(opt string) interface{} {
88 if val, ok := s.win.Buf.Settings[opt]; ok {
94 var formatParser = regexp.MustCompile(`\$\(.+?\)`)
96 // Display draws the statusline to the screen
97 func (s *StatusLine) Display() {
98 // We'll draw the line at the lowest line in the window
99 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 for j, sug := range b.Suggestions {
112 style := statusLineStyle
113 if b.CurSuggestion == j {
114 style = style.Reverse(true)
116 for _, r := range sug {
117 screen.SetContent(winX+x, y, r, nil, style)
119 if x >= s.win.Width {
123 screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
125 if x >= s.win.Width {
130 for x < s.win.Width {
131 screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)
137 formatter := func(match []byte) []byte {
138 name := match[2 : len(match)-1]
139 if bytes.HasPrefix(name, []byte("opt")) {
141 return []byte(fmt.Sprint(s.FindOpt(string(option))))
142 } else if bytes.HasPrefix(name, []byte("bind")) {
143 binding := string(name[5:])
144 for k, v := range config.Bindings["buffer"] {
149 return []byte("null")
151 if fn, ok := statusInfo[string(name)]; ok {
152 return []byte(fn(s.win.Buf))
158 leftText := []byte(s.win.Buf.Settings["statusformatl"].(string))
159 leftText = formatParser.ReplaceAllFunc(leftText, formatter)
160 rightText := []byte(s.win.Buf.Settings["statusformatr"].(string))
161 rightText = formatParser.ReplaceAllFunc(rightText, formatter)
163 statusLineStyle := config.DefStyle.Reverse(true)
164 if style, ok := config.Colorscheme["statusline"]; ok {
165 statusLineStyle = style
168 leftLen := util.StringWidth(leftText, util.CharacterCount(leftText), 1)
169 rightLen := util.StringWidth(rightText, util.CharacterCount(rightText), 1)
171 for x := 0; x < s.win.Width; x++ {
173 r, combc, size := util.DecodeCharacter(leftText)
174 leftText = leftText[size:]
175 rw := runewidth.RuneWidth(r)
176 for j := 0; j < rw; j++ {
183 screen.SetContent(winX+x, y, c, combc, statusLineStyle)
185 } else if x >= s.win.Width-rightLen && x < rightLen+s.win.Width-rightLen {
186 r, combc, size := util.DecodeCharacter(rightText)
187 rightText = rightText[size:]
188 rw := runewidth.RuneWidth(r)
189 for j := 0; j < rw; j++ {
196 screen.SetContent(winX+x, y, c, combc, statusLineStyle)
199 screen.SetContent(winX+x, y, ' ', nil, statusLineStyle)