From 8b6ae1804e7055d690420701934b72d1e4a905ae Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Mon, 27 Jun 2022 10:29:42 +0200 Subject: [PATCH] Add linify script --- bin/linify | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100755 bin/linify diff --git a/bin/linify b/bin/linify new file mode 100755 index 0000000..a2ec801 --- /dev/null +++ b/bin/linify @@ -0,0 +1,73 @@ +#!/usr/bin/env lua + +-- Ajdacent characters numbering: +-- 1 +-- 2 + 3 +-- 4 +-- '+' = it's another plus +-- '.' = it's something else + +local adjacency_char_map = { + ["...."] = "┼"; + ["...+"] = "│"; + ["..+."] = "─"; + ["..++"] = "┌"; + [".+.."] = "─"; + [".+.+"] = "┐"; + [".++."] = "─"; + [".+++"] = "┬"; + ["+..."] = "│"; + ["+..+"] = "│"; + ["+.+."] = "└"; + ["+.++"] = "├"; + ["++.."] = "┘"; + ["++.+"] = "┤"; + ["+++."] = "┴"; + ["++++"] = "┼"; +} + +local input do + input = setmetatable({}, {__index = function(self, key) + if type(key) == "number" then + return {} + end + end}) + + for line in io.stdin:lines() do + local buf = {} + for char in line:gmatch(".") do + table.insert(buf, char) + end + table.insert(input, buf) + end +end + +local function rawipairs(tab) + return function(tab, i) + i = i + 1 + if rawget(tab, i) then + return i, rawget(tab, i) + end + end, tab, 0 +end + +local output = {} +for lnum, line in rawipairs(input) do + output[lnum] = {} + for cnum, char in rawipairs(line) do + if char == "+" then + local adjacency = + (input[lnum-1][cnum] == "+" and "+" or ".") + ..(line[cnum-1] == "+" and "+" or ".") + ..(line[cnum+1] == "+" and "+" or ".") + ..(input[lnum+1][cnum] == "+" and "+" or ".") + output[lnum][cnum] = adjacency_char_map[adjacency] + else + output[lnum][cnum] = char + end + end +end + +for _, line in ipairs(output) do + print(table.concat(line)) +end