| 1 |
package display |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"regexp" |
| 7 |
"strconv" |
| 8 |
"strings" |
| 9 |
|
| 10 |
luar "layeh.com/gopher-luar" |
| 11 |
|
| 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" |
| 19 |
) |
| 20 |
|
| 21 |
// StatusLine represents the information line at the bottom |
| 22 |
// of each window |
| 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 |
| 27 |
|
| 28 |
win *BufWindow |
| 29 |
} |
| 30 |
|
| 31 |
var statusInfo = map[string]func(*buffer.Buffer) string{ |
| 32 |
"filename": func(b *buffer.Buffer) string { |
| 33 |
return b.GetName() |
| 34 |
}, |
| 35 |
"line": func(b *buffer.Buffer) string { |
| 36 |
return strconv.Itoa(b.GetActiveCursor().Y + 1) |
| 37 |
}, |
| 38 |
"col": func(b *buffer.Buffer) string { |
| 39 |
return strconv.Itoa(b.GetActiveCursor().X + 1) |
| 40 |
}, |
| 41 |
"modified": func(b *buffer.Buffer) string { |
| 42 |
if b.Modified() { |
| 43 |
return "+ " |
| 44 |
} |
| 45 |
if b.Type.Readonly { |
| 46 |
return "[ro] " |
| 47 |
} |
| 48 |
return "" |
| 49 |
}, |
| 50 |
"overwrite": func(b *buffer.Buffer) string { |
| 51 |
if b.OverwriteMode && !b.Type.Readonly { |
| 52 |
return "[ovwr] " |
| 53 |
} |
| 54 |
return "" |
| 55 |
}, |
| 56 |
"lines": func(b *buffer.Buffer) string { |
| 57 |
return strconv.Itoa(b.LinesNum()) |
| 58 |
}, |
| 59 |
"percentage": func(b *buffer.Buffer) string { |
| 60 |
return strconv.Itoa((b.GetActiveCursor().Y + 1) * 100 / b.LinesNum()) |
| 61 |
}, |
| 62 |
} |
| 63 |
|
| 64 |
func SetStatusInfoFnLua(fn string) { |
| 65 |
luaFn := strings.Split(fn, ".") |
| 66 |
if len(luaFn) <= 1 { |
| 67 |
return |
| 68 |
} |
| 69 |
plName, plFn := luaFn[0], luaFn[1] |
| 70 |
pl := config.FindPlugin(plName) |
| 71 |
if pl == nil { |
| 72 |
return |
| 73 |
} |
| 74 |
statusInfo[fn] = func(b *buffer.Buffer) string { |
| 75 |
if pl == nil || !pl.IsLoaded() { |
| 76 |
return "" |
| 77 |
} |
| 78 |
val, err := pl.Call(plFn, luar.New(ulua.L, b)) |
| 79 |
if err == nil { |
| 80 |
if v, ok := val.(lua.LString); !ok { |
| 81 |
screen.TermMessage(plFn, "should return a string") |
| 82 |
return "" |
| 83 |
} else { |
| 84 |
return string(v) |
| 85 |
} |
| 86 |
} |
| 87 |
return "" |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
// NewStatusLine returns a statusline bound to a window |
| 92 |
func NewStatusLine(win *BufWindow) *StatusLine { |
| 93 |
s := new(StatusLine) |
| 94 |
s.win = win |
| 95 |
return s |
| 96 |
} |
| 97 |
|
| 98 |
// FindOpt finds a given option in the current buffer's settings |
| 99 |
func (s *StatusLine) FindOpt(opt string) any { |
| 100 |
if val, ok := s.win.Buf.Settings[opt]; ok { |
| 101 |
return val |
| 102 |
} |
| 103 |
return "null" |
| 104 |
} |
| 105 |
|
| 106 |
var formatParser = regexp.MustCompile(`\$\(.+?\)`) |
| 107 |
|
| 108 |
// Display draws the statusline to the screen |
| 109 |
func (s *StatusLine) Display() { |
| 110 |
// We'll draw the line at the lowest line in the window |
| 111 |
y := s.win.Height + s.win.Y - 1 |
| 112 |
|
| 113 |
winX := s.win.X |
| 114 |
|
| 115 |
b := s.win.Buf |
| 116 |
// autocomplete suggestions (for the buffer, not for the infowindow) |
| 117 |
if b.HasSuggestions && len(b.Suggestions) > 1 { |
| 118 |
statusLineStyle := config.DefStyle.Reverse(true) |
| 119 |
if style, ok := config.Colorscheme["statusline.suggestions"]; ok { |
| 120 |
statusLineStyle = style |
| 121 |
} else if style, ok := config.Colorscheme["statusline"]; ok { |
| 122 |
statusLineStyle = style |
| 123 |
} |
| 124 |
x := 0 |
| 125 |
for j, sug := range b.Suggestions { |
| 126 |
style := statusLineStyle |
| 127 |
if b.CurSuggestion == j { |
| 128 |
style = style.Reverse(true) |
| 129 |
} |
| 130 |
for _, r := range sug { |
| 131 |
screen.SetContent(winX+x, y, r, nil, style) |
| 132 |
x++ |
| 133 |
if x >= s.win.Width { |
| 134 |
return |
| 135 |
} |
| 136 |
} |
| 137 |
screen.SetContent(winX+x, y, ' ', nil, statusLineStyle) |
| 138 |
x++ |
| 139 |
if x >= s.win.Width { |
| 140 |
return |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
for x < s.win.Width { |
| 145 |
screen.SetContent(winX+x, y, ' ', nil, statusLineStyle) |
| 146 |
x++ |
| 147 |
} |
| 148 |
return |
| 149 |
} |
| 150 |
|
| 151 |
formatter := func(match []byte) []byte { |
| 152 |
name := match[2 : len(match)-1] |
| 153 |
if bytes.HasPrefix(name, []byte("opt")) { |
| 154 |
option := name[4:] |
| 155 |
return fmt.Append(nil, s.FindOpt(string(option))) |
| 156 |
} else if bytes.HasPrefix(name, []byte("bind")) { |
| 157 |
binding := string(name[5:]) |
| 158 |
for k, v := range config.Bindings["buffer"] { |
| 159 |
if v == binding { |
| 160 |
return []byte(k) |
| 161 |
} |
| 162 |
} |
| 163 |
return []byte("null") |
| 164 |
} else { |
| 165 |
if fn, ok := statusInfo[string(name)]; ok { |
| 166 |
return []byte(fn(s.win.Buf)) |
| 167 |
} |
| 168 |
return []byte{} |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
leftText := []byte(s.win.Buf.Settings["statusformatl"].(string)) |
| 173 |
leftText = formatParser.ReplaceAllFunc(leftText, formatter) |
| 174 |
rightText := []byte(s.win.Buf.Settings["statusformatr"].(string)) |
| 175 |
rightText = formatParser.ReplaceAllFunc(rightText, formatter) |
| 176 |
|
| 177 |
statusLineStyle := config.DefStyle.Reverse(true) |
| 178 |
if s.win.IsActive() { |
| 179 |
if style, ok := config.Colorscheme["statusline"]; ok { |
| 180 |
statusLineStyle = style |
| 181 |
} |
| 182 |
} else { |
| 183 |
if style, ok := config.Colorscheme["statusline.inactive"]; ok { |
| 184 |
statusLineStyle = style |
| 185 |
} else if style, ok := config.Colorscheme["statusline"]; ok { |
| 186 |
statusLineStyle = style |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
leftLen := util.StringWidth(leftText, util.CharacterCount(leftText), 1) |
| 191 |
rightLen := util.StringWidth(rightText, util.CharacterCount(rightText), 1) |
| 192 |
|
| 193 |
for x := 0; x < s.win.Width; x++ { |
| 194 |
if x < leftLen { |
| 195 |
r, combc, size := util.DecodeCharacter(leftText) |
| 196 |
leftText = leftText[size:] |
| 197 |
rw := runewidth.RuneWidth(r) |
| 198 |
for j := 0; j < rw; j++ { |
| 199 |
c := r |
| 200 |
if j > 0 { |
| 201 |
c = ' ' |
| 202 |
combc = nil |
| 203 |
x++ |
| 204 |
} |
| 205 |
screen.SetContent(winX+x, y, c, combc, statusLineStyle) |
| 206 |
} |
| 207 |
} else if x >= s.win.Width-rightLen && x < rightLen+s.win.Width-rightLen { |
| 208 |
r, combc, size := util.DecodeCharacter(rightText) |
| 209 |
rightText = rightText[size:] |
| 210 |
rw := runewidth.RuneWidth(r) |
| 211 |
for j := 0; j < rw; j++ { |
| 212 |
c := r |
| 213 |
if j > 0 { |
| 214 |
c = ' ' |
| 215 |
combc = nil |
| 216 |
x++ |
| 217 |
} |
| 218 |
screen.SetContent(winX+x, y, c, combc, statusLineStyle) |
| 219 |
} |
| 220 |
} else { |
| 221 |
screen.SetContent(winX+x, y, ' ', nil, statusLineStyle) |
| 222 |
} |
| 223 |
} |
| 224 |
} |