-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_print.txt
1749 lines (1456 loc) · 60.8 KB
/
easy_print.txt
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
--@name Easy Print
--@author legokidlogan
--@shared
--@include lkl/color_scheme.txt
--[[
- A library for standardizing and simplifying colored prints.
- Makes heavy use of my global colors library (gColors), though custom colors can be used as well.
- Each function returns a sequential table 'printTbl' filled with alternating color and string values, for use with print( unpack( printTbl ) )
- Ex:
local items = { entOne, entTwo, "hello" }
local printTbl = easyPrint.chipNamePrefix()
table.Add( printTbl, easyPrint.simpleList( nil, items ) )
print( unpack( printTbl ) )
==>
printTbl = {
c_white, "[MyChipName] ",
c_yellow, tostring( entOne ),
c_white, ", ",
c_yellow, tostring( entTwo ),
c_white, ", ",
c_yellow, "hello"
}
- To modify the colors used, either pass a custom ColorScheme into the functions, or modify the base "EasyPrint" color scheme.
- The EasyPrint scheme can be gotten from either:
A.
require( "lkl/easy_print.txt" )
local epScheme = ColorScheme.getColorScheme( "EasyPrint" )
B.
local epScheme = require( "lkl/easy_print.txt" )
C.
require( "lkl/easy_print.txt" )
local epScheme = easyPrint.getColorScheme()
- To modify color schemes, see the 'color_scheme.txt' library documentation.
- If you use a custom color scheme, it's recommended to have its base set to the EasyPrint scheme, since there are extra color types it adds.
- These are the special color types (and their default values) that get added by the EasyPrint color scheme:
- "ChipNamePrefix": c_white -- Color used for the chip name prefix, e.g. "[ChipName]" in "[ChipName] Hello World!"
- The standard format of most easyPrint functions are as follows:
easyPrint.functionName( colorScheme, ..., postWhitespace ) return printTbl end
colorScheme: (ColorScheme or nil)
- The color scheme to use for the print. Defaults to the EasyPrint color scheme.
...: (any)
- Arguments specific to the function.
postWhitespace: (optional) (string)
- A whitespace string to append to the end of the print. (e.g. " " for a space, "\n" for a newline)
RETURNS: printTbl
printTbl: (table)
- A sequential table filled with alternating color and string values, for use with print( unpack( printTbl ) )
- Can easily be added onto previous printTbls with table.Add( printTbl, easyPrint.functionName( ... ) )
- If an easyPrint function needs varargs, then the arguments will typically follow this order instead:
easyPrint.functionName( colorScheme, postWhitespace, ... ) return printTbl end
- For shorter code snippets, it's reccommended to do local ep = easyPrint after your require() statement.
--]]
easyPrint = easyPrint or {}
if easyPrint._alreadyLoaded then return easyPrint.getColorScheme() end
easyPrint._alreadyLoaded = true
require( "lkl/color_scheme.txt" )
projectNameID = projectNameID or ( "EasyPrint|" .. chip():getChipName() )
printName = printName or ( "[" .. projectNameID .. "]" )
local ep = easyPrint
local epScheme = ColorScheme.createIfNew( "EasyPrint", nil, c_white, {
["ChipNamePrefix"] = c_white,
} )
local mMax = math.max
local mCeil = math.ceil
local mRound = math.round
local stringRep = string.rep
local stringSub = string.sub
local stringSplit = string.split
local stringChar = string.char
local stringMatch = string.match
local stringUpper = string.upper
local tableInsert = table.insert
local tableRemove = table.remove
local tableAdd = table.add
local tableIsEmpty = table.isEmpty
local tableGetKeys = table.getKeys
local preparePointStyle
local getPointStr
local addDescription
local makePlayerLookup
local stringIsUpper
local formatString
----- GLOBAL FUNCTIONS -----
--[[
- Similar to print(), but allows for automatic color scheme usage and gColor conversion.
- Only supports the multicolor format of print(), not the single-color, multi-string format.
- i.e. use this for print( c_red, "Hello", c_green, "World" ) instead of print( "Hello", "World" )
- If you're converting a project to use color schemes, then you can use this function to swap out the old print() statements.
- ex:
print(
c_white, "[SomeChipName] ",
c_alert_red, "Hey! ",
c_white, "You broke ",
c_yellow, "this thing",
c_white, "! You should do ",
c_yellow, "this ",
c_white, "instead."
)
Replaced with:
easyPrint.printInScheme( myColorScheme,
"ChipNamePrefix", "[SomeChipName] ",
"Alert", "Hey! ",
"Text", "You broke ",
"Highlight", "this thing",
"Text", "! You should do ",
"Highlight", "this ",
"Text", "instead."
)
colorScheme: (ColorScheme or nil)
- The color scheme to use for fallbacks, if an invalid color object is found.
- Defaults to the EasyPrint color scheme.
...: (pairs of (color or string), string)
- A list of alternating color and string values, similar to print().
- For each 'color' it can be one of these, evaluated in this order:
- Color: A raw color object.
- string: A color name from the color scheme. (e.g. "Highlight")
- string: The name of a gColor from gcolors.txt (e.g. "c_red" or "::c_red" or "red" or "::c(255,0,0,255)")
- ex:
easyPrint.printInScheme( myColorScheme,
"c_white", "[Test] ",
c_pale_blue, "Hello! ",
"c_yellow", "This ",
"::c_red", "is ",
"green", "a ",
"::c(0,0,255,255)", "test ",
"Pending", "print!"
)
--]]
function ep.printInScheme( colorScheme, ... )
colorScheme = colorScheme or epScheme
local stringyTbl = { ... }
local printTbl = ep.colorizePrintTable( colorScheme, stringyTbl )
print( unpack( printTbl ) )
end
--[[
- For a standard print table (which has alternating Color and string values), converts the Colors into string forms.
- Returns the converted table, does not self-modify.
- Primarily used for optimizing net messages.
- ex:
local printTbl = {
c_yellow, "hello",
c_white, "world",
}
local printTblStr = easyPrint.stringifyPrintTable( printTbl )
==>
printTblStr = {
"::c_yellow", "hello",
"::c_white", "world",
}
colorScheme: (ColorScheme or nil)
- The color scheme to use for fallbacks, if an invalid Color object is found.
- Defaults to the EasyPrint color scheme.
printTbl: (table)
- The print table to convert.
--]]
function ep.stringifyPrintTable( colorScheme, printTbl )
colorScheme = colorScheme or epScheme
local colorToString = gColors.colorToString
local fallbackColorStr = colorToString( colorScheme:getFallbackColor() ) or "::c_white"
local stringyTbl = {}
for i = 1, #printTbl, 2 do
local iPlusOne = i + 1
local color = printTbl[i]
local str = printTbl[iPlusOne]
stringyTbl[i] = colorToString( color ) or fallbackColorStr
stringyTbl[iPlusOne] = str
end
return stringyTbl
end
--[[
- Turns a 'color string' into a Color object, according to the colorScheme and gColors.
- If the color string is invalid, then it will return the fallback color from the color scheme.
- Processes color strings in this order:
- Color: A raw color object.
- string: A color name from the color scheme. (e.g. "Highlight")
- string: The name of a gColor from gcolors.txt (e.g. "c_red" or "::c_red" or "red" or "::c(255,0,0,255)")
colorScheme: (ColorScheme or nil)
- The color scheme to use for fallbacks and interpreting scheme color types (e.g. "Highlight")
- Defaults to the EasyPrint color scheme.
colorStr: (string)
- The string to convert to a color.
ignoreSchemeColors: (bool or nil)
- If true, then the color string will never be interepreted as a color scheme type, saving performance when iterated across a table.
- This is for if you are cetain that you only have a raw Color or a gColor string.
- Defaults to false.
--]]
function ep.colorizeString( colorScheme, colorStr, ignoreSchemeColors )
colorScheme = colorScheme or epScheme
local fallbackColor = colorScheme:getFallbackColor() or c_white
if not ignoreSchemeColors and type( colorStr ) == "string" then
local retVals = { colorScheme:hasColor( colorStr ) }
local hasColor = tableRemove( retVals, 1 )
local colors = retVals
if hasColor then
if type( colors[1] ) == "Color" then
return unpack( colors )
end
return fallbackColor
end
end
return gColors.stringToColor( colorStr ) or fallbackColor
end
--[[
- The reverse of easyPrint.stringifyPrintTable()
- Iterates easyPrint.colorizeString() over the color strings in stringyTbl.
- Returns a new table, does not self-modify stringyTbl.
--]]
function ep.colorizePrintTable( colorScheme, stringyTbl, ignoreSchemeColors )
colorScheme = colorScheme or epScheme
local printTbl = {}
local colorizeString = ep.colorizeString
for i = 1, #stringyTbl, 2 do
local iPlusOne = i + 1
local colorStr = stringyTbl[i]
local str = stringyTbl[iPlusOne]
local color = colorizeString( colorScheme, colorStr, ignoreSchemeColors )
--printTbl[i] = stringToColor( colorStr ) or fallbackColor
printTbl[i] = color
printTbl[iPlusOne] = str
end
return printTbl
end
function ep.getColorScheme()
return ColorScheme.getColorScheme( "EasyPrint" )
end
--[[
- The prefix for all prints, e.g. "[MyChipName] "
- You should ALWAYS start off your print tables with this, as it helps with identifying the source of various prints from all sorts of chips.
colorScheme: (ColorScheme or nil)
- The color scheme to use.
- Defaults to the EasyPrint color scheme.
- For all future functions that use colorScheme as an arg, assume they share this behavior.
printNameOverride: (optional) (string)
- The full string used for the print prefix. Recommended to not have spaces.
- Defaults to the global var 'printName' which gets defined as "[" .. projectNameID .. "]"
- projectNameID is another global variable, one that's best to define before require()'ing this file, or any other of my libraries.
- As the name implies, it's the title of the project, e.g. "Anti-AFK" or "Teleporter" or whatever.
- Some of my libraries, such as this one, will automatically default this value if unpecified, based on chip():getChipName()
postWhitespace: (optional) (string)
- A whitespace string to append to the end of the print. (e.g. " " for a space, "\n" for a newline)
- Unlike ALL OTHER easyPrint functions, this defaults to " " instead of ""
--]]
function ep.chipNamePrefix( colorScheme, printNameOverride, postWhitespace )
colorScheme = colorScheme or epScheme
printNameOverride = printNameOverride or printName
postWhitespace = postWhitespace or " "
return {
colorScheme:getColor( "ChipNamePrefix" ), printNameOverride .. postWhitespace,
}
end
--[[
- Colorizes a vector, with each axis being a different color.
vec: (Vector)
- The vector to colorize.
decimals: (optional) (number)
- The number of decimals to round the vector to.
- Defaults to 2.
--]]
function ep.vector( colorScheme, vec, decimals, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
decimals = decimals or 2
local cText = colorScheme:getColor( "Text", nil, vec )
local cHighlightWeak = colorScheme:getColor( "HighlightWeak", nil, vec )
local _, c1, c2, c3 = colorScheme:getColor( "Vector", nil, vec )
return {
cHighlightWeak, "Vector",
cText, "( ",
c1, tostring( mRound( vec[1], decimals ) ),
cText, ", ",
c2, tostring( mRound( vec[2], decimals ) ),
cText, ", ",
c3, tostring( mRound( vec[3], decimals ) ),
cText, " )" .. postWhitespace,
}
end
--[[
- Colorizes an angle, with each axis being a different color.
ang: (Angle)
- The angle to colorize.
decimals: (optional) (number)
- The number of decimal to round the vector to.
- Defaults to 2.
--]]
function ep.angle( colorScheme, ang, decimals, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
decimals = decimals or 2
local cText = colorScheme:getColor( "Text", nil, ang )
local cHighlightWeak = colorScheme:getColor( "HighlightWeak", nil, ang )
local _, c1, c2, c3 = colorScheme:getColor( "Angle", nil, ang )
return {
cHighlightWeak, "Angle",
cText, "( ",
c1, tostring( mRound( ang[1], decimals ) ),
cText, ", ",
c2, tostring( mRound( ang[2], decimals ) ),
cText, ", ",
c3, tostring( mRound( ang[3], decimals ) ),
cText, " )" .. postWhitespace,
}
end
--[[
- Colorizes a color.
color: (Color)
- The angle to colorize.
decimals: (optional) (number)
- The number of decimal to round the color to.
- Defaults to 2.
alphaOverride: (optional) (number)
- If specified, overrides the alpha used for the coloration of the printTbl.
--]]
function ep.color( colorScheme, color, decimals, alphaOverride, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
decimals = decimals or 2
local cText = colorScheme:getColor( "Text", nil, color )
local cHighlightWeak = colorScheme:getColor( "HighlightWeak", nil, color )
local c = colorScheme:getColor( "Color", nil, color )
if alphaOverride then
c.a = alphaOverride
end
return {
cHighlightWeak, "Color",
cText, "( ",
c, tostring( mRound( color.r, decimals ) ),
cText, ", ",
c, tostring( mRound( color.g, decimals ) ),
cText, ", ",
c, tostring( mRound( color.b, decimals ) ),
cText, ", ",
c, tostring( mRound( color.a, decimals ) ),
cText, " )" .. postWhitespace,
}
end
--[[
- Colorizes an arbitrary value, based on its type.
obj: (any)
- The value to colorize.
- If it's a color, alphaOverride will be 255.
--]]
function ep.anyType( colorScheme, obj, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
local objType = type( obj )
if objType == "Vector" then
return ep.vector( colorScheme, obj, nil, postWhitespace )
elseif objType == "Angle" then
return ep.angle( colorScheme, obj, nil, postWhitespace )
elseif objType == "Color" then
return ep.color( colorScheme, obj, nil, 255, postWhitespace )
end
local fallbackColor = colorScheme:getColor( "Highlight", nil, obj ) or c_yellow
local color
if objType == "table" then
color = colorScheme:getColorAuto( obj, fallbackColor )
else
color = colorScheme:getColor( objType, fallbackColor, obj )
end
return {
color, tostring( obj ) .. postWhitespace,
}
end
--[[
- Colorizes a player, yielding their name instead of tostring( ply ).
- Color is based on the player's team.
ply: (Player)
- The player to colorize the name of.
--]]
function ep.playerName( colorScheme, ply, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
local color = colorScheme:getColor( "Player", nil, ply )
if not isValid( ply ) or not ply:isPlayer() then
return {
color, "[INVALID_PLAYER_NAME]" .. postWhitespace,
}
end
return {
color, ply:getName() .. postWhitespace,
}
end
--[[
- For a command called by caller onto target, observed by observer, colorizes the caller.
- If the caller is the observer, it will use "You" instead of their name.
- Note that, unlike easyPrint.commandPlayerAsTarget(), the target arg goes unused here.
caller: (Player or Entity or nil or false)
- The player who called the command.
- If an Entity, it will be colorized as an entity. Useful for fancy prints for automated, entity-driven commands.
- If nil or false, it will be colorized as "[CONSOLE]".
_: (unused)
- Placeholder slot to line up with the target argument in easyPrint.commandPlayerAsTarget()
observer: (Player)
- The player who observed the command.
dontCapitalize: (optional) (boolean)
- If true, "You" will not be capitalized, in the event that caller == observer.
- ex:
'Bob called a command on Alice, observed by Charlie.'
==>
{
teamColorOfBob, "Bob",
}
- ex:
'Bob called a command on Charlie, observed by Charlie.'
==>
{
teamColorOfBob, "Bob",
}
- ex:
'Charlie called a command on Bob, observed by Charlie.'
==>
{
colorSelf, "You",
}
--]]
function ep.commandPlayerAsCaller( colorScheme, caller, _, observer, dontCapitalize, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
local callerType = type( caller )
if caller == observer then
local text = dontCapitalize and "you" or "You"
return {
colorScheme:getColor( "PlayerSelf" ), text .. postWhitespace,
}
end
if callerType == "Player" then
return ep.playerName( colorScheme, caller, postWhitespace )
end
if callerType == "Entity" then
return {
colorScheme:getColor( "Entity", nil, caller ), tostring( caller ) .. postWhitespace,
}
end
return {
colorScheme:getColor( "Console" ), "[CONSOLE]" .. postWhitespace,
}
end
--[[
- For a command called by caller onto target, observed by observer, colorizes the target.
- If the target is the observer, it will use "Yourself" or "You" depending on the caller.
- If the target is the caller (but not the observer), then it will use "Themself"
- If the target is a list of targets, two tables will be returned:
- The first table will be a list of colorized print tables, in the same order as the list of targets.
- The second table will be a lookup table, from target to colorized print table.
caller: (Player or Entity or nil or false)
- The player who called the command.
- If an Entity, it will be colorized as an entity. Useful for fancy prints for automated, entity-driven commands.
- If nil or false, it will be colorized as "[CONSOLE]".
target: (Player)
- The player who was targeted by the command.
observer: (Player)
- The player who observed the command.
dontCapitalize: (optional) (boolean)
- If true, "You", etc will not be capitalized.
- ex:
'Bob called a command on Alice, observed by Charlie.'
==>
{
teamColorOfAlice, "Alice",
}
- ex:
'Bob called a command on Bob, observed by Charlie.'
==>
{
teamColorOfBob, "Themself",
}
- ex:
'Bob called a command on Charlie, observed by Charlie.'
==>
{
colorSelf, "You",
}
- ex:
'Charlie called a command on Bob, observed by Charlie.'
==>
{
teamColorOfBob, "Bob",
}
- ex:
'Charlie called a command on Charlie, observed by Charlie.'
==>
{
colorSelf, "Yourself",
}
- ex:
'Bob called a command on Alice and Dennis, observed by Charlie.'
==>
{
[1] = { teamColorOfAlice, "Alice" },
[2] = { teamColorOfDennis, "Dennis" },
},
{
[Alice] = { teamColorOfAlice, "Alice" },
[Dennis] = { teamColorOfDennis, "Dennis" },
}
--]]
function ep.commandPlayerAsTarget( colorScheme, caller, target, observer, dontCapitalize, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
local targetType = type( target )
if targetType == "table" then
local printTblList = {}
local printTblLookup = {}
for i, ply in ipairs( target ) do
if type( ply ) == "table" then
error( "Cannot have sub-tables in target list" )
end
local printTbl = ep.commandPlayerAsTarget( colorScheme, caller, ply, observer, dontCapitalize, postWhitespace )
printTblList[i] = printTbl
printTblLookup[ply] = printTbl
end
return printTblList, printTblLookup
end
if target == observer then
if caller == observer then
local text = dontCapitalize and "yourself" or "Yourself"
return {
colorScheme:getColor( "PlayerSelf" ), text .. postWhitespace,
}
end
local text = dontCapitalize and "you" or "You"
return {
colorScheme:getColor( "PlayerSelf" ), text .. postWhitespace,
}
end
if target == caller then
local text = dontCapitalize and "themself" or "Themself"
return { -- Still uses the caller's color, to mimic how ULX only uses the self-target color for when it represents the observer.
colorScheme:getColor( "Player", nil, target ), text .. postWhitespace,
}
end
if targetType == "Player" then
return ep.playerName( colorScheme, target, postWhitespace )
end
if targetType == "Entity" then
return {
colorScheme:getColor( "Entity", nil, target ), tostring( target ) .. postWhitespace,
}
end
return { -- Realistically, the console can't ever be the 'target' of a command, but for consistency's sake
colorScheme:getColor( "Console" ), "[CONSOLE]" .. postWhitespace,
}
end
--[[
- Formats a command print table, similarly to ULX.
formatStr: (string)
- The format string to use.
- These are the specifiers:
- #A: The caller's name. (uses an 'A' to mimic ULX, standing for 'admin')
- #a vs #A will determine dontCapitalize when passed to easyPrint.commandPlayerAsCaller
- #T: The target(s)'s name(s).
- #t vs #T will determine dontCapitalize when passed to easyPrint.commandPlayerAsTarget
- #s: Additional strings.
caller: (Player)
- The player who called the command.
targets: (Player or table)
- The player(s) who were targeted by the command.
observer: (Player)
- The player who observed the command.
postWhitespace: (optional) (string)
- The string to append to the end of the print. Defaults to "", like usual.
...: (optional) (string)
- Additional strings to use in the format string, to replace the instances of #s.
--]]
function ep.command( colorScheme, formatStr, caller, targets, observer, postWhitespace, ... )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
if type( formatStr ) ~= "string" then
error( "Expected formatStr to be a string" )
end
if type( targets ) ~= "table" then
targets = { targets }
end
local callerTbl = ep.commandPlayerAsCaller( colorScheme, caller, targets, observer, true, "" )
local callerTblCap = ep.commandPlayerAsCaller( colorScheme, caller, targets, observer, false, "" )
local targetsTbl = ep.commandPlayerAsTarget( colorScheme, caller, targets, observer, true, "" )
local targetsTblCap = ep.commandPlayerAsTarget( colorScheme, caller, targets, observer, false, "" )
local targetsPrintTbl
local targetsPrintTblCap
local printTbl = {}
local cText = colorScheme:getColor( "Text" )
local stringArgs = { ... }
local function buildTargetsPrintTbl( _, val, _ )
return val -- val is a printTbl from ep.commandPlayerAsTarget, use it as that element's print table
end
local function getTargetsPrintTbl( cap )
if cap then
if targetsPrintTblCap then return targetsPrintTblCap end
targetsPrintTblCap = ep.simpleList( colorScheme, targetsTblCap, buildTargetsPrintTbl, true, true, "" )
return targetsPrintTblCap
else
if targetsPrintTbl then return targetsPrintTbl end
targetsPrintTbl = ep.simpleList( colorScheme, targetsTbl, buildTargetsPrintTbl, true, true, "" )
return targetsPrintTbl
end
end
--string.gsub( formatStr, "([^#]*)#([%.%d]*[%a])([^#]*)", function( prefix, tag, postfix )
formatString( formatStr, "#", function( prefix, specifier ) -- Starfall has a harsh limit on the length for gsub
if prefix and prefix ~= "" then
tableAdd( printTbl, { cText, prefix } )
end
--local specifier = string.sub( tag, -1, -1 )
local cap = stringIsUpper( specifier )
specifier = stringUpper( specifier )
if specifier == "A" then
tableAdd( printTbl, cap and callerTblCap or callerTbl )
elseif specifier == "T" then
tableAdd( printTbl, getTargetsPrintTbl( cap ) )
elseif specifier == "S" then
local arg = tableRemove( stringArgs, 1 )
if arg then
tableAdd( printTbl, { cText, tostring( arg ) } )
end
end
if postfix and postfix ~= "" then
tableAdd( printTbl, { cText, postfix } )
end
end )
printTbl[#printTbl] = printTbl[#printTbl] .. postWhitespace
return printTbl
end
--[[
- Formats a command print, and sends it to all players, automatically adjusting the observer accordingly.
- Uses the same format as easyPrint.command()
- If run on the server, the command will be sent to each player not on the hideFrom list.
- If run on the client, will only proccess the print for the local player.
- Note that this command automatically runs print(), and does not return a print table.
hideFrom: (optional) (Player or table)
- The player(s) to hide the command from.
- Only used on serverside.
--]]
function ep.commandLog( colorScheme, formatStr, caller, targets, hideFrom, ... )
colorScheme = colorScheme or epScheme
if type( formatStr ) ~= "string" then
error( "Expected formatStr to be a string" )
end
if CLIENT then
local printTbl = ep.chipNamePrefix( colorScheme )
tableAdd( printTbl, ep.command( colorScheme, formatStr, caller, targets, player(), "", ... ) )
print( unpack( printTbl ) )
return
end
if type( hideFrom ) == "Player" then
hideFrom = { hideFrom }
end
hideFrom = makePlayerLookup( hideFrom or {} )
for _, ply in ipairs( find.allPlayers() ) do
if not hideFrom[ply] then
local printTbl = ep.chipNamePrefix( colorScheme )
tableAdd( printTbl, ep.command( colorScheme, formatStr, caller, targets, ply, "", ... ) )
ep.printToClient( ply, printTbl )
end
end
end
--[[
- Makes a list of colorized values, separated by commas.
- Does not traverse into sub-tables.
list: (table)
- The list of values to colorize.
- Must be a sequential table.
func: (optional) (function or Color)
- A function of the form function( colorScheme, val, index ) return printTbl end
printTbl: (table)
- The printTbl to use for the given value in the list.
val: (any)
- The value to colorize, taken from the list.
index: (number)
- The index of the given value in the list.
- Specifies how to colorize (create a printTbl for) each value individually. Does not handle the commas, etc.
- Defaults to easyPrint.anyType(). (approximately, modifies it to discard extra whitespace)
- If provided as a Color, will simply use that color for all values, and tostring() for the text.
sayAnd: (optional) (boolean)
- Whether to say "and" before the last value.
- Defaults to false.
noOxfordComma: (optional) (boolean)
- Whether to ommit the oxford comma. (i.e. "a, b, and c" vs "a, b and c")
- Only applies if sayAnd is true.
- Defaults to false.
--]]
function ep.simpleList( colorScheme, list, func, sayAnd, noOxfordComma, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
func = func or function( a, b ) return ep.anyType( a, b, "" ) end -- Don't pass index as postWhitespace to ep.anyType()
if type( func ) == "Color" then
local color = func
func = function( _, val ) return { color, tostring( val ) } end
end
local printTbl = {}
local listCount = #list
if listCount == 0 then
return printTbl
end
local listCountMinusOne = listCount - 1
local cText = colorScheme:getColor( "Text", nil, list )
for i = 1, listCount do
local val = list[i]
local valPrintTbl = func( colorScheme, val, i )
for _, v in ipairs( valPrintTbl ) do
tableInsert( printTbl, v )
end
if i < listCount then
if sayAnd and i == listCountMinusOne then
if noOxfordComma then
tableInsert( printTbl, cText )
tableInsert( printTbl, " and " )
else
tableInsert( printTbl, cText )
tableInsert( printTbl, ", and " )
end
else
tableInsert( printTbl, cText )
tableInsert( printTbl, ", " )
end
end
end
local printTblCount = #printTbl
printTbl[printTblCount] = printTbl[printTblCount] .. postWhitespace
return printTbl
end
--[[
- Makes a list of colorized values, separated by commas.
- Does not traverse into sub-tables.
- Unlike easyPrint.simpleList(), this function will include the keys of the provided table.
- Keys will be colored with 'HighlightWeak'.
tbl: (table)
- The table of values to colorize. Keys will be included in the output.
keyOrder: (optional (table)
- A sequential table of keys to use for the order of the output.
- Can also be used to only include certain keys, by omitting unwanted ones.
- Defaults to table.getKeys( tbl ).
func: (optional) (function)
- Behaves the same as in easyPrint.simpleList().
- Only modifies the appearance of the value, not the key.
doVertical: (optional) (boolean)
- Whether to print the list vertically.
- Defaults to false.
doBraces: (optional) (boolean or number)
- Whether to print the list in braces.
- Defaults to false.
- If doVertical is true, each entry will be indented by a number of spaces.
- If doBraces is a number, it will be used as the number of spaces.
- If doBraces is true, it will be 2 spaces.
- ex:
easyPrint.keyValueList( nil, { a = 1, b = 2, c = 3 } )
==>
a = 1, b = 2, c = 3
- ex:
easyPrint.keyValueList( nil, { a = 1, b = 2, c = 3 }, nil, nil, true, false )
==>
a = 1,
b = 2,
c = 3
- ex:
easyPrint.keyValueList( nil, { a = 1, b = 2, c = 3 }, nil, nil, false, true )
==>
{ a = 1, b = 2, c = 3 }
- ex: For quick colored debug prints of local variables:
easyPrint.keyValueList( nil, { a = a, b = b, c = c } )
==>
a = 1, b = 2, c = 3
- ex: For a fancy debug print of the arguments passed to a known function:
local function someFunc( arg1, arg2, arg3 )
-- ...
end
local function test_someFunc( arg1, arg2, arg3 )
local printTbl = easyPrint.chipNamePrefix()
tableAdd( printTbl, {
c_white, "Testing someFunc( "
} )
tableAdd( printTbl, easyPrint.keyValueList( nil,
{ arg1 = arg1, arg2 = arg2, arg3 = arg3 },
{ "arg1", "arg2", "arg3" }
) )
tableAdd( printTbl, {
c_white, " ):\n",
} )
print( unpack( printTbl ) )
someFunc( arg1, arg2, arg3 )
end
test_someFunc( 1, nil, 3 )
==>
[ChipName] Testing someFunc( arg1 = 1, arg2 = nil, arg3 = 3 ):
...
--]]
function ep.keyValueList( colorScheme, tbl, keyOrder, func, doVertical, doBraces, postWhitespace )
colorScheme = colorScheme or epScheme
postWhitespace = postWhitespace or ""
keyOrder = keyOrder or tableGetKeys( tbl )
func = func or function( a, b ) return ep.anyType( a, b, "" ) end -- Don't pass index as postWhitespace to ep.anyType()
local printTbl = {}
local cText = colorScheme:getColor( "Text", nil, tbl )
if tableIsEmpty( keyOrder ) then
tableInsert( printTbl, cText )
tableInsert( printTbl, ( doBraces and "{}" or "" ) .. postWhitespace )
return printTbl
end
local cHighlightWeak = colorScheme:getColor( "HighlightWeak", nil, tbl )
local commaStr = doVertical and ",\n" or ", "
local indentSize = isnumber( doBraces ) and doBraces or 2
local indentStr = string.rep( " ", indentSize )
indentStr = doVertical and doBraces and indentStr or ""
if doBraces then
if doVertical then
tableInsert( printTbl, cText )
tableInsert( printTbl, "{\n" )
else
tableInsert( printTbl, cText )
tableInsert( printTbl, "{ " )
end
end
for _, key in ipairs( keyOrder ) do
local val = tbl[key]
local valPrintTbl = func( colorScheme, val, key )
tableInsert( printTbl, cHighlightWeak )
tableInsert( printTbl, indentStr .. tostring( key ) )
tableInsert( printTbl, cText )
tableInsert( printTbl, " = " )
for _, v in ipairs( valPrintTbl ) do
tableInsert( printTbl, v )
end
tableInsert( printTbl, cText )
tableInsert( printTbl, commaStr )
end
-- Remove last comma since the loop doesn't know when it's on the last iteration
tableRemove( printTbl )
tableRemove( printTbl )
if doBraces then
if doVertical then
tableInsert( printTbl, cText )
tableInsert( printTbl, "\n}" )