From 8944c523367edb2286f10ce5d862650194666751 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Thu, 12 Sep 2019 16:11:52 +0200 Subject: [PATCH] Add highlight script --- bin/highlight | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 bin/highlight diff --git a/bin/highlight b/bin/highlight new file mode 100755 index 0000000..06653ae --- /dev/null +++ b/bin/highlight @@ -0,0 +1,28 @@ +#!/usr/bin/env luajit +-- vim: set noexpandtab tabstop=3 :miv -- + +local ansicolors = require 'ansicolors' +local lpeg = require 'lpeg' + +local colors = { 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan' } + +local function uncase(str) + local p = lpeg.P(0) + for char in str:gmatch('.') do + p = p * (lpeg.P(char:upper()) + lpeg.P(char:lower())) + end + return p +end + +local pattern = lpeg.P(false) +for i=1, select('#', ...) do + pattern = pattern + uncase(select(i, ...)) / function(capture) return ansicolors('%{'..colors[(i-1)%#colors+1]..' underline}'..capture) end +end + +pattern = lpeg.Cs((pattern + lpeg.P(1)) ^ 1) + +print(pattern:match('hello world testing stuff!')) + +for line in io.stdin:lines() do + print(pattern:match(line) or '') +end