Skip to content

Commit ba71c19

Browse files
committed
feat: allow non-str types in colorize function
The colorize function currently only allows strings, so you have to always convert the inputs yourself before invoking. This is a bit annoying, we should be able to handle e.g. lists of bools. Let's just accept all types and stringify them. This might not behave well with odd types such as function references, but eh, that's on the user. In future we might behave more gracefully there.
1 parent 4a89eff commit ba71c19

File tree

4 files changed

+69
-5
lines changed

4 files changed

+69
-5
lines changed

core/func_colorize.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
var FuncColorize = BuiltInFunc{
1515
Name: FUNC_COLORIZE,
1616
Execute: func(f FuncInvocation) RadValue {
17-
strVal := f.GetStr("_val").Plain()
17+
strVal := ToPrintableQuoteStr(f.GetArg("_val"), false)
1818
enum := lo.Uniq(f.GetList("_enum").AsStringList(false))
1919
skipIfSingle := f.GetBool("skip_if_single")
2020

core/testing/color_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,3 +303,67 @@ display:
303303
assertOnlyOutput(t, stdOutBuffer, expected)
304304
assertNoErrors(t)
305305
}
306+
307+
func Test_Colorize_CanColorInts(t *testing.T) {
308+
script := `
309+
nums = [1, 2, 3]
310+
for n in nums:
311+
n.colorize(nums).print()
312+
`
313+
setupAndRunCode(t, script, "--color=always")
314+
expected := "\x1b[38;2;230;38;25m1\x1b[0;22;0;0;0m\n\x1b[38;2;99;130;233m2\x1b[0;22;0;0;0m\n\x1b[38;2;106;189;15m3\x1b[0;22;0;0;0m\n"
315+
assertOnlyOutput(t, stdOutBuffer, expected)
316+
assertNoErrors(t)
317+
}
318+
319+
func Test_Colorize_CanColorBools(t *testing.T) {
320+
script := `
321+
bools = [true, false]
322+
for b in bools:
323+
b.colorize(bools).print()
324+
`
325+
setupAndRunCode(t, script, "--color=always")
326+
expected := "\x1b[38;2;99;130;233mtrue\x1b[0;22;0;0;0m\n\x1b[38;2;230;38;25mfalse\x1b[0;22;0;0;0m\n"
327+
assertOnlyOutput(t, stdOutBuffer, expected)
328+
assertNoErrors(t)
329+
}
330+
331+
func Test_Colorize_CanColorMixedTypes(t *testing.T) {
332+
script := `
333+
items = [42, "hello", true]
334+
for item in items:
335+
item.colorize(items).print()
336+
`
337+
setupAndRunCode(t, script, "--color=always")
338+
expected := "\x1b[38;2;230;38;25m42\x1b[0;22;0;0;0m\n\x1b[38;2;99;130;233mhello\x1b[0;22;0;0;0m\n\x1b[38;2;106;189;15mtrue\x1b[0;22;0;0;0m\n"
339+
assertOnlyOutput(t, stdOutBuffer, expected)
340+
assertNoErrors(t)
341+
}
342+
343+
func Test_Colorize_CanColorFloats(t *testing.T) {
344+
script := `
345+
floats = [1.5, 2.7, 3.14]
346+
for f in floats:
347+
f.colorize(floats).print()
348+
`
349+
setupAndRunCode(t, script, "--color=always")
350+
expected := "\x1b[38;2;230;38;25m1.5\x1b[0;22;0;0;0m\n\x1b[38;2;99;130;233m2.7\x1b[0;22;0;0;0m\n\x1b[38;2;106;189;15m3.14\x1b[0;22;0;0;0m\n"
351+
assertOnlyOutput(t, stdOutBuffer, expected)
352+
assertNoErrors(t)
353+
}
354+
355+
func Test_Colorize_ConsistentColors(t *testing.T) {
356+
script := `
357+
values = [1, 2, 3]
358+
for v in values:
359+
v.colorize(values).print()
360+
for v in values:
361+
v.colorize(values).print()
362+
`
363+
setupAndRunCode(t, script, "--color=always")
364+
// Colors should repeat - first three lines same as last three lines
365+
expected := "\x1b[38;2;230;38;25m1\x1b[0;22;0;0;0m\n\x1b[38;2;99;130;233m2\x1b[0;22;0;0;0m\n\x1b[38;2;106;189;15m3\x1b[0;22;0;0;0m\n"
366+
expected += "\x1b[38;2;230;38;25m1\x1b[0;22;0;0;0m\n\x1b[38;2;99;130;233m2\x1b[0;22;0;0;0m\n\x1b[38;2;106;189;15m3\x1b[0;22;0;0;0m\n"
367+
assertOnlyOutput(t, stdOutBuffer, expected)
368+
assertNoErrors(t)
369+
}

docs-web/docs/reference/functions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -766,15 +766,15 @@ Assigns consistent colors to values from a set of possible values. The same valu
766766
same set.
767767

768768
```rad
769-
colorize(_val: str, _enum: str[], *, skip_if_single: bool = false) -> str
769+
colorize(_val: any, _enum: any[], *, skip_if_single: bool = false) -> str
770770
```
771771

772772
**Parameters:**
773773

774774
| Parameter | Type | Description |
775775
|------------------|----------------|------------------------------------------------|
776-
| `_val` | `str` | Value to colorize |
777-
| `_enum` | `str[]` | Set of possible values for consistent coloring |
776+
| `_val` | `any` | Value to colorize |
777+
| `_enum` | `any[]` | Set of possible values for consistent coloring |
778778
| `skip_if_single` | `bool = false` | Don't colorize if only one value in set |
779779

780780
Useful for automatically coloring table data or distinguishing values in lists.

rts/signatures.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func init() {
112112
newFnSignature(`get_rad_home() -> str`),
113113
newFnSignature(`load(_map: map, _key: any, _loader: fn() -> any, *, reload: bool = false, override: any?) -> error|any`),
114114
newFnSignature(`color_rgb(_val: any, red: int, green: int, blue: int) -> error|str`),
115-
newFnSignature(`colorize(_val: str, _enum: str[], *, skip_if_single: bool = false) -> str`),
115+
newFnSignature(`colorize(_val: any, _enum: any[], *, skip_if_single: bool = false) -> str`),
116116
newFnSignature(`http_get(url: str, *, body: any?, json: any?, headers: map?) -> { "success": bool, "status_code"?: int, "body"?: any, "error"?: str, "duration_seconds": float }`),
117117
newFnSignature(`http_post(url: str, *, body: any?, json: any?, headers: map?) -> { "success": bool, "status_code"?: int, "body"?: any, "error"?: str, "duration_seconds": float }`),
118118
newFnSignature(`http_put(url: str, *, body: any?, json: any?, headers: map?) -> { "success": bool, "status_code"?: int, "body"?: any, "error"?: str, "duration_seconds": float }`),

0 commit comments

Comments
 (0)