-
Notifications
You must be signed in to change notification settings - Fork 1
/
profile.lua
214 lines (182 loc) · 5.9 KB
/
profile.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
-- bases on Cmder's boot script: https://github.com/cmderdev/cmder/blob/master/config/cmder.lua
local settings = {
color_vsc_unknown = "\x1b[30;1m",
color_vsc_clean = "\x1b[1;37;40m",
color_vsc_dirty = "\x1b[31;1m",
color_lambda = "\x1b[1;30;40m",
color_console = "\x1b[0m",
hg_status_detection = false,
benchmark = false,
}
profile_settings = {
extension_npm_cache = 1,
extension_npm = 1
}
function interp(s, tab)
return (s:gsub('($%b{})', function(w) return tab[w:sub(3, -2)] or w end))
end
---
-- 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 git 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 .git 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
local function get_hg_dir(path)
return get_dir_contains(path, '.hg')
end
local function get_git_dir(path)
return get_dir_contains(path, '.git')
end
---
-- Find out current branch
-- @return {false|mercurial branch name}
---
function get_hg_branch()
for line in io.popen("hg branch 2>nul"):lines() do
local m = line:match("(.+)$")
if m then
return m
end
end
return false
end
---
-- Get the status of working dir
-- @return {bool}
---
function get_hg_status()
for line in io.popen("hg status"):lines() do
return false
end
return true
end
function hg_prompt_filter()
if get_hg_dir() then
local branch = get_hg_branch()
if branch then
if settings.hg_status_detection then
if get_hg_status() then
color = settings.color_vsc_clean
else
color = settings.color_vsc_dirty
end
else
color = settings.color_vsc_unknown
end
clink.prompt.value = clink.prompt.value .. color .." ["..branch.."]"
return false
end
end
return false
end
---
-- Find out current branch
-- @return {false|git branch name}
---
function get_git_branch()
for line in io.popen("git branch 2>nul"):lines() do
local m = line:match("%* (.+)$")
if m then
return m
end
end
return false
end
---
-- Get the status of working dir
-- @return {bool}
---
function get_git_status()
return os.execute("git diff --quiet --ignore-submodules HEAD 2>nul")
end
function git_prompt_filter()
if get_git_dir() then
local branch = get_git_branch()
if branch then
if get_git_status() then
color = settings.color_vsc_clean
else
color = settings.color_vsc_dirty
end
clink.prompt.value = clink.prompt.value .. color.." ["..branch.."]"
return false
end
end
return false
end
function add_modules(path)
local completions_dir = path
for _,lua_module in ipairs(clink.find_files(completions_dir..'*.lua')) do
-- Skip files that starts with _. This could be useful if some files should be ignored
if profile_settings["extension_" .. lua_module:match[[(.*).lua$]]] ~= -1 then
if not string.match(lua_module, '^_.*') then
local filename = completions_dir..lua_module
-- use dofile instead of require because require caches loaded modules
-- so config reloading using Alt-Q won't reload updated modules.
dofile(filename)
end
end
end
end
function main_prompt_decorator()
clink.prompt.value = interp("${old}\n${color_lambda}${lambda} ${color_console}", {
old = clink.prompt.value,
lambda = "➥",
color_lambda = settings.color_lambda,
color_console = settings.color_console,
})
end
function benchmark_call(f_name, ...)
if settings.benchmark then
local time = os.clock()
local result = _G[f_name](arg)
print(f_name .. " : " .. (os.clock() - time))
else
local result = _G[f_name](arg)
end
return result
end
clink.prompt.register_filter(function() benchmark_call("main_prompt_decorator") end, 60)
clink.prompt.register_filter(function() benchmark_call("hg_prompt_filter") end, 50)
clink.prompt.register_filter(function() benchmark_call("git_prompt_filter") end, 50)
local script_dir = debug.getinfo(1, "S").source:match[[^@?(.*[\/])[^\/]-$]]
add_modules(script_dir .. "clink-completions/")