-
Notifications
You must be signed in to change notification settings - Fork 153
/
powerline_hg.lua
126 lines (110 loc) · 4.02 KB
/
powerline_hg.lua
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
-- Constants
local segmentColors = {
branch = {
fill = colorGreen,
text = colorBlack
},
clean = {
fill = colorGreen,
text = colorWhite
},
dirty = {
fill = colorRed,
text = colorWhite
}
}
--- copied from clink.lua
-- Resolves closest directory location for specified directory.
-- Navigates subsequently up one level and tries to find specified directory
-- @param {string} path Path to directory will be checked. If not provided
-- current directory will be used
-- @param {string} dirname Directory name to search for
-- @return {string} Path to specified directory or nil if such dir not found
local function get_dir_contains(path, dirname)
-- return parent path for specified entry (either file or directory)
local function pathname(path)
local prefix = ""
local i = path:find("[\\/:][^\\/:]*$")
if i then
prefix = path:sub(1, i-1)
end
return prefix
end
-- Navigates up one level
local function up_one_level(path)
if path == nil then path = '.' end
if path == '.' then path = clink.get_cwd() end
return pathname(path)
end
-- Checks if provided directory contains hg directory
local function has_specified_dir(path, specified_dir)
if path == nil then path = '.' end
local found_dirs = clink.find_dirs(path..'/'..specified_dir)
if #found_dirs > 0 then return true end
return false
end
-- Set default path to current directory
if path == nil then path = '.' end
-- If we're already have .hg directory here, then return current path
if has_specified_dir(path, dirname) then
return path..'/'..dirname
else
-- Otherwise go up one level and make a recursive call
local parent_path = up_one_level(path)
if parent_path == path then
return nil
else
return get_dir_contains(parent_path, dirname)
end
end
end
-- copied from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
function get_hg_dir(path)
return get_dir_contains(path, '.hg')
end
-- * The segments{} table will hold values for each prompt segment to be (sequentially) displayed
---- * text
---- * textColor: Use one of the color constants. Ex: colorWhite
---- * fillColor: Use one of the color constants. Ex: colorBlue
local segments = {}
---
-- Sets the properties of the Segment object, and prepares for a segment to be added
---
local function init()
segments = {}
if get_hg_dir() then
-- if we're inside of hg repo then try to detect current branch
-- 'hg id' gives us BOTH the branch name AND an indicator that there
-- are uncommitted changes, in one fast(er) call
local pipe = io.popen("hg id 2>&1")
local output = pipe:read('*all')
local rc = { pipe:close() }
if output ~= nil and
string.sub(output,1,7) ~= "abort: " and -- not an HG working copy
string.sub(output,1,12) ~= "000000000000" and -- empty wc (needs update)
(not string.find(output, "is not recognized")) then -- 'hg' not in path
local items = {}
for i in string.gmatch(output, "%S+") do
table.insert(items, i)
end
-- Branch segment
table.insert(segments, {" " .. plc_hg_branchSymbol .. " " .. items[2] .. " ", segmentColors.branch.text, segmentColors.branch.fill})
if string.sub(items[1], -1, -1) == "+" then
-- Dirty segment
table.insert(segments, {plc_hg_conflictSymbol, segmentColors.dirty.text, segmentColors.dirty.fill})
end
end
end
end
---
-- Uses the segment properties to add a new segment to the prompt
---
local function addAddonSegment()
init()
for i = 1, #segments do
addSegment(segments[i][1], segments[i][2], segments[i][3])
end
end
-- Register this addon with Clink
clink.prompt.register_filter(addAddonSegment, 61)