Repository navigation
Expand file tree
/
Copy pathcommand_frame.lua
More file actions
697 lines (633 loc) · 22.6 KB
/
Copy pathcommand_frame.lua
File metadata and controls
697 lines (633 loc) · 22.6 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
--------------------------------------------------------------------------------
-- This script automatically inserts divider lines before and after command
-- output. Various settings allow configuring colors and other details about
-- the appearance.
--
-- The configuration settings are controlled by 'clink set'.
--
-- For example, 'clink set command_frame.enable true' turns on the command
-- frames.
--
-- Many commands and programs add an extra blank line at the end of their
-- output, which can look messy with the divider lines. Clink can optionally
-- remove blank lines from the end of command output. You can enable that by
-- running 'clink set prompt.spacing compact' or 'clink set prompt.spacing
-- sparse' (https://chrisant996.github.io/clink/clink.html#prompt_spacing).
--
--
-- PRIMARY ON/OFF SETTING:
--
-- command_frame.enable
-- Set to true to enable printing command frame divider lines
-- before and after command output.
--
--
-- CONFIGURATION SETTINGS:
--
-- command_frame.exit_code_coloring
-- Set to true to color the bottom divider line based on the
-- exit code. Set to false to color it the same as the top.
--
-- command_frame.shape
-- Set to 'round' for rounded corners, or 'square' for squared
-- corners, or 'line' for straight divider lines.
--
-- command_frame.transient_spacing
-- When this is true and 'prompt.spacing' is 'sparse', then a
-- blank line is printed before a transient prompt if a
-- command frame was present for the previous command or if a
-- command frame will be present for the next command (but
-- this can't predict what the 'command_frame.auto_remove'
-- setting will end up doing).
--
-- command_frame.use_ascii
-- Set to true to use only ASCII characters (the minus sign).
--
-- command_frame.auto_remove
-- Set to true to automatically remove command frame divider
-- lines when the exit code is 0 and the duration was short or
-- the amount of output was short.
-- See the 'command_frame.min_duration' and
-- 'command_frame.min_lines' settings.
--
-- command_frame.show_caption
-- Include the command in a caption in the top divider line.
--
-- command_frame.show_cwd
-- Include the current working directory in the top divider
-- line.
--
-- command_frame.show_time
-- Include the date and time in the top divider line.
--
-- command_frame.min_duration
-- Only print the duration in the bottom divider line if it
-- exceeds this many seconds.
--
-- command_frame.min_lines
-- When command_frame.auto_remove is true, then the command
-- frame divider lines may be removed if the command output was
-- fewer than this many lines.
--
-- command_frame.always_commands
-- A space-delimited list of commands. Command frame divider
-- lines are always printed for any command in this list.
--
-- command_frame.never_commands
-- A space-delimited list of commands. Command frame divider
-- lines are never printed for any command in this list.
--
--
-- COLOR SETTINGS:
--
-- color.command_frame_top
-- Color for the top divider line.
--
-- color.command_frame_succeeded
-- Color for the bottom divider line when the exit code is 0
-- and 'command_frame.exit_code_coloring' is true.
--
-- color.command_frame_failed
-- Color for the bottom divider line when the exit code is
-- non-zero and 'command_frame.exit_code_coloring' is true.
--
-- color.command_frame_caption
-- Color for the command caption when the
-- 'command_frame.show_caption' setting is true.
--
-- color.command_frame_duration
-- Color for the command duration in the bottom divider line.
--
--------------------------------------------------------------------------------
if not clink.onendedit or
not console.cellcount or
not console.plaintext or
not os.geterrorlevel or
not clink.parseline or
not console.getcursorpos or
not unicode.char then
print('command_frame.lua requires a newer version of Clink; please upgrade.')
return
end
local settings_list = {
["command_frame.enable"] =
{
default = false,
shortdesc = "Prints a frame around command output",
longdesc =
"When this is true, a frame is printed around command output.\n"..
"Adjust the behavior by configuring other 'command_frame.*' settings.",
},
["command_frame.exit_code_coloring"] =
{
default = true,
shortdesc = "Color bottom line according to exit code",
longdesc =
"When this is false, the bottom line uses 'color.command_frame_top'.\n"..
"When this is true, the bottom line is colored by the exit code:\n"..
"Exit code zero uses 'color.command_frame_succeeded'.\n"..
"Exit code non-zero uses 'color.command_frame_failed'.",
},
["command_frame.shape"] =
{
default = { "round", "square", "line" },
shortdesc = "The corner and line shape",
longdesc =
"This chooses the corner and line shape for the divider lines. This has no"..
"effect when 'command_frame.use_ascii' is true.",
},
["command_frame.transient_spacing"] =
{
default = true,
shortdesc = "May add a blank before transient prompt",
longdesc =
"When this is true and 'prompt.spacing' is 'sparse', then a blank line is\n"..
"printed before a transient prompt if a command frame was present for the\n"..
"previous command or if a command frame will be present for the next command\n"..
"(but this can't predict what the 'command_frame.auto_remove' setting will\n"..
"end up doing).",
},
["command_frame.use_ascii"] =
{
default = false,
shortdesc = "Uses ASCII characters for lines",
longdesc =
"When this is false, Unicode line drawing characters may be used.",
},
["command_frame.auto_remove"] =
{
default = false,
shortdesc = "Remove divider lines for short commands",
longdesc =
"When this is true and command's exit code is 0, then automatically try\n"..
"to remove the divider lines if there was no output, or if the duration\n"..
"was less than 'command_frame.min_duration' and the output was shorter than\n"..
"'command_frame.min_lines' and the command is not in the list from\n"..
"'command_frame.always_commands'.",
},
["command_frame.show_caption"] =
{
default = true,
shortdesc = "Print the command in the top line",
longdesc =
"When this is true, print the command as a caption in the top line.",
},
["command_frame.show_cwd"] =
{
default = false,
shortdesc = "Print the cwd in the top line",
longdesc =
"When this is true, print the current working directory in the top line.",
},
["command_frame.show_time"] =
{
default = false,
shortdesc = "Print the date and time in the top line",
longdesc =
"When this is true, print the date and time in the top line.",
},
["command_frame.min_duration"] =
{
default = 3,
shortdesc = "Minimum duration to print",
longdesc =
"Only print the duration in the bottom line when at least this many seconds.",
},
["command_frame.min_lines"] =
{
default = 8,
shortdesc = "Minimum lines of output",
longdesc =
"When 'command_frame.auto_remove' is true, then the frame may be removed if\n"..
"the output is fewer than this many lines.\n"..
"See the 'command_frame.auto_remove' setting for details.",
},
["command_frame.always_commands"] =
{
default = "",
shortdesc = "Commands that always add a frame",
longdesc =
"When 'command_frame.auto_remove' is true, then the frame may be removed if\n"..
"the command name is not in this list (delimited by spaces).\n"..
"See the 'command_frame.auto_remove' setting for details.",
},
["command_frame.never_commands"] =
{
default = "",
shortdesc = "Commands that never add a frame",
longdesc =
"When a command name is in this list (delimited by spaces), then a frame is\n"..
"never printed for the command.",
},
["color.command_frame_top"] =
{
default = "sgr 38;5;26",
shortdesc = "Color for line before a command",
},
["color.command_frame_failed"] =
{
default = "sgr 38;5;160",
shortdesc = "Color for line after a failed command",
longdesc =
"When the color is blank, 'color.command_frame_top' is used.",
},
["color.command_frame_succeeded"] =
{
default = "sgr 38;5;34",
shortdesc = "Color for line after a successful command",
longdesc =
"When the color is blank, 'color.command_frame_top' is used.",
},
["color.command_frame_caption"] =
{
default = "sgr 38;5;252;48;5;237",
shortdesc = "Color for caption within a divider line",
longdesc =
"When the color is blank, the default terminal color is used.",
},
["color.command_frame_duration"] =
{
default = "", --"sgr 38;5;172;48;5;237",
shortdesc = "Color for duration in a divider line",
longdesc =
"When the color is blank, the color is based on the command's exit code.",
},
}
for name,info in pairs(settings_list) do
if info.longdesc then
settings.add(name, info.default, info.shortdesc, info.longdesc)
else
settings.add(name, info.default, info.shortdesc)
end
end
-- luacheck: globals flexprompt
-- luacheck: globals NONL
local U = unicode.char
local shapes = {}
shapes.round = {
top_chars = { U(0x256d), U(0x2500), U(0x256e) },
bottom_chars = { U(0x2570), U(0x2500), U(0x256f) },
}
shapes.square = {
top_chars = { U(0x250c), U(0x2500), U(0x2510) },
bottom_chars = { U(0x2514), U(0x2500), U(0x2518) },
}
shapes.line = {
top_chars = { U(0x2500) },
bottom_chars = { U(0x2500) },
}
local function sgr(code)
if not code then
return "\x1b[m"
elseif string.byte(code) == 0x1b then
return code
else
return "\x1b["..code.."m"
end
end
local function get_setting(name)
local fullname = "command_frame."..name
local value = settings.get(fullname)
--[[
local dflt = settings_list[fullname].default
dflt = (type(dflt) == "table" and dflt[1]) or dflt
if value ~= dflt then
return value
elseif command_frame[name] ~= nil then
return command_frame[name]
end
--]]
return value
end
local function get_color(name)
local fullname = "color."..name
local value = settings.get(fullname)
--[[
local descr = settings.get(fullname, true)
local deflt = settings_list[fullname].default
if descr ~= deflt then
return value
elseif command_frame["color_"..name] ~= nil then
return command_frame["color_"..name]
end
--]]
return value
end
local function isnilorempty(s)
return s == nil or s == ""
end
local function can_use_lines()
if get_setting("use_ascii") then
return false
elseif flexprompt and flexprompt.get_charset and flexprompt.get_charset() ~= "unicode" then
return false
else
return true
end
end
local function get_top_chars()
local shape = get_setting("shape")
local chars = can_use_lines() and shapes[shape].top_chars or { "-" }
local left_corner = chars[1]
local line_char = chars[3] and chars[2] or chars[1]
local right_corner = chars[3] and chars[3] or chars[1]
return left_corner, line_char, right_corner
end
local function get_bottom_chars()
local shape = get_setting("shape")
local chars = can_use_lines() and shapes[shape].bottom_chars or { "-" }
local left_corner = chars[1]
local line_char = chars[3] and chars[2] or chars[1]
local right_corner = chars[3] and chars[3] or chars[1]
return left_corner, line_char, right_corner
end
local div_begin
local prev_begin_divider
local can_auto_remove
local was_printed
local function add_margin(text)
return isnilorempty(text) and "" or (" "..text.." ")
end
local function trim(text)
return text:gsub("^%s+", ""):gsub("%s+$", "")
end
local function format_error_level(errorlevel)
local text = ""
if type(errorlevel) == "number" and errorlevel ~= 0 then
if errorlevel >= -65535 and errorlevel <= 65535 then
text = string.format(" exit %d ", errorlevel)
else
text = string.format(" exit 0x%x ", bit32.band(errorlevel, 0xffffffff))
end
end
return text
end
local function format_command_duration(duration)
local text = ""
if duration and duration >= tonumber(get_setting("min_duration")) then
local h, m, s
local t = math.floor(duration * 10) % 10
duration = math.floor(duration)
s = (duration % 60)
duration = math.floor(duration / 60)
if duration > 0 then
t = nil -- Don't show tenths of seconds if duration exceeds 60 sec.
m = (duration % 60)
duration = math.floor(duration / 60)
if duration > 0 then
h = duration
end
end
text = s
if t then
text = text.."."..t
end
text = text.."s"
if m then
text = m.."m "..text
if h then
text = h.."h "..text
end
end
text = " took "..text.." "
end
return text
end
local function format_command(line, width)
local text = trim(line)
text = console.ellipsify(text, width or (console.getwidth() / 2))
return add_margin(text)
end
local padding_amount = 2
local function make_divider_begin(line)
local terminal_width = console.getwidth()
local line_color = sgr("0;"..get_color("command_frame_top"))
local text_color = get_color("command_frame_caption")
if isnilorempty(text_color) then
text_color = sgr()
else
text_color = sgr("0;"..text_color)
end
local left_corner, line_char, right_corner = get_top_chars()
local caption_limit = terminal_width / 2
local cwd_limit = terminal_width / 3
local div
for i = 1, 4 do
local div_left = table.concat({
line_color,
left_corner,
string.rep(line_char, padding_amount),
})
if get_setting("show_caption") then
div_left = table.concat({
div_left,
text_color,
format_command(line, caption_limit),
line_color,
string.rep(line_char, padding_amount),
})
end
if get_setting("show_cwd") then
div_left = table.concat({
div_left,
add_margin(console.ellipsify(os.getcwd(), cwd_limit)),
})
end
local div_right = table.concat({
right_corner,
sgr(),
})
if get_setting("show_time") and i <= 2 then
div_right = table.concat({
add_margin(trim(os.date())),
string.rep(line_char, padding_amount),
div_right,
})
end
local used = console.cellcount(div_left) + console.cellcount(div_right)
if used <= terminal_width then
div = table.concat({
div_left,
string.rep(line_char, terminal_width - used),
div_right,
})
break
end
if i ~= 2 then
caption_limit = caption_limit * 9 / 10
cwd_limit = cwd_limit * 9 / 10
end
end
if not div then
local line_width = terminal_width - console.cellcount(left_corner..right_corner)
div = line_color..left_corner..string.rep(line_char, line_width)..right_corner..sgr()
end
return div
end
local function maybe_print_divider_end()
local begin_time = div_begin
if not begin_time then
return
end
local duration = os.clock() - begin_time
local terminal_width = console.getwidth()
local errorlevel = os.geterrorlevel()
local maybe_auto_remove = can_auto_remove and get_setting("auto_remove")
div_begin = nil
can_auto_remove = nil
-- If the exit code was 0 then maybe remove the beginning divider line.
if prev_begin_divider and
(errorlevel or 0) == 0 and
maybe_auto_remove then
local n = 0
local y = console.getnumlines()
local plain = console.plaintext(prev_begin_divider)
local min_duration = tonumber(get_setting("min_duration"))
local min_lines = tonumber(get_setting("min_lines"))
-- Always try to remove if there was no output, or try to remove if the
-- output didn't exceed command_frame.min_lines and the duration didn't
-- exceed command_frame.min_duration.
while n < min_lines do
y = y - 1
n = n + 1
local l = console.getlinetext(y)
if #l >= terminal_width / 2 and l == plain:sub(1, #l) then
local text = string.format("\x1b[%uA\x1b[M", n)
if n > 1 then
text = text..string.format("\x1b[%uB", n - 1)
end
clink.print(text, NONL)
return
end
if duration > min_duration then
break
end
end
end
local line_color
if not errorlevel or not get_setting("exit_code_coloring") then
line_color = sgr("0;"..get_color("command_frame_top"))
elseif tonumber(errorlevel) == 0 then
line_color = sgr("0;"..get_color("command_frame_succeeded"))
else
line_color = sgr("0;"..get_color("command_frame_failed"))
end
local text_color = get_color("command_frame_duration")
if isnilorempty(text_color) then
text_color = line_color
else
text_color = sgr("0;"..text_color)
end
-- Record that a command frame was printed before the current prompt.
was_printed = true
local left_corner, line_char, right_corner = get_bottom_chars()
local div_left = table.concat({
line_color,
left_corner,
string.rep(line_char, padding_amount),
format_error_level(errorlevel),
})
local div_right = table.concat({
text_color,
format_command_duration(duration),
line_color,
string.rep(line_char, padding_amount),
right_corner,
sgr(),
})
local consumed_width = console.cellcount(div_left) + console.cellcount(div_right)
if consumed_width > terminal_width - padding_amount then
local line_width = terminal_width - console.cellcount(left_corner..right_corner)
clink.print(line_color..left_corner..string.rep(line_char, line_width)..right_corner..sgr())
return
end
local div = table.concat({
-- "\r\n",
div_left,
line_color,
string.rep(line_char, terminal_width - consumed_width),
div_right,
})
clink.print(div, NONL)
print()
end
local function needs_frame(line)
if line:gsub(" ", "") == "" then
return
end
if not get_setting("enable") then
return
end
-- Ignore commands that redirect to nul. This also catches hidden commands
-- invoked by the direnv.lua script.
if line:find(">%s*[nN][uU][lL][%s;,+=()]") or
line:find(">%s*[nN][uU][lL]$") then
return
end
local conditional_commands = {}
for _, c in ipairs(string.explode(clink.lower(get_setting("never_commands")))) do
conditional_commands[clink.lower(c)] = "never"
end
for _, c in ipairs(string.explode(clink.lower(get_setting("always_commands")))) do
conditional_commands[clink.lower(c)] = "always"
end
local always
local commands = clink.parseline(line)
for _, c in ipairs(commands) do
local ls = c.line_state
for i = 1, ls:getwordcount() do
local w = path.getbasename(ls:getword(i))
local x = conditional_commands[clink.lower(w)]
if x == "never" then
return
elseif x == "always" then
always = true
end
end
end
return true, always
end
local function is_command_frame_likely()
if rl_state and
rl_state.line_buffer and
needs_frame(rl_state.line_buffer) then
return true
end
end
--------------------------------------------------------------------------------
-- Prompt filters.
local pf_before = clink.promptfilter(-999999)
local pf_after = clink.promptfilter(999999)
function pf_before:filter(prompt) -- luacheck: no unused
maybe_print_divider_end()
end
function pf_after:filter(prompt) -- luacheck: no unused
end
function pf_after:transientfilter(prompt) -- luacheck: no unused
if get_setting("enable") and
settings.get("prompt.spacing") == "sparse" and
get_setting("transient_spacing") and
(was_printed or is_command_frame_likely()) then
return "\n"..prompt
end
end
--------------------------------------------------------------------------------
-- Event handlers.
clink.onendedit(function(line)
was_printed = nil
local need, always = needs_frame(line)
if not need then
return
end
can_auto_remove = not always
local div = make_divider_begin(line)
if div then
local x = console.getcursorpos()
if x > 1 then
print()
end
clink.print(div)
div_begin = os.clock()
prev_begin_divider = div
end
end)