aboutsummaryrefslogtreecommitdiff
path: root/plugins/gitstatus.lua
blob: a58fd0fa632d6a562fc3cadb86a87953824c2d19 (plain)
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
63
64
65
66
67
68
69
local core = require "core"
local config = require "core.config"
local style = require "core.style"
local StatusView = require "core.statusview"


local git = {
  branch = nil,
  inserts = 0,
  deletes = 0,
}


local function exec(cmd, wait)
  local tempfile = core.temp_filename()
  system.exec(string.format("%s > %q", cmd, tempfile))
  coroutine.yield(wait)
  local fp = io.open(tempfile)
  local res = fp:read("*a")
  fp:close()
  os.remove(tempfile)
  return res
end


core.add_thread(function()
  while true do
    if system.get_file_info(".git") then
      -- get branch name
      git.branch = exec("git rev-parse --abbrev-ref HEAD", 1):match("[^\n]*")

      -- get diff
      local line = exec("git diff --stat", 1):match("[^\n]*%s*$")
      git.inserts = tonumber(line:match("(%d+) ins")) or 0
      git.deletes = tonumber(line:match("(%d+) del")) or 0

    else
      git.branch = nil
    end

    coroutine.yield(config.project_scan_rate)
  end
end)


local get_items = StatusView.get_items

function StatusView:get_items()
  if not git.branch then
    return get_items(self)
  end
  local left, right = get_items(self)

  local t = {
    style.dim, self.separator,
    (git.inserts ~= 0 or git.deletes ~= 0) and style.accent or style.text,
    git.branch,
    style.dim, "  ",
    git.inserts ~= 0 and style.accent or style.text, "+", git.inserts,
    style.dim, " / ",
    git.deletes ~= 0 and style.accent or style.text, "-", git.deletes,
  }
  for _, item in ipairs(t) do
    table.insert(right, item)
  end

  return left, right
end