-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpand.lua
More file actions
23 lines (23 loc) · 853 Bytes
/
expand.lua
File metadata and controls
23 lines (23 loc) · 853 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
local util = require "system.util"
local args = assert(util.argparse({t = true}, ...))
args.t = args.t or "8"
if #args == 0 then args[1] = "-" end
local stops = {}
if args.t:match("^%d+$") then
local t = tonumber(args.t)
setmetatable(stops, {__index = function(_, n) return (math.ceil((n + 1) / t) * t) - n end})
elseif args.t:match("^[%d, ]+$") then
local a = 1
for tt in args.t:gmatch "%d+" do
local t = tonumber(tt)
assert(t > a, "expand: invalid tab stop format")
for i = a, t - 1 do stops[i] = t - i end
a = t
end
setmetatable(stops, {__index = function(_, n) return 1 end})
else error("expand: invalid tab stop format") end
for _, v in ipairs(args) do
for line in io.lines(v ~= "-" and v or nil) do
print(line:gsub("()\t", function(n) return (" "):rep(stops[n]) end))
end
end