-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.v
More file actions
86 lines (76 loc) · 2.22 KB
/
utils.v
File metadata and controls
86 lines (76 loc) · 2.22 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
// Utility functions for tree logger
module logger
import x.json2
// deep_merge recursively merges source map into target map
// If both source and target have a key with map values, they are merged recursively
// Otherwise, source value overwrites target value
fn deep_merge(source map[string]json2.Any, target map[string]json2.Any) map[string]json2.Any {
mut result := target.clone()
for key, value in source {
if key !in result {
result[key] = value
} else if existing := result[key] {
if is_object(existing) && is_object(value) {
// Recursively merge nested objects
nested_target := existing as map[string]json2.Any
nested_source := value as map[string]json2.Any
result[key] = json2.Any(deep_merge(nested_source, nested_target))
} else {
result[key] = value
}
}
}
return result
}
// is_object checks if the json2.Any value is a map (object)
fn is_object(v json2.Any) bool {
return v is map[string]json2.Any
}
// is_array checks if the json2.Any value is an array
fn is_array(v json2.Any) bool {
return v is []json2.Any
}
// format_duration converts milliseconds to human readable string
// Returns "Xms" for durations under 1 second, "X.XXs" for longer durations
fn format_duration(ms int) string {
if ms < 1000 {
return '${ms}ms'
}
// Truncate to 2 decimal places to avoid rounding issues (e.g., 999.999 -> 999.99 not 1000.00)
seconds := f64(ms) / 1000.0
truncated := f64(int(seconds * 100)) / 100.0
return '${truncated:.2f}s'
}
// format_bytes converts bytes to human readable string
fn format_bytes(bytes i64) string {
if bytes < 1024 {
return '${bytes}B'
}
if bytes < 1024 * 1024 {
kb := f64(bytes) / 1024.0
return '${kb:.1f}KB'
}
if bytes < 1024 * 1024 * 1024 {
mb := f64(bytes) / (1024.0 * 1024.0)
return '${mb:.1f}MB'
}
gb := f64(bytes) / (1024.0 * 1024.0 * 1024.0)
return '${gb:.2f}GB'
}
// truncate_string truncates a string to max_len and adds ellipsis if truncated
fn truncate_string(s string, max_len int) string {
if s.len <= max_len {
return s
}
if max_len <= 3 {
return s[..max_len]
}
return s[..(max_len - 3)] + '...'
}
// truncate_uuid shortens a UUID for display (first 8 chars)
fn truncate_uuid(s string) string {
if s.len > 8 {
return s[..8]
}
return s
}