File: status.lua

package info (click to toggle)
micro 2.0.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,128 kB
  • sloc: sh: 265; makefile: 77; xml: 53
file content (62 lines) | stat: -rw-r--r-- 1,454 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
VERSION = "1.0.0"

local micro = import("micro")
local buffer = import("micro/buffer")
local config = import("micro/config")
local shell = import("micro/shell")
local filepath = import("filepath")
local humanize = import("humanize")
local strings = import("strings")

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(false))
end

function bytes(b)
    return tostring(b:Size())
end

function size(b)
    return humanize.Bytes(b:Size())
end

local function parseRevision(b, opt)
    if b.Type.Kind ~= buffer.BTInfo then
        local dir = filepath.Dir(b.Path)
        local str, err = shell.ExecCommand("git", "-C", dir, "rev-parse", opt, "HEAD")
        if err == nil then
            return strings.TrimSpace(str)
        end
    end
    return ""
end

function branch(b)
    return parseRevision(b, "--abbrev-ref")
end

function hash(b)
    return parseRevision(b, "--short")
end

function paste(b)
    if config.GetGlobalOption("paste") then
        return "PASTE "
    end
    return ""
end