/
tree.ex
190 lines (155 loc) · 4.9 KB
/
tree.ex
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
defmodule Kino.Tree do
@moduledoc """
A kino for interactively viewing nested data as a tree view.
The data can be any term.
## Examples
data = %{
id: 1,
email: "user@example.com",
inserted_at: ~U[2022-01-01T10:00:00Z],
addresses: [
%{
country: "pl",
city: "Kraków",
street: "Karmelicka",
zip: "00123"
}
]
}
Kino.Tree.new(data)
The tree view is particularly useful when inspecting larger data
structures:
data = Process.info(self())
Kino.Tree.new(data)
"""
use Kino.JS, assets_path: "lib/assets/tree"
@doc """
Creates a new kino displaying the given data structure.
"""
@spec new(term()) :: Kino.Layout.t()
def new(data) do
tree = to_node(data, [])
kino = Kino.JS.new(__MODULE__, tree)
Kino.Layout.grid([kino], boxed: true)
end
defp to_node(string, suffix) when is_binary(string) do
%{content: [green(inspect(string)) | suffix], children: nil}
end
defp to_node(atom, suffix) when is_atom(atom) do
span =
if atom in [nil, true, false] do
magenta(inspect(atom))
else
blue(inspect(atom))
end
%{content: [span | suffix], children: nil}
end
defp to_node(number, suffix) when is_number(number) do
%{content: [blue(inspect(number)) | suffix], children: nil}
end
defp to_node({}, suffix) do
%{content: [black("{}") | suffix], children: nil}
end
defp to_node(tuple, suffix) when is_tuple(tuple) do
size = tuple_size(tuple)
children = tuple |> Tuple.to_list() |> to_children(size)
%{
content: [black("{...}") | suffix],
children: children,
expanded: %{prefix: [black("{")], suffix: [black("}") | suffix]}
}
end
defp to_node([], suffix) do
%{content: [black("[]") | suffix], children: nil}
end
defp to_node(list, suffix) when is_list(list) do
size = length(list)
children =
if Keyword.keyword?(list) do
to_key_value_children(list, size)
else
to_children(list, size)
end
%{
content: [black("[...]") | suffix],
children: children,
expanded: %{prefix: [black("[")], suffix: [black("]") | suffix]}
}
end
defp to_node(%Regex{} = regex, suffix) do
%{content: [red(inspect(regex)) | suffix], children: nil}
end
defp to_node(%module{} = struct, suffix) when is_struct(struct) do
if Inspect.impl_for(struct) != Inspect.Any do
%{content: [black(inspect(struct)) | suffix], children: nil}
else
map = Map.from_struct(struct)
size = map_size(map)
children = to_key_value_children(map, size)
%{
content: [black("%"), blue(inspect(module)), black("{...}") | suffix],
children: children,
expanded: %{
prefix: [black("%"), blue(inspect(module)), black("{")],
suffix: [black("}") | suffix]
}
}
end
end
defp to_node(%{} = map, suffix) when map_size(map) == 0 do
%{content: [black("%{}") | suffix], children: nil}
end
defp to_node(map, suffix) when is_map(map) do
size = map_size(map)
children = to_key_value_children(map, size)
%{
content: [black("%{...}") | suffix],
children: children,
expanded: %{prefix: [black("%{")], suffix: [black("}") | suffix]}
}
end
defp to_node(other, suffix) do
%{content: [black(inspect(other)) | suffix], children: nil}
end
defp to_key_value_node({key, value}, suffix) do
{key_span, sep_span} =
case to_node(key, []) do
%{content: [%{text: ":" <> name} = span]} when is_atom(key) ->
{%{span | text: name <> ":"}, black(" ")}
%{content: [span]} ->
{%{span | text: inspect(key, width: :infinity)}, black(" => ")}
end
case to_node(value, suffix) do
%{content: content, expanded: %{prefix: prefix} = expanded} = node ->
%{
node
| content: [key_span, sep_span | content],
expanded: %{expanded | prefix: [key_span, sep_span | prefix]}
}
%{content: content} = node ->
%{node | content: [key_span, sep_span | content]}
end
end
defp to_children(items, container_size) do
Enum.with_index(items, fn item, index ->
to_node(item, suffix(index, container_size))
end)
end
defp to_key_value_children(items, container_size) do
Enum.with_index(items, fn item, index ->
to_key_value_node(item, suffix(index, container_size))
end)
end
defp suffix(index, container_size) do
if index != container_size - 1 do
[black(",")]
else
[]
end
end
defp black(text), do: %{text: text, color: nil}
defp red(text), do: %{text: text, color: "var(--ansi-color-red)"}
defp green(text), do: %{text: text, color: "var(--ansi-color-green)"}
defp blue(text), do: %{text: text, color: "var(--ansi-color-blue)"}
defp magenta(text), do: %{text: text, color: "var(--ansi-color-magenta)"}
end