-
Notifications
You must be signed in to change notification settings - Fork 17
/
flexprompt.lua
3223 lines (2811 loc) · 106 KB
/
flexprompt.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
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--------------------------------------------------------------------------------
-- Clink requirements.
--
-- Notes:
-- - Transient prompt support requires Clink v1.2.29 or higher.
-- - Right prompt support requires Clink v1.2.24 or higher.
-- - Exit code support requires Clink v1.2.14 or higher.
-- - Async prompt filtering requires Clink v1.2.10 or higher.
if ((clink and clink.version_encoded) or 0) < 10020010 then
print("clink-flex-prompt requires Clink v1.2.10 or higher.")
return
end
settings.add(
"flexprompt.enable",
true,
"Enable flexprompt.",
"Setting this to false disables the flexprompt prompt filter.\n" ..
"Takes effect on the next Clink session.")
if not settings.get("flexprompt.enable") then
log.info("Flexprompt is disabled by the 'flexprompt.enable' setting.")
return
end
--------------------------------------------------------------------------------
-- Internals.
-- luacheck: no max line length
-- luacheck: globals console io os unicode
-- luacheck: globals string.equalsi string.matchlen
-- luacheck: globals _error_handler NONL
-- luacheck: globals flexprompt
-- luacheck: globals CMDER_SESSION prompt_includeVersionControl
flexprompt = flexprompt or {}
flexprompt.settings = flexprompt.settings or {}
flexprompt.settings.symbols = flexprompt.settings.symbols or {}
flexprompt.defaultargs = flexprompt.defaultargs or {}
local modules = {}
local scms = {}
local vpns = {}
-- Is reset to {} at each onbeginedit.
local _cached_state = {}
-- Check if Clink natively supporting prompt spacing.
local clink_prompt_spacing = (settings.get("prompt.spacing") ~= nil)
--------------------------------------------------------------------------------
-- Color codes.
local realblack = { fg="30", bg="40", extfg="38;5;0", extbg="48;5;0" }
local realwhite = { fg="37", bg="47", extfg="38;5;7", extbg="48;5;7", altcolor=realblack }
local nearlywhite = { fg="37", bg="47", extfg="38;5;252", extbg="48;5;252" }
flexprompt.colors =
{
bold = { fg="1" },
default = { fg="39", bg="49" },
-- Normal low intensity colors. Some styles brighten the normal low
-- intensity colors; the "dark" versions are never brightened.
black = { fg="30", bg="40", lean="brightblack", classic="brightblack", },
red = { fg="31", bg="41", lean="brightred", classic="brightred", },
green = { fg="32", bg="42", lean="brightgreen", classic="brightgreen", },
yellow = { fg="33", bg="43", lean="brightyellow", classic="brightyellow", },
blue = { fg="34", bg="44", lean="brightblue", classic="brightblue", },
magenta = { fg="35", bg="45", lean="brightmagenta", classic="brightmagenta", },
cyan = { fg="36", bg="46", lean="brightcyan", classic="brightcyan", },
white = { fg="37", bg="47", lean="brightwhite", classic="brightwhite", },
-- High intensity colors.
brightblack = { fg="90", bg="100", },
brightred = { fg="91", bg="101", },
brightgreen = { fg="92", bg="102", },
brightyellow = { fg="93", bg="103", },
brightblue = { fg="94", bg="104", },
brightmagenta = { fg="95", bg="105", },
brightcyan = { fg="96", bg="106", },
brightwhite = { fg="97", bg="107", },
-- Low intensity colors. Some styles brighten the normal low intensity
-- colors; the "dark" versions are never brightened.
darkblack = { fg="30", bg="40", },
darkred = { fg="31", bg="41", },
darkgreen = { fg="32", bg="42", },
darkyellow = { fg="33", bg="43", },
darkblue = { fg="34", bg="44", },
darkmagenta = { fg="35", bg="45", },
darkcyan = { fg="36", bg="46", },
darkwhite = { fg="37", bg="47", },
-- Real colors. These use the real color (vs console theme color) when
-- extended colors are available.
realblack = realblack,
realred = { fg="31", bg="41", extfg="38;5;1", extbg="48;5;1" },
realgreen = { fg="32", bg="42", extfg="38;5;2", extbg="48;5;2" },
realyellow = { fg="33", bg="43", extfg="38;5;3", extbg="48;5;3" },
realblue = { fg="34", bg="44", extfg="38;5;4", extbg="48;5;4" },
realmagenta = { fg="35", bg="45", extfg="38;5;5", extbg="48;5;5" },
realcyan = { fg="36", bg="46", extfg="38;5;6", extbg="48;5;6" },
realwhite = realwhite,
realbrightblack = { fg="91", bg="101", extfg="38;5;8", extbg="48;5;8" },
realbrightred = { fg="91", bg="101", extfg="38;5;9", extbg="48;5;9" },
realbrightgreen = { fg="92", bg="102", extfg="38;5;10", extbg="48;5;10" },
realbrightyellow = { fg="93", bg="103", extfg="38;5;11", extbg="48;5;11" },
realbrightblue = { fg="94", bg="104", extfg="38;5;12", extbg="48;5;12" },
realbrightmagenta = { fg="95", bg="105", extfg="38;5;13", extbg="48;5;13" },
realbrightcyan = { fg="96", bg="106", extfg="38;5;14", extbg="48;5;14" },
realbrightwhite = { fg="97", bg="107", extfg="38;5;15", extbg="48;5;15" },
-- Default text color in rainbow style.
rainbow_text = nearlywhite,
-- Version control colors.
vcs_blacktext = realblack,
vcs_whitetext = nearlywhite,
vcs_conflict = { fg="91", bg="101", extfg="38;5;160", extbg="48;5;160", rainbow={ fg="31", bg="41", extfg="38;5;1", extbg="48;5;1", altcolor=nearlywhite } },
vcs_unresolved = { fg="91", bg="101", extfg="38;5;160", extbg="48;5;160", rainbow={ fg="31", bg="41", extfg="38;5;1", extbg="48;5;1", altcolor=realblack } },
vcs_clean = { fg="92", bg="102", extfg="38;5;40", extbg="48;5;40", rainbow={ fg="32", bg="42", extfg="38;5;2", extbg="48;5;2", altcolor=realblack } },
vcs_dirty = { fg="93", bg="103", extfg="38;5;11", extbg="48;5;11", rainbow={ fg="33", bg="43", extfg="38;5;178", extbg="48;5;178", altcolor=realblack } },
vcs_staged = { fg="95", bg="105", extfg="38;5;164", extbg="48;5;164", rainbow={ fg="35", bg="45", extfg="38;5;5", extbg="48;5;5", altcolor=realblack } },
vcs_unpublished = { fg="95", bg="105", extfg="38;5;141", extbg="48;5;141", rainbow={ fg="35", bg="45", extfg="38;5;99", extbg="48;5;99", altcolor=realblack } },
vcs_remote = { fg="96", bg="106", extfg="38;5;44", extbg="48;5;44", rainbow={ fg="36", bg="46", extfg="38;5;6", extbg="48;5;6", altcolor=realblack } },
vcs_unknown = realwhite,
-- Exit code colors.
exit_zero = { fg="32", bg="42", extfg="38;5;2", extbg="48;5;2", rainbow={ fg="30", bg="40", extfg="38;5;0", extbg="48;5;0", altcolor={ fg="32", bg="42", extfg="38;5;34", extbg="48;5;34" } } },
exit_nonzero = { fg="91", bg="101", extfg="38;5;160", extbg="48;5;160", rainbow={ fg="31", bg="41", extfg="38;5;1", extbg="48;5;1", altcolor={ fg="93", bg="103", extfg="38;5;11", extbg="48;5;11" } },
classic={ fg="91", bg="101", extfg="38;5;196", extbg="48;5;196" } },
}
--------------------------------------------------------------------------------
-- Configuration.
flexprompt.choices = {}
flexprompt.choices.charsets =
{
ascii = "ascii",
unicode = "unicode",
}
flexprompt.choices.styles =
{
lean = "lean",
classic = "classic",
rainbow = "rainbow",
}
flexprompt.choices.sides =
{
left = "left",
both = "both",
}
-- Default prompt strings based on styles and sides.
flexprompt.choices.prompts =
{
lean = { left = { "{battery}{histlabel}{cwd}{git}{duration}{time}" }, both = { "{battery}{histlabel}{cwd}{git}", "{exit}{duration}{time}" } },
classic = { left = { "{battery}{histlabel}{cwd}{git}{exit}{duration}{time}" }, both = { "{battery}{histlabel}{cwd}{git}", "{exit}{duration}{time}" } },
rainbow = { left = { "{battery:breakright}{histlabel}{cwd}{git}{exit}{duration}{time:dim}" }, both = { "{battery:breakright}{histlabel}{cwd}{git}", "{exit}{duration}{time}" } },
breaks = { left = { "{battery:breakright}{histlabel}{cwd}{break}{git}{break}{exit}{duration}{break}{time:dim}" }, both = { "{battery}{break}{histlabel}{cwd}{break}{git}", "{exit}{duration}{break}{time}" } },
}
-- Only if style != lean.
flexprompt.choices.ascii_caps =
{
-- Open Close
none = { "", "" },
flat = { "", "", separators="bar" },
}
-- Only if style != lean.
flexprompt.choices.caps =
{
-- Open Close
none = { "", "" },
flat = { "", "" },
vertical = { "", "" }, -- A separator when style == rainbow.
pointed = { "", "" },
slant = { "", "" },
backslant = { "", "" },
round = { "", "" },
blurred = { "░▒▓", "▓▒░" },
}
-- Only if style == classic.
flexprompt.choices.separators =
{ -- Left Right
none = { "", "" },
space = { " ", " ", lean=" " }, -- Also when style == lean.
spaces = { " ", " ", lean=" " }, -- Also when style == lean.
vertical = { "│", "│", rainbow="" },
pointed = { "", "" },
slant = { "", "" },
backslant = { "", "" },
round = { "", "" },
dot = { "·", "·" },
updiagonal = { "╱", "╱" },
downdiagonal= { "╲", "╲" },
bar = { "|", "|" },
slash = { "/", "/" },
backslash = { "\\", "\\" },
}
flexprompt.choices.lines =
{
one = 1,
two = 2,
}
-- Only if lines > 1 and right_prompt is not nil.
flexprompt.choices.connections =
{
disconnected= " ",
dotted = "·",
solid = "─",
dashed = "-",
}
-- Only if lines > 1.
flexprompt.choices.left_frames =
{
none = {},
square = { "┌─", "└─" },
round = { "╭─", "╰─" },
}
-- Only if lines > 1 and right_prompt is not nil.
flexprompt.choices.right_frames =
{
none = {},
square = { "─┐", "─┘" },
round = { "─╮", "─╯" },
}
-- Only if separators or connectors or frames.
local fc_frame = 1
local fc_back = 2
local fc_fore = 3
local fc_sep = 4
flexprompt.choices.frame_colors =
{ -- Frame Back Fore Separator (optional; falls back to Frame)
lightest = { "38;5;244", "38;5;240", "38;5;248" },
light = { "38;5;242", "38;5;238", "38;5;246" },
dark = { "38;5;240", "38;5;236", "38;5;244" },
darkest = { "38;5;238", "38;5;234", "38;5;242" },
}
flexprompt.choices.spacing =
{
compact = "compact",
normal = "normal",
sparse = "sparse",
}
flexprompt.choices.flows =
{
concise = "concise",
fluent = "fluent",
}
flexprompt.choices.transient =
{
"off",
"same dir",
"always",
}
-- Only if lines > 1 and left frame none, or if lines == 1 and style == lean, or if transient.
flexprompt.choices.prompt_symbols =
{
angle = { ">" }, -- unicode="❯" looks very good in some fonts, and is missing in some fonts.
dollar = { "$" },
percent = { "%" },
}
local symbols =
{
branch = { powerline="" },
unpublished = { nerdfonts2={""," "}, nerdfonts3={""," "} },
submodule = { nerdfonts2={""," "}, nerdfonts3={""," "} },
conflict = { "!" },
addcount = { "+" },
modifycount = { "*" },
deletecount = { "-" },
renamecount = { "" }, -- Empty string counts renames as modified.
summarycount = { "*", unicode="±" },
untrackedcount = { "?" },
aheadbehind = { "" }, -- Optional symbol preceding ahead/behind counts.
aheadcount = { ">>", unicode="↓" },
behindcount = { "<<", unicode="↑" },
staged = { "#", unicode="↗" },
battery = { "%" },
charging = { "++", nerdfonts2={""," "}, nerdfonts3={""," "} },
smartcharging = { "%", unicode="♥" },
-- Note: coloremoji for exit_zero requires Clink v1.4.28 or higher.
exit_zero = { coloremoji="✔️", nerdfonts2={"\x1b[92m\002","\x1b[92m \002"}, nerdfonts3={"\x1b[92m\002","\x1b[92m \002"} },
exit_nonzero = { coloremoji="❌", nerdfonts2={"\x1b[91m\002","\x1b[91m \002"}, nerdfonts3={"\x1b[91m\002","\x1b[91m \002"} },
prompt = { ">" },
overtype_prompt = { ">", unicode="►" },
admin = { powerline="" },
no_admin = { nerdfonts2={""," "}, nerdfonts3={""," "} },
vpn = { coloremoji="☁️", nerdfonts2={"",""}, nerdfonts3=" " },
no_vpn = { coloremoji="🌎", nerdfonts2={""," "}, nerdfonts3={""," "} },
refresh = { nerdfonts2="", nerdfonts3=" " }, --
}
if ((clink.version_encoded) or 0) < 10040028 then
-- Remove the coloremoji for exit_zero if the Clink version is too low.
symbols.exit_zero.coloremoji = nil
end
--------------------------------------------------------------------------------
-- Wizard state.
local _wizard
local function get_errorlevel()
if os.geterrorlevel and settings.get("cmd.get_errorlevel") then
if _wizard then return _wizard.exit or 0 end
return os.geterrorlevel()
end
end
--------------------------------------------------------------------------------
-- Configuration helpers.
local pad_right_edge = " "
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 getcolortable = console.getcolortable
if not getcolortable then
getcolortable = function()
return {
"#0c0c0c", "#0037da", "#13a10e", "#3a96dd",
"#c50f1f", "#881798", "#c19c00", "#cccccc",
"#767676", "#3b78ff", "#16c60c", "#61d6d6",
"#e74856", "#b4009e", "#f9f1a5", "#f2f2f2",
foreground=8, background=1, default=true,
}
end
end
local ansi_to_vga =
{
0, 4, 2, 6, 1, 5, 3, 7,
8, 12, 10, 14, 9, 13, 11, 15,
}
local function rgb_from_colortable(num)
num = num and ansi_to_vga[num + 1]
if not num then
return
end
local colortable = getcolortable()
if not colortable or not colortable[num + 1] then
return
end
local r, g, b = colortable[num + 1]:match("^#(%x%x)(%x%x)(%x%x)$")
if not r or not g or not b then
return
end
r = tonumber(r, 16)
g = tonumber(g, 16)
b = tonumber(b, 16)
return r, g, b
end
local cube_series = { 0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff }
local function rgb_from_color(inner)
local pro, r, g, b, epi = inner:match("^([34]8;2;)(%d+);(%d+);(%d+)(.*)$")
if pro and r and g and b and epi then
return pro, r, g, b, epi
end
local cube
pro, cube, epi = inner:match("^([34]8;5;)(%d+)(.*)$") -- luacheck: no unused
if cube then
cube = tonumber(cube)
if cube < 0 or cube > 255 then
return
elseif cube <= 15 then
cube = cube
r, g, b = rgb_from_colortable(cube)
if not r or not g or not b then
return
end
elseif cube >= 232 and cube <= 255 then
r = 8 + ((cube - 232) * 10)
g = r
b = r
else
cube = cube - 16
r = math.floor(cube / 36)
cube = cube - r * 36
g = math.floor(cube / 6)
cube = cube - g * 6
b = cube
r = cube_series[r + 1]
g = cube_series[g + 1]
b = cube_series[b + 1]
end
pro = inner:sub(1, 1).."8;2;"
return pro, r, g, b, epi
end
local n = 0
local bold = false
local tag = 0
epi = ""
for i = 1, #inner + 1 do
local x = inner:byte(i)
if x == 0x3b or not x then
if n == 1 then
bold = true
elseif (n >= 30 and n <= 37) or
(n >= 40 and n <= 47) or
(n >= 90 and n <= 97) or
(n >= 100 and n <= 107) or
n == 39 or n == 49 then
if tag == 0 then
tag = n
end
else
if #epi > 0 then
epi = epi..";"
end
epi = epi..tostring(n)
end
if not x then
break
end
n = 0
elseif x >= 0x30 and x <= 0x39 then
n = (n * 10) + (x - 0x30)
elseif x == 0x6d then
break
else
return
end
end
if tag == 0 then
tag = 39
end
if tag >= 90 then
bold = true
tag = tag - 60
end
local fore = (tag >= 30 and tag < 40)
tag = math.fmod(tag, 10) + (bold and 8 or 0)
r, g, b = rgb_from_colortable(tag)
if not r or not g or not b then
return
end
return (fore and "38;2;" or "48;2;"), r, g, b, epi
end
local function blend_color(code1, code2, opacity1)
opacity1 = opacity1 or 0.5
if opacity1 < 0 or opacity1 > 1 then
return
end
local inner1 = code1:match("^\x1b%[(.*)m$") or code1
local inner2 = code2:match("^\x1b%[(.*)m$") or code2
if inner1:find("\x1b") or inner2:find("\x1b") then
return
end
local pro1, r1, g1, b1, epi1 = rgb_from_color(inner1)
if not pro1 or not r1 or not g1 or not b1 then
return
end
local _, r2, g2, b2 = rgb_from_color(inner2)
if not r2 or not g2 or not b2 then
return
end
local r = math.floor((tonumber(r1) * opacity1) + (tonumber(r2) * (1 - opacity1)))
local g = math.floor((tonumber(g1) * opacity1) + (tonumber(g2) * (1 - opacity1)))
local b = math.floor((tonumber(b1) * opacity1) + (tonumber(b2) * (1 - opacity1)))
local ret = pro1..string.format("%u;%u;%u", r, g, b)..epi1
if inner1 ~= code1 then
ret = sgr(ret)
end
return ret
end
local _can_use_extended_colors = nil
local function can_use_extended_colors(force)
if _can_use_extended_colors == nil or force then
_can_use_extended_colors = flexprompt.settings.use_8bit_color
if _can_use_extended_colors == nil then
_can_use_extended_colors = false
if clink.getansihost then
local host = clink.getansihost()
if host == "winconsolev2" or host == "winterminal" then
_can_use_extended_colors = true
end
end
end
end
return _can_use_extended_colors
end
local function get_best_fg(color)
if can_use_extended_colors() then
return color.extfg or color.fg
end
return color.fg
end
local function get_best_bg(color)
if can_use_extended_colors() then
return color.extbg or color.bg
end
return color.bg
end
local function use_best_color(normal, extended)
return can_use_extended_colors() and extended or normal
end
local function get_style()
-- Indexing into the styles table validates that the style name is
-- recognized.
return flexprompt.choices.styles[flexprompt.settings.style or "lean"] or "lean"
end
local _charset
local function get_charset()
if not _charset then
-- Indexing into the charsets table validates that the charset name is
-- recognized.
if flexprompt.settings.no_graphics then
_charset = "ascii"
else
_charset = flexprompt.choices.charsets[flexprompt.settings.charset or "unicode"] or "unicode"
end
end
return _charset
end
local _nerdfonts_version
local function get_nerdfonts_version()
if not _nerdfonts_version then
local ver
local t = type(flexprompt.settings.nerdfonts_version)
if t == "string" or t == "number" then
ver = tostring(flexprompt.settings.nerdfonts_version)
if ver == "" then
ver = nil
end
end
if not ver then
local env = os.getenv("FLEXPROMPT_NERDFONTS_VERSION")
if env then
local num = tonumber(env:gsub("^ +", ""))
if num then
ver = tostring(num)
if ver == "" then
ver = nil
end
end
end
end
if ver then
_nerdfonts_version = "nerdfonts" .. ver
end
end
return not flexprompt.settings.no_graphics and _nerdfonts_version or nil
end
local _nerdfonts_width
local function get_nerdfonts_width()
if not _nerdfonts_width then
if flexprompt.settings.nerdfonts_width == 2 then
_nerdfonts_width = 2
else
local env = os.getenv("FLEXPROMPT_NERDFONTS_WIDTH")
if env and tonumber(env) == 2 then
_nerdfonts_width = 2
else
_nerdfonts_width = 1
end
end
end
return _nerdfonts_width
end
local function get_lines()
return flexprompt.choices.lines[flexprompt.settings.lines or "one"] or 1
end
local function get_spacing()
if clink_prompt_spacing then
return "normal"
else
-- Indexing into the spacing table validates that the spacing name is
-- recognized.
return flexprompt.choices.spacing[flexprompt.settings.spacing or "normal"] or "normal"
end
end
local function get_connector()
local connector = flexprompt.settings.connection or "disconnected"
if flexprompt.choices.connections[connector] then
return flexprompt.choices.connections[connector]
end
if console.cellcount(connector) == 1 then
return connector
end
return " "
end
local function lookup_color(args)
if not args then
return args
end
if type(args) == "table" then
return args[get_style()] or args
end
if args and not args:match("^[0-9]") then
local color = flexprompt.colors[args]
if color then
local redirect = color[get_style()]
if redirect then
if type(redirect) == "table" then
color = redirect
else
color = flexprompt.colors[redirect]
end
end
end
return color
end
local mode = args:sub(1,3)
if mode == "38;" or mode == "48;" then
args = args:sub(4)
return { fg = "38;"..args, bg = "48;"..args }
end
-- Use the color even though it's not understood. But not in rainbow style,
-- because that can garble segment transitions.
if get_style() ~= "rainbow" then
return { fg = args, bg = args }
end
end
local function get_frame()
if not _charset then get_charset() end
if _charset == "ascii" then return end
local l = flexprompt.choices.left_frames[flexprompt.settings.left_frame or "none"]
local r = flexprompt.choices.right_frames[flexprompt.settings.right_frame or "none"]
if l and #l == 0 then
l = nil
end
if r and #r == 0 then
r = nil
end
return l, r
end
local function get_frame_color()
local frame_color = flexprompt.settings.frame_color or "light"
if type(frame_color) ~= table then
frame_color = flexprompt.choices.frame_colors[frame_color] or frame_color
end
if type(frame_color) ~= "table" then
frame_color = { frame_color, frame_color, frame_color }
end
frame_color =
{
lookup_color(frame_color[fc_frame]),
lookup_color(frame_color[fc_back]),
lookup_color(frame_color[fc_fore]),
lookup_color(frame_color[fc_sep] or frame_color[fc_frame]),
}
return frame_color
end
local function resolve_symbol_table(symbol)
if type(symbol) ~= "table" then
return symbol
else
local ret
if not flexprompt.settings.no_graphics then
local term = clink.getansihost and clink.getansihost() or nil
if flexprompt.settings.use_color_emoji and term == "winterminal" and symbol["coloremoji"] then
ret = symbol["coloremoji"]
elseif term and symbol[term] then
ret = symbol[term]
else
local nf = get_nerdfonts_version()
if symbol[nf] then
symbol = symbol[nf]
if type(symbol) == "table" then
if get_nerdfonts_width() == 2 then
ret = symbol[2] or symbol[1]
else
ret = symbol[1]
end
else
ret = symbol
end
elseif flexprompt.settings.powerline_font and symbol["powerline"] then
ret = symbol["powerline"]
end
end
end
if not ret then
local charset = get_charset()
ret = symbol[charset] or symbol[1]
end
return ret
end
end
local function get_symbol(name, fallback)
local settings_symbols = flexprompt.settings.symbols
local symbol = settings_symbols and settings_symbols[name] or symbols[name] or fallback or ""
symbol = resolve_symbol_table(symbol)
if not symbol then
symbol = resolve_symbol_table(symbols[name] or fallback or "")
end
return symbol or ""
end
local function get_icon(name)
if flexprompt.settings.no_graphics or not flexprompt.settings.use_icons then return "" end
if type(flexprompt.settings.use_icons) == "table" and not flexprompt.settings.use_icons[name] then return "" end
return get_symbol(name)
end
local function get_prompt_symbol_color()
local color
if flexprompt.settings.prompt_symbol_color then
color = flexprompt.settings.prompt_symbol_color
else
local err = get_errorlevel()
if err then
color = (err == 0) and
(flexprompt.settings.exit_zero_color or "realbrightgreen") or
(flexprompt.settings.exit_nonzero_color or "realbrightred")
end
end
color = color or "brightwhite"
color = lookup_color(color)
return sgr(get_best_fg(color))
end
local function get_prompt_symbol()
local p = nil
if rl.insertmode and not rl.insertmode() then
p = get_symbol("overtype_prompt", "►")
end
return p or get_symbol("prompt", ">")
end
local function get_transient_prompt_symbol()
local p = get_symbol("transient_prompt")
if p ~= "" then
return p
end
return get_symbol("prompt", ">")
end
local function get_flow()
-- Indexing into the flows table validates that the flow name is recognized.
return flexprompt.choices.flows[flexprompt.settings.flow or "concise"] or "concise"
end
local function make_fluent_text(text, force)
if not force and get_style() == "rainbow" then
return text
else
local t = type(force)
if t == "string" or t == "table" then
local color = lookup_color(force)
if color then
return sgr(get_best_fg(color)) .. text .. "\002"
end
end
return "\001" .. text .. "\002"
end
end
local function get_screen_width()
return _wizard and _wizard.width or console.getwidth()
end
local function connect(lhs, rhs, frame, sgr_frame_color)
local lhs_len = console.cellcount(lhs)
local rhs_len = console.cellcount(rhs)
local frame_len = console.cellcount(frame)
local width = get_screen_width() - #pad_right_edge
local gap = width - (lhs_len + rhs_len + frame_len)
local dropped
if gap < 0 then
gap = gap + rhs_len
rhs_len = 0 -- luacheck: no unused
rhs = ""
if gap < 0 then
gap = gap + frame_len
frame_len = 0 -- luacheck: no unused
frame = ""
end
dropped = true
end
if gap > 0 then
if not sgr_frame_color then
sgr_frame_color = sgr(get_best_fg(flexprompt.colors.red))
end
lhs = lhs .. sgr_frame_color .. string.rep(get_connector(), gap)
end
return lhs..rhs..frame, dropped
end
local _refilter_modules
local _module_results = {}
local function refilter_module(module)
_refilter_modules = _refilter_modules or {}
_refilter_modules[module] = true
end
local function reset_render_state(keep_results)
_can_use_extended_colors = nil
_charset = nil
_nerdfonts_version = nil
_nerdfonts_width = nil
_wizard = nil
_refilter_modules = nil
if not keep_results then
_module_results = {}
end
end
local list_on_reset_render_state = {}
local function add_on_reset_render_state(func)
table.insert(list_on_reset_render_state, func)
end
--------------------------------------------------------------------------------
-- Other helpers.
local function spairs(t, order)
local keys = {}
local num = 0
for k in pairs(t) do
num = num + 1
keys[num] = k
end
if order then
table.sort(keys, function(a,b) return order(t, a, b) end)
else
table.sort(keys)
end
local i = 0
return function()
i = i + 1
if keys[i] then
return keys[i], t[keys[i]]
end
end
end
local function get_parent(dir)
local parent = path.toparent(dir)
if parent and parent ~= "" and parent ~= dir then
return parent
end
end
local function has_dir(dir, subdir)
local test = path.join(dir, subdir)
return os.isdir(test) and test or nil
end
local function has_file(dir, file)
local test = path.join(dir, file)
return os.isfile(test) and test or nil
end
local function append_text(lhs, rhs)
if not lhs then return rhs and tostring(rhs) or "" end
if not rhs then return tostring(lhs) end
lhs = tostring(lhs)
rhs = tostring(rhs)
if #lhs > 0 and #rhs > 0 then
return lhs .. " " .. rhs
else
return lhs .. rhs
end
end
local function maybe_apply_tilde(dir, force)
if force or flexprompt.settings.use_home_tilde then
local home = os.getenv("HOME")
if home and string.find(string.lower(dir), string.lower(home), 1, true--[[plain]]) == 1 then
dir = "~" .. string.sub(dir, #home + 1)
return dir, true
end
end
return dir
end
local function get_default_prompts()
local style = get_style()
local prompts = flexprompt.choices.prompts[style]["both"]
local left_prompt = prompts[1]
local right_prompt = prompts[2]
if flexprompt.default_prompt_append then
left_prompt = left_prompt .. (flexprompt.default_prompt_append.left_prompt or "")
right_prompt = (flexprompt.default_prompt_append.right_prompt or "") .. right_prompt
end
return left_prompt, right_prompt
end
local function is_module_in_prompt(name)
local pattern = "{" .. name .. "[:}]"
local top_prompt = flexprompt.settings.top_prompt
local left_prompt = flexprompt.settings.left_prompt
local right_prompt = flexprompt.settings.right_prompt
if not top_prompt and not left_prompt and not right_prompt then
left_prompt, right_prompt = get_default_prompts()
end
local is = 0
if top_prompt and top_prompt:match(pattern) then
is = is + 1
end
if left_prompt and left_prompt:match(pattern) then
is = is + 1
end
if right_prompt and right_prompt:match(pattern) then
is = is + 2
end
if is > 0 then
return is
end
end
local function unicode_normalize(s)
if unicode.normalize then
return unicode.normalize(3, s)
else
return s
end
end
local function first_letter(s)
if unicode.iter then
-- This handles combining marks, but does not yet handle ZWJ (0x200d)
-- such as in emoji sequences.
local letter = ""
for codepoint, value, combining in unicode.iter(s) do
if value == 0x200d then
break
elseif not combining and #letter > 0 then
break
end
letter = letter .. codepoint
end
return letter
else
return s:sub(1, 1)
end
end
local function abbrev_child(parent, child)
local letter = first_letter(child)
if not letter or letter == "" then
return child, false
end