Add more functions to customize status bar

Adds `status.lines`, `status.vcol`, `status.bytes`, `status.size`,
and exposes some functions from go-humanize to plugins.

Ref #1727
This commit is contained in:
Zachary Yedidia 2020-06-24 17:19:42 -04:00
parent db1df05017
commit 5f62f550f3
5 changed files with 59 additions and 4 deletions

View File

@ -534,6 +534,22 @@ func (b *Buffer) Modified() bool {
return buff != b.origHash
}
// Size returns the number of bytes in the current buffer
func (b *Buffer) Size() int {
nb := 0
for i := 0; i < b.LinesNum(); i++ {
nb += len(b.LineBytes(i))
if i != b.LinesNum()-1 {
if b.Endings == FFDos {
nb++ // carriage return
}
nb++ // newline
}
}
return nb
}
// calcHash calculates md5 hash of all lines in the buffer
func calcHash(b *Buffer, out *[md5.Size]byte) error {
h := md5.New()

View File

@ -1243,7 +1243,7 @@ func runtimePluginsLiterateLiterateLua() (*asset, error) {
return a, nil
}
var _runtimePluginsStatusHelpStatusMd = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x6c\x91\xc1\x4e\xf3\x30\x10\x84\xef\x7e\x8a\x51\xfe\xcb\x0f\x42\x79\x80\x1e\x90\x38\x70\xaf\xd4\x72\x8f\x6b\x6f\x9a\x95\x92\x75\xe4\xdd\x20\x78\x7b\x14\xbb\x11\xa1\x70\x8b\x76\xe7\x9b\x1d\x4f\xfe\xe1\x64\xde\x16\x75\xee\x3c\x10\xb4\x7c\x63\x1e\x97\x2b\x0b\xe6\x9c\xde\x39\x92\x42\xd3\x44\xe8\x17\x09\xc6\x49\x14\x7d\xca\x98\x52\xe4\xfe\x93\xe5\x0a\xfb\xe6\x46\x16\x6a\x9d\x7b\xd3\x6d\xde\xd5\x45\x9f\xf2\xe4\x6d\xec\xe0\x25\xfe\x9c\xe5\x0e\x69\x2e\xae\x4f\x05\xa0\x0f\x1f\x0c\x21\x89\x91\x98\xba\xd4\xdf\xdb\x23\x78\xc1\x85\xea\x7d\xa6\xd8\xe2\x38\x92\x57\x82\x12\x15\x6d\x4c\x61\x99\x48\xcc\xaf\xae\x6b\x54\x67\x43\x52\xda\xce\xe0\x7f\xf7\x8c\x81\xc6\x79\x1b\x74\x0f\xb7\xf7\x64\x02\x4b\x4d\xc5\x49\xda\xb5\x10\xfe\x5d\xc5\x7a\xc2\x86\x4c\xfb\x3e\x6c\xf0\xb6\xe5\x5a\x94\x22\x58\x76\xb1\x5d\x89\x5d\x8d\x0f\xce\x3d\x6e\x05\xb4\x97\xec\x25\x0c\xdd\x01\x99\x6c\xc9\x52\xbd\xc5\x4f\x84\xdb\xb3\xc3\x92\x33\x89\xe1\xca\x86\x2a\x6e\x77\xf8\xe0\xf5\x1e\x5e\x47\x7f\xc1\x21\x4d\x13\xdb\x1e\x9e\xbd\x1a\xed\xe8\xa6\x01\x57\xae\x6c\x6e\xe5\x80\x15\x91\xd5\x5f\x46\x8a\xe5\xdf\x35\xc7\x97\xd3\xf9\xb5\x71\xc0\x2a\x67\x5b\x05\x24\x65\xdf\xba\xaf\x00\x00\x00\xff\xff\x39\x4f\xac\xd5\x4b\x02\x00\x00"
var _runtimePluginsStatusHelpStatusMd = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x7c\x51\x41\x6e\xdc\x30\x0c\xbc\xeb\x15\xc4\xf6\xd2\x16\x85\x1f\x90\x43\x81\x1e\x7a\x0f\x90\xf4\x6e\x59\xa6\xd7\x04\x24\xd2\x10\xa9\x45\xf3\xfb\x42\xf2\x0a\x71\xbc\x45\x6e\x02\x87\x33\x9c\x19\x7d\x81\x17\xf3\x56\xd4\xb9\xd7\x15\x41\xdb\x1b\xb6\x58\xae\xc4\xb0\x65\xb9\xd1\x8c\x0a\x2a\x09\x61\x29\x1c\x8c\x84\x15\x16\xc9\x90\x64\xa6\xe5\x8d\xf8\x0a\xf6\xce\x8b\xc4\x38\x38\xf7\x47\xfb\x7c\xdc\x81\x45\x72\xf2\x16\x47\xf0\x3c\x7f\x9c\xe5\x11\x64\x6b\xaa\x3f\x1a\x01\xff\xfa\x60\x10\x84\x0d\xd9\xd4\xc9\x72\x96\x87\xe0\x19\x26\xdc\xef\x13\xce\x03\x3c\x47\xf4\x8a\xa0\x88\x6d\x77\x96\x50\x12\xb2\xf9\xaa\x5a\xad\x3a\x5b\x45\xb1\x9f\x81\xaf\xe3\x4f\x58\x31\x6e\x7d\x30\x7e\xbb\xe7\xc9\x08\xc4\xbb\x2b\x12\x1e\x6a\x21\xf4\x58\xc5\x7b\x0b\xb6\x7a\xeb\x6e\x8a\xe2\x0c\xc4\x0f\x66\x77\xb9\x27\xe7\xbe\xf7\xd8\xc3\x94\x3d\x87\x75\x7c\x82\x8c\x56\x72\xd3\x41\x60\x9f\x10\xee\x61\x43\xc9\x19\xd9\xe0\x4a\x06\xfb\xf2\x70\xa0\xaf\x5e\xcf\xe4\x3a\xfa\x1f\x39\x48\x4a\x64\x47\xf2\xe6\xd5\xf0\xc0\xbe\x5c\x80\x76\x5e\x43\xee\x95\x00\x29\xcc\xa4\x7e\x8a\x38\xb7\x1f\xbb\x3c\xff\x7a\x79\xfd\x7d\x71\x00\x75\x9d\xac\x2e\x20\x37\xfc\xa8\x5e\x13\xeb\x39\x58\x49\x13\xe6\xea\xae\xa1\xbd\xa3\xa9\x2c\x0b\xe6\x23\xf9\x16\x24\x9e\xb8\x37\xd2\xe2\x23\x04\x89\x25\xf1\x41\xe9\x9e\x53\xe5\x83\xc0\xf4\x66\x9f\x5c\x6f\x68\xbf\xde\x4b\xea\x2e\xfe\x05\x00\x00\xff\xff\x94\x05\x0c\xae\x04\x03\x00\x00"
func runtimePluginsStatusHelpStatusMdBytes() ([]byte, error) {
return bindataRead(
@ -1263,7 +1263,7 @@ func runtimePluginsStatusHelpStatusMd() (*asset, error) {
return a, nil
}
var _runtimePluginsStatusStatusLua = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xbc\x93\x3f\x8f\xd4\x30\x10\xc5\xfb\x7c\x8a\xd1\x54\x59\x69\xd7\x77\x14\x94\x5b\x2c\x90\xfb\x23\x24\x0e\x6d\x22\x7a\x27\x19\x6f\x2c\x39\xb6\x65\x3b\x08\x1a\x3e\x3b\x72\xec\x70\xc9\x9d\x90\x16\x84\x70\x37\x33\x2f\xcf\xcf\x3f\xc7\x5f\xaa\x73\xfd\xf8\xf4\x09\x8e\x80\x6f\xd8\x2d\xbb\xc5\xa2\x50\xa6\xe3\x0a\x46\xd9\x39\x03\x47\x90\xa3\x35\x2e\x94\x38\xd7\xb8\xcb\xd3\x76\x12\x82\xdc\xcb\xf1\x4d\x6a\xff\x52\x75\x46\x0b\x79\x79\xa5\x4a\x6d\xdc\x15\x85\x98\x74\x17\xa4\xd1\x20\xb5\x0c\xe5\xae\x00\x80\xb4\x31\xab\x29\xd4\x81\x87\xc9\x3f\x6a\x61\xee\x74\x89\x7e\xae\x58\xeb\xb8\xee\x06\xbc\x46\x3a\x70\x7f\x9d\xd0\x72\x1f\x28\x2b\x53\x36\x76\xea\xfb\xf3\xa4\x83\x1c\xe9\x4e\x2a\x5a\x94\xb8\x5f\xe6\xe7\xe6\x81\x94\xdd\x03\x0e\xa4\xec\x4d\xf6\x19\x7b\xdc\x15\xa4\xfb\xd5\xb9\x52\xdc\xb2\x4d\xe6\x52\x40\xcb\x9a\xef\x96\xd8\x47\xa9\x7b\xf8\x71\xcc\x1c\xd9\xbb\x26\x46\x82\x30\x90\x9e\x85\x71\x25\x84\x7e\x20\xa5\x5e\x11\x9c\xbb\x39\xf1\x4a\x1b\x9c\xd4\x17\xbf\x52\xe7\x4e\x44\xbd\x95\xa6\x5c\x7b\x20\x17\x2f\x71\xb6\x63\xd5\x37\xea\xde\x9b\x71\xe4\xba\x2f\xf1\x22\x03\xee\x01\x1d\x7d\x3d\x58\xee\x3c\xc5\xe2\x70\xe0\x6d\x1b\x3b\x8e\x44\xac\x1f\xaa\xd3\x87\x55\x08\x29\x92\xdd\x11\xb4\x54\xdb\xb3\xc4\xe5\x28\x4c\x4e\x2f\x21\x59\xe3\xe4\x58\x5b\xde\x51\x99\xb2\x3c\xfb\x44\x82\x2f\x3e\x42\x2c\x96\xc9\x96\x6f\xbc\xe3\xdf\xd3\x7d\xfb\x1f\x89\xc6\x24\x7f\xcc\xd3\x0f\xc6\x85\x7f\x88\x32\x86\xf8\x3b\x90\xf3\x1b\x58\x91\xcc\xff\xf9\x3d\x85\x7b\x65\x5a\xae\x9e\x6c\x94\x95\x98\xdf\xca\x36\xd3\x62\xfe\xf9\x54\x37\x15\x3c\x6f\xb1\xdd\x38\x76\x7e\x06\x00\x00\xff\xff\xa0\x33\xe7\xcc\x6f\x04\x00\x00"
var _runtimePluginsStatusStatusLua = "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\xbc\x94\xc1\x6b\xdb\x30\x14\xc6\xef\xfe\x2b\x84\x4e\x32\x24\x6a\x77\xd8\x65\x90\x43\xda\xa5\x69\xd9\x68\x47\x1c\xca\xae\xb2\xfd\x1c\x3f\x90\x25\x23\xc9\x61\xed\x61\x7f\xfb\x90\x64\xb7\x76\x42\xc0\x1b\xa3\xb9\xe9\xbd\xdf\xfb\xf2\xf9\xb3\x9e\x9f\x37\xbb\xec\xe1\xe9\x91\xac\x08\xfd\xc4\xaf\xf9\x35\x4d\x12\xa9\x0b\x21\x49\x83\x85\xd1\x64\x45\xb0\x69\xb5\x71\x8c\x86\x33\x4d\xfb\x6e\xde\x55\x15\x98\xd3\xf6\x55\x2c\xbf\x51\x85\x56\x15\x1e\xce\xa8\x58\x7e\xa3\xea\xae\x11\x0a\x5f\x61\xc4\x0d\x25\x9a\x26\x49\xd5\xa9\xc2\xa1\x56\x04\x15\x3a\x96\x26\x84\x90\x68\x8e\x67\xe0\x32\x27\x5c\x67\x1f\x54\xa5\xef\x14\xa3\x36\x9c\x78\x6e\x84\x2a\x6a\x3a\x07\xad\x85\x9d\x07\xb6\xc2\x3a\x98\x45\x1e\x0b\x2d\x67\x81\x12\x15\xd8\x59\x64\xfe\xe2\x66\x92\x36\x86\xe6\xc1\x98\x32\x5f\x97\xe5\xae\x53\x0e\x1b\xb8\x43\x09\x03\x48\x17\x43\x7f\xb7\xbf\x07\xd9\x2e\x08\xad\x41\xb6\x57\xbd\x4c\x53\xd2\x34\x01\x55\x8e\xd2\x0f\x6e\x59\x1e\xb5\x0d\xb8\xce\x28\xe2\xb4\x75\x06\xd5\x81\xe5\x5f\xbe\xfb\xf6\x63\xd7\xb0\xf4\x74\xd0\xe7\x71\x79\x6e\x0b\x6e\x5d\x38\x3c\xc2\x6d\x67\xac\x36\x2c\xf5\x95\x67\xb4\x9d\x90\x3f\xcf\xb5\x42\x10\x97\xc5\x32\x7c\x85\xf3\x21\x9f\xc9\xc9\xcc\x70\xbf\xf8\x4d\x14\xbc\x30\x19\x2f\xd2\x30\x8b\x15\xc9\xf9\xfe\xa5\x05\xfe\x0d\x55\x49\x7e\xaf\xfa\x2d\xe0\x37\x7b\xff\x16\x88\xab\x41\x05\xd0\xff\xe2\xd5\xb6\x35\x48\x79\x76\xff\x43\xb5\x7f\x4b\x23\x36\x3c\x85\x1d\xd1\x7d\xc5\x2f\xc1\x14\x8d\xbe\x16\x04\x8c\x5f\xc1\x20\xc7\x37\xbf\xa0\xb8\xd5\x4d\x23\x54\xc9\xe8\x01\x1d\x5d\x10\x6a\xe0\xb8\x6c\x85\xb1\xe0\x0f\xcb\xa5\xc8\x73\x5f\x31\x50\xf9\xf3\xfd\x66\xfd\x75\x64\x02\xab\x28\xb7\x22\x0a\xe5\xf4\x59\x46\xc1\xf5\x96\xf8\xde\x60\x93\xb5\xa2\x00\x16\xbd\xbc\xeb\xf8\x04\x4f\x86\x28\x4d\x86\xce\x34\x5f\xbf\x7d\x97\xd3\xfd\xfc\x81\x89\x7a\x27\x7f\x9d\xa7\xad\xb5\x71\xff\x31\x4a\x6f\xe2\xdf\x82\x0c\x5f\xa7\x51\x92\xfd\x6e\x6f\xc1\x6d\xa5\xce\x85\x7c\x6a\x3d\xc6\x68\xff\x15\x9b\x7a\x1a\xc4\x7f\xac\xb3\xfd\x86\xbc\xff\xc5\xf4\x8f\x7d\xe5\x4f\x00\x00\x00\xff\xff\xdc\xdb\xdf\x2a\x2d\x06\x00\x00"
func runtimePluginsStatusStatusLuaBytes() ([]byte, error) {
return bindataRead(

View File

@ -18,6 +18,7 @@ import (
"time"
"unicode/utf8"
humanize "github.com/dustin/go-humanize"
lua "github.com/yuin/gopher-lua"
luar "layeh.com/gopher-luar"
)
@ -69,6 +70,8 @@ func Import(pkg string) *lua.LTable {
return importTime()
case "unicode/utf8", "utf8":
return importUtf8()
case "humanize":
return importHumanize()
default:
return nil
}
@ -556,3 +559,14 @@ func importUtf8() *lua.LTable {
return pkg
}
func importHumanize() *lua.LTable {
pkg := L.NewTable()
L.SetField(pkg, "Bytes", luar.New(L, humanize.Bytes))
L.SetField(pkg, "Ordinal", luar.New(L, humanize.Ordinal))
L.SetField(pkg, "Ftoa", luar.New(L, humanize.Ftoa))
L.SetField(pkg, "FtoaWithDigits", luar.New(L, humanize.FtoaWithDigits))
return pkg
}

View File

@ -6,10 +6,14 @@ Using the `statusformatl` and `statusformatr` options, the exact contents
of the status line can be modified. Please see the documentation for
those options (`> help options`) for more information.
This plugin provides the three functions that can be used in the status
line format:
This plugin provides functions that can be used in the status line format:
* `status.branch`: returns the name of the current git branch.
* `status.hash`: returns the hash of the current git commit.
* `status.paste`: returns "" if the paste option is disabled and "PASTE"
if it is enabled.
* `status.lines`: returns the number of lines in the buffer.
* `status.vcol`: returns the visual column number of the cursor.
* `status.bytes`: returns the number of bytes in the current buffer.
* `status.size`: returns the size of the current buffer in a human-readable
format.

View File

@ -3,14 +3,35 @@ VERSION = "1.0.0"
local micro = import("micro")
local buffer = import("micro/buffer")
local config = import("micro/config")
local humanize = import("humanize")
function init()
micro.SetStatusInfoFn("status.branch")
micro.SetStatusInfoFn("status.hash")
micro.SetStatusInfoFn("status.paste")
micro.SetStatusInfoFn("status.vcol")
micro.SetStatusInfoFn("status.lines")
micro.SetStatusInfoFn("status.bytes")
micro.SetStatusInfoFn("status.size")
config.AddRuntimeFile("status", config.RTHelp, "help/status.md")
end
function lines(b)
return tostring(b:LinesNum())
end
function vcol(b)
return tostring(b:GetActiveCursor():GetVisualX())
end
function bytes(b)
return tostring(b:Size())
end
function size(b)
return humanize.Bytes(b:Size())
end
function branch(b)
if b.Type.Kind ~= buffer.BTInfo then
local shell = import("micro/shell")