Skip to content

Instantly share code, notes, and snippets.

@heptal heptal/
Last active Apr 20, 2016

What would you like to do?
Lua pattern matcher UI in Hammerspoon webview

local name = "id""-","");

local html = [[
.   all characters
%a  letters
%b  balanced delimiters
%c  control characters
%d  digits
%l  lower case letters
%p  punctuation characters
%s  space characters
%u  upper case letters
%w  alphanumeric characters
%x  hexadecimal digits
%z  the character with representation 0
<input id="pattern" type="text" placeholder="pattern">
<button id="sendData" onclick="sendData()">Evaluate pattern</button><br>
<textarea id="text" rows="10" cols="70" placeholder="text to match"></textarea><br>
<div id="response"></div>

local js = [[
function sendData() {
    pattern: document.getElementById("pattern").value,
    text: document.getElementById("text").value

local css = [[
input { width: 250px; }
body {font-family: monospace;}
span {background:rgba(0,0,200,0.2) }
#response {font-size:11px}

local uc =
  local pattern, text = input.body.pattern, input.body.text;
  local result = text:gsub(pattern, function(s) return '<span>'..s..'</span>' end)
  result = result:gsub("\n", "<br>"):gsub(" ", "&nbsp;")
  webview:evaluateJavaScript("document.getElementById('response').innerHTML = "..string.format("%q",result))

local frame = hs.geometry.rect(hs.screen.mainScreen():frame().topleft, "600x700")

webview =, {developerExtrasEnabled=true}, uc):windowStyle(1|2|4|8):deleteOnClose(true):allowTextEntry(true)
webview:html(string.format('%s<script type="text/javascript">%s</script><style>%s</style>', html, js, css)):show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.