1 | -- utils.lua |
2 | -- basic utilities for git status site implementation |
3 |
|
4 | -- Copyright (c) 2020 Joshua 'joshuas3' Stockin |
5 | -- <https://github.com/JoshuaS3/joshstock.in> |
6 | -- <https://joshstock.in> |
7 |
|
8 | local _M = {} |
9 |
|
10 | string.split = function(str, delimiter) |
11 | local t = {} |
12 | if not str or str == "" then |
13 | return t |
14 | end |
15 | if delimiter == "" then -- string to table |
16 | string.gsub(str, ".", function(c) table.insert(t, c) end) |
17 | return t |
18 | end |
19 | delimiter = delimiter or "%s" |
20 | local str_len = string.len(str) |
21 | local ptr = 1 |
22 | while true do |
23 | local sub = string.sub(str, ptr, str_len) |
24 | local pre, after = string.find(sub, delimiter) |
25 | if pre then -- delimiter found |
26 | table.insert(t, string.sub(str, ptr, ptr + (pre - 2))) |
27 | ptr = ptr + after |
28 | else -- delimiter not found |
29 | table.insert(t, string.sub(str, ptr, str_len)) |
30 | break |
31 | end |
32 | end |
33 | return t |
34 | end |
35 |
|
36 |
|
37 | string.trim = function(str) |
38 | return str:match('^()%s*$') and '' or str:match('^%s*(.*%S)') |
39 | end |
40 |
|
41 |
|
42 | _M.process = function(command) |
43 | local output |
44 | local status, err = pcall(function() |
45 | local process = io.popen(command, "r") |
46 | assert(process, "Error opening process") |
47 | output = process:read("*all") |
48 | process:close() |
49 | end) |
50 | if status then |
51 | return output |
52 | else |
53 | return string.format("Error in call: %s", err or command) |
54 | end |
55 | end |
56 |
|
57 | _M.iso8601 = function(iso8601) |
58 | local y,mo,d,h,mi,s = iso8601:match("(%d%d%d%d)-(%d%d)-(%d%d)T(%d%d):(%d%d):(%d%d)") |
59 | local luatime = os.time{year=y,month=mo,day=d,hour=h,min=mi,sec=s} |
60 | return os.date("%d %b %Y %H:%M", luatime) |
61 | end |
62 |
|
63 | return _M |
64 |
|