-
Notifications
You must be signed in to change notification settings - Fork 2
/
theme.lua
250 lines (210 loc) · 7.81 KB
/
theme.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
local gitutil = require('gitutil')
-- Source: https://github.com/AmrEldib/cmder-powerline-prompt
local arrowSymbol = ""
local branchSymbol = ""
-- I want to pass it from a function to an other...
local env
-- Resets the prompt
function lambda_prompt_filter()
local cwd = clink.get_cwd()
cwd = string.gsub(cwd, clink.get_env("home"), "~")
env = clink.prompt.value:match('%[33;22;49m%((.+)%).+%[39;22;49m')
local prompt = "\x1b[37;44m {cwd} {git}{hg} {env}\n\x1b[90;40m{lamb} \x1b[0m"
local new_value = string.gsub(prompt, "{cwd}", cwd)
clink.prompt.value = string.gsub(new_value, "{lamb}", ">_")
end
--- 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 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
-- copied from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
local function get_hg_dir(path)
return get_dir_contains(path, '.hg')
end
-- adopted from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
function colorful_hg_prompt_filter()
-- Colors for mercurial status
local colors = {
clean = "\x1b[1;37;40m",
dirty = "\x1b[31;1m",
}
if get_hg_dir() then
-- if we're inside of mercurial repo then try to detect current branch
local branch = get_hg_branch()
if branch then
-- Has branch => therefore it is a mercurial folder, now figure out status
if get_hg_status() then
color = colors.clean
else
color = colors.dirty
end
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", color.."("..branch..")")
return false
end
end
-- No mercurial present or not in mercurial file
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", "")
return false
end
-- copied from clink.lua
-- clink.lua is saved under %CMDER_ROOT%\vendor
local function get_git_dir(path)
-- 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
-- Checks if provided directory contains git directory
local function has_git_dir(dir)
return #clink.find_dirs(dir..'/.git') > 0 and dir..'/.git'
end
local function has_git_file(dir)
local gitfile = io.open(dir..'/.git')
if not gitfile then return false end
local git_dir = gitfile:read():match('gitdir: (.*)')
gitfile:close()
return git_dir and dir..'/'..git_dir
end
-- Set default path to current directory
if not path or path == '.' then path = clink.get_cwd() end
-- Calculate parent path now otherwise we won't be
-- able to do that inside of logical operator
local parent_path = pathname(path)
return has_git_dir(path)
or has_git_file(path)
-- Otherwise go up one level and make a recursive call
or (parent_path ~= path and get_git_dir(parent_path) or nil)
end
---
-- Get the status of working dir
-- @return {bool}
---
function get_git_status()
local file = io.popen("git status --no-lock-index --porcelain -s 2>nul")
for line in file:lines() do
file:close()
return false
end
file:close()
return true
end
-- adopted from clink.lua
-- Modified to add colors and arrow symbols
function colorful_git_prompt_filter()
-- Colors for git status
local colors = {
clean = "\x1b[34;42m"..arrowSymbol.."\x1b[30;42m ",
dirty = "\x1b[34;43m"..arrowSymbol.."\x1b[30;43m ",
}
local closingcolors = {
clean = "", -- " \x1b[32;40m"..arrowSymbol,
dirty = " ±" --\x1b[33;40m"..arrowSymbol,
}
local git_dir = get_git_dir()
if git_dir then
-- if we're inside of git repo then try to detect current branch
local branch = gitutil.get_git_branch(git_dir)
if branch then
-- Has branch => therefore it is a git folder, now figure out status
if get_git_status() then
color = colors.clean
closingcolor = closingcolors.clean
else
color = colors.dirty
closingcolor = closingcolors.dirty
end
--clink.prompt.value = string.gsub(clink.prompt.value, "{git}", color.." "..branch..closingcolor)
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", color..branchSymbol.." "..branch..closingcolor)
return false
end
end
-- No git present or not in git file
if env then
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", "\x1b[34;46m"..arrowSymbol)
else
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", "\x1b[34;40m"..arrowSymbol)
end
return false
end
-- Modified to add colors and arrow symbols to env
function colorful_env_prompt_filter()
local beforeColor = ""
local git_dir = get_git_dir()
if git_dir then
local branch = gitutil.get_git_branch(git_dir)
if branch then
-- Has branch => therefore it is a git folder, now figure out status
if get_git_status() then
if env then
beforeColor = "\x1b[32;46m"..arrowSymbol --cleen
else
beforeColor = "\x1b[32;40m"..arrowSymbol --cleen
end
else
if env then
beforeColor = "\x1b[33;46m"..arrowSymbol --dirty
else
beforeColor = "\x1b[33;40m"..arrowSymbol --dirty
end
end
end
end
if env then
clink.prompt.value = string.gsub(clink.prompt.value, "{env}", beforeColor.."\x1b[37;46m "..env.." \x1b[36;49m"..arrowSymbol.."\x1b[0m")
else
clink.prompt.value = string.gsub(clink.prompt.value, "{env}", beforeColor)
end
end
-- override the built-in filters
clink.prompt.register_filter(lambda_prompt_filter, 55)
clink.prompt.register_filter(colorful_hg_prompt_filter, 60)
clink.prompt.register_filter(colorful_git_prompt_filter, 60)
clink.prompt.register_filter(colorful_env_prompt_filter, 60)
-- helpful for colors