3
3
-- !!! THIS FILE IS OVERWRITTEN WHEN CMDER IS UPDATED
4
4
-- !!! Use "%CMDER_ROOT%\config\<whatever>.lua" to add your lua startup scripts
5
5
6
- -- luacheck: globals clink
6
+ -- luacheck: globals CMDER_SESSION
7
+ -- luacheck: globals uah_color cwd_color lamb_color clean_color dirty_color conflict_color unknown_color
8
+ -- luacheck: globals prompt_homeSymbol prompt_lambSymbol prompt_type prompt_useHomeSymbol prompt_useUserAtHost
9
+ -- luacheck: globals prompt_singleLine prompt_includeVersionControl
10
+ -- luacheck: globals prompt_overrideGitStatusOptIn prompt_overrideSvnStatusOptIn
11
+ -- luacheck: globals clink io.popenyield os.isdir settings.get
7
12
8
13
-- At first, load the original clink.lua file
9
14
-- this is needed as we set the script path to this dir and therefore the original
@@ -151,12 +156,12 @@ local function set_prompt_filter()
151
156
cwd = string.gsub (cwd , clink .get_env (" HOME" ), prompt_homeSymbol )
152
157
end
153
158
154
- uah = ' '
159
+ local uah = ' '
155
160
if prompt_useUserAtHost then
156
161
uah = clink .get_env (" USERNAME" ) .. " @" .. clink .get_env (" COMPUTERNAME" ) .. ' '
157
162
end
158
163
159
- cr = " \n "
164
+ local cr = " \n "
160
165
if prompt_singleLine then
161
166
cr = ' '
162
167
end
@@ -170,7 +175,7 @@ local function set_prompt_filter()
170
175
171
176
local version_control = prompt_includeVersionControl and " {git}{hg}{svn}" or " "
172
177
173
- prompt = " {uah}{cwd}" .. version_control .. cr .. get_lamb_color () .. " {env}{lamb}\x1b [0m "
178
+ local prompt = " {uah}{cwd}" .. version_control .. cr .. get_lamb_color () .. " {env}{lamb}\x1b [0m "
174
179
prompt = string.gsub (prompt , " {uah}" , uah )
175
180
prompt = string.gsub (prompt , " {cwd}" , cwd )
176
181
prompt = string.gsub (prompt , " {env}" , env )
191
196
local function get_dir_contains (path , dirname )
192
197
193
198
-- return parent path for specified entry (either file or directory)
194
- local function pathname (path )
199
+ local function pathname (path ) -- luacheck: ignore 432
195
200
local prefix = " "
196
201
local i = path :find (" [\\ /:][^\\ /:]*$" )
197
202
if i then
@@ -201,14 +206,14 @@ local function get_dir_contains(path, dirname)
201
206
end
202
207
203
208
-- Navigates up one level
204
- local function up_one_level (path )
209
+ local function up_one_level (path ) -- luacheck: ignore 432
205
210
if path == nil then path = ' .' end
206
211
if path == ' .' then path = clink .get_cwd () end
207
212
return pathname (path )
208
213
end
209
214
210
215
-- Checks if provided directory contains git directory
211
- local function has_specified_dir (path , specified_dir )
216
+ local function has_specified_dir (path , specified_dir ) -- luacheck: ignore 432
212
217
if path == nil then path = ' .' end
213
218
local found_dirs = clink .find_dirs (path .. ' /' .. specified_dir )
214
219
if # found_dirs > 0 then return true end
236
241
local function get_git_dir (path )
237
242
238
243
-- return parent path for specified entry (either file or directory)
239
- local function pathname (path )
244
+ local function pathname (path ) -- luacheck: ignore 432
240
245
local prefix = " "
241
246
local i = path :find (" [\\ /:][^\\ /:]*$" )
242
247
if i then
@@ -255,7 +260,8 @@ local function get_git_dir(path)
255
260
local gitfile = io.open (dir .. ' /.git' )
256
261
if not gitfile then return false end
257
262
258
- local git_dir = gitfile :read ():match (' gitdir: (.*)' )
263
+ local line = gitfile :read () or ' '
264
+ local git_dir = line :match (' gitdir: (.*)' )
259
265
gitfile :close ()
260
266
261
267
if os .isdir then -- only available in Clink v1.0.0 and higher
@@ -303,6 +309,9 @@ local function get_git_branch(git_dir)
303
309
local HEAD = head_file :read ()
304
310
head_file :close ()
305
311
312
+ -- If HEAD is missing, something is wrong.
313
+ if not HEAD then return end
314
+
306
315
-- if HEAD matches branch expression, then we're on named branch
307
316
-- otherwise it is a detached commit
308
317
local branch_name = HEAD :match (' ref: refs/heads/(.+)' )
@@ -322,6 +331,9 @@ local function get_hg_branch()
322
331
-- local cmd = "hg prompt \"{branch}{status}{|{patch}}{update}\""
323
332
local cmd = " hg branch 2>nul"
324
333
local file = io.popen (cmd )
334
+ if not file then
335
+ return false
336
+ end
325
337
326
338
for line in file :lines () do
327
339
local m = line :match (" (.+)$" )
339
351
-- Find out current branch
340
352
-- @return {false|svn branch name}
341
353
---
342
- local function get_svn_branch (svn_dir )
354
+ local function get_svn_branch ()
343
355
local file = io_popenyield (" svn info 2>nul" )
356
+ if not file then
357
+ return false
358
+ end
359
+
344
360
for line in file :lines () do
345
361
local m = line :match (" ^Relative URL:" )
346
362
if m then
@@ -359,12 +375,16 @@ end
359
375
---
360
376
local function get_git_status ()
361
377
local file = io_popenyield (" git --no-optional-locks status --porcelain 2>nul" )
378
+ if not file then
379
+ return {}
380
+ end
381
+
362
382
local conflict_found = false
363
383
local is_status = true
364
384
for line in file :lines () do
365
385
local code = line :sub (1 , 2 )
366
386
-- print (string.format("code: %s, line: %s", code, line))
367
- if code == " DD" or code == " AU" or code == " UD" or code == " UA" or code == " DU" or code == " AA" or code == " UU" then
387
+ if code == " DD" or code == " AU" or code == " UD" or code == " UA" or code == " DU" or code == " AA" or code == " UU" then -- luacheck: no max line length
368
388
is_status = false
369
389
conflict_found = true
370
390
break
@@ -374,23 +394,8 @@ local function get_git_status()
374
394
end
375
395
end
376
396
file :close ()
377
- return { status = is_status , conflict = conflict_found }
378
- end
379
-
380
-
381
- ---
382
- -- Get the status of working dir
383
- -- @return {bool}
384
- ---
385
- local function get_hg_status ()
386
- local file = io.popen (" hg status -0" )
387
- for line in file :lines () do
388
- file :close ()
389
- return false
390
- end
391
- file :close ()
392
397
393
- return true
398
+ return { status = is_status , conflict = conflict_found }
394
399
end
395
400
396
401
---
@@ -399,13 +404,17 @@ end
399
404
---
400
405
local function get_svn_status ()
401
406
local file = io_popenyield (" svn status -q" )
402
- for line in file :lines () do
407
+ if not file then
408
+ return { error = true }
409
+ end
410
+
411
+ for line in file :lines () do -- luacheck: ignore 512, no unused
403
412
file :close ()
404
- return false
413
+ return { clean = false }
405
414
end
406
415
file :close ()
407
416
408
- return true
417
+ return { clean = true }
409
418
end
410
419
411
420
---
@@ -433,24 +442,28 @@ local function get_git_status_setting()
433
442
end
434
443
435
444
local gitStatusConfig = io_popenyield (" git --no-pager config cmder.status 2>nul" )
436
- for line in gitStatusConfig :lines () do
437
- if string.match (line , ' false' ) then
438
- gitStatusConfig :close ()
439
- last_git_status_setting = false
440
- return false
445
+ if gitStatusConfig then
446
+ for line in gitStatusConfig :lines () do
447
+ if string.match (line , ' false' ) then
448
+ gitStatusConfig :close ()
449
+ last_git_status_setting = false
450
+ return false
451
+ end
441
452
end
453
+ gitStatusConfig :close ()
442
454
end
443
- gitStatusConfig :close ()
444
455
445
456
local gitCmdStatusConfig = io_popenyield (" git --no-pager config cmder.cmdstatus 2>nul" )
446
- for line in gitCmdStatusConfig :lines () do
447
- if string.match (line , ' false' ) then
448
- gitCmdStatusConfig :close ()
449
- last_git_status_setting = false
450
- return false
457
+ if gitCmdStatusConfig then
458
+ for line in gitCmdStatusConfig :lines () do
459
+ if string.match (line , ' false' ) then
460
+ gitCmdStatusConfig :close ()
461
+ last_git_status_setting = false
462
+ return false
463
+ end
451
464
end
465
+ gitCmdStatusConfig :close ()
452
466
end
453
- gitCmdStatusConfig :close ()
454
467
455
468
last_git_status_setting = true
456
469
return true
@@ -536,8 +549,6 @@ local function hg_prompt_filter()
536
549
return false
537
550
end
538
551
539
- local result = " "
540
-
541
552
local hg_dir = get_hg_dir ()
542
553
if hg_dir then
543
554
-- Colors for mercurial status
@@ -559,16 +570,20 @@ local function hg_prompt_filter()
559
570
local color = colors .clean
560
571
561
572
local pipe = io.popen (" hg status -amrd 2>&1" )
562
- local output = pipe :read (' *all' )
563
- local rc = { pipe :close () }
573
+ if pipe then
574
+ output = pipe :read (' *all' )
575
+ pipe :close ()
576
+ if output ~= nil and output ~= " " then color = colors .dirty end
577
+ end
564
578
565
- if output ~= nil and output ~= " " then color = colors .dirty end
566
- result = color .. " (" .. branch .. " )"
579
+ local result = color .. " (" .. branch .. " )"
580
+ clink .prompt .value = string.gsub (clink .prompt .value , " {hg}" , " " .. verbatim (result ))
581
+ return false
567
582
end
568
583
end
569
584
570
- clink . prompt . value = string.gsub ( clink . prompt . value , " {hg} " , " " .. verbatim ( result ))
571
- return false
585
+ -- No hg present or not in hg repo
586
+ clink . prompt . value = string.gsub ( clink . prompt . value , " {hg} " , " " )
572
587
end
573
588
574
589
local function svn_prompt_filter ()
@@ -589,7 +604,6 @@ local function svn_prompt_filter()
589
604
if svn_dir then
590
605
-- if we're inside of svn repo then try to detect current branch
591
606
local branch = get_svn_branch ()
592
- local color
593
607
if branch then
594
608
-- If in a different repo or branch than last time, discard cached info
595
609
if cached_info .svn_dir ~= svn_dir or cached_info .svn_branch ~= branch then
@@ -599,7 +613,7 @@ local function svn_prompt_filter()
599
613
end
600
614
-- Get the svn status using coroutine if available and option is enabled. Otherwise use a blocking call
601
615
local svnStatus
602
- if clink .promptcoroutine and io .popenyield and settings .get (" prompt.async" ) and prompt_overrideSvnStatusOptIn then
616
+ if clink .promptcoroutine and io .popenyield and settings .get (" prompt.async" ) and prompt_overrideSvnStatusOptIn then -- luacheck: no max line length
603
617
svnStatus = clink_promptcoroutine (function ()
604
618
return get_svn_status ()
605
619
end )
@@ -613,9 +627,10 @@ local function svn_prompt_filter()
613
627
svnStatus = get_svn_status ()
614
628
end
615
629
616
- if svnStatus == nil then
630
+ local color
631
+ if not svnStatus or svnStatus .error then
617
632
color = colors .nostatus
618
- elseif svnStatus then
633
+ elseif svnStatus . clean then
619
634
color = colors .clean
620
635
else
621
636
color = colors .dirty
0 commit comments