-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty.lua
More file actions
executable file
·50 lines (47 loc) · 1.01 KB
/
Copy pathpretty.lua
File metadata and controls
executable file
·50 lines (47 loc) · 1.01 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
#!/usr/bin/env lua
local inp = io.stdin:read("*a")
local function out(c)
io.stdout:write(c)
end
local indent = 0
local stack = {}
for c in inp:gmatch(".") do
if c == '"' then
out(c)
if stack[#stack] ~= '"' then
table.insert(stack, c)
else
table.remove(stack)
end
elseif c == "(" or c == "[" then
out(c)
if stack[#stack] ~= '"' then
out("\n")
indent = indent + 1
out((" "):rep(indent))
table.insert(stack, c)
end
elseif c == ")" or c == "]" then
if stack[#stack] ~= '"' then
out("\n")
indent = indent - 1
out((" "):rep(indent))
table.remove(stack)
end
out(c)
elseif c == "," then
if stack[#stack] == '"' then
out(",")
else
out(", ")
if stack[#stack] == "[" then
if indent > 0 then
out("\n")
out((" "):rep(indent))
end
end
end
else
out(c)
end
end