Skip to content

Instantly share code, notes, and snippets.

@asmagill
asmagill / moonscript_traceback.lua
Last active August 29, 2015 14:25
Hammerspoon debug.traceback modification for moonscript; minimally tested, so leave comments, if it does/doesn't work for you!
local module = {
--[=[
_NAME = 'moonscript.traceback.lua',
_VERSION = 'the 1st digit of Pi/0',
_URL = 'https://github.com/asmagill/hammerspoon-config',
_LICENSE = [[ See README.md ]]
_DESCRIPTION = [[
debug.traceback supplement for moonscript files
--
-- Code snippit to check/add LSUIElement to Hammerspoon's Info.plist file
-- if running 10.8, since this entry is required for HS to be able to toggle
-- the dock icon. Surprisingly it isn't necessary in later OS X versions...
--
-- Save this file as LSUIElementCheck.lua in ~/.hammerspoon/ and add:
--
-- dofile("LSUIElementCheck.lua")
--
-- to the top of your init.lua also located in ~/.hammerspoon/
@asmagill
asmagill / graphpaper.lua
Last active October 22, 2015 20:04
Hammerspoon: generate a table of images of "graph lines" which will fill the specified screen via imageFromASCII
-- save file in ~/.hammerspoon and use as follows:
--
-- graph = require("graphpaper")
-- images = graph.fillScreen(x,y,screen)
-- all three parameters are optional. x and y specify the graph size in screen points.
-- default 10 for x, default whatever x is for y, default hs.screen.mainScreen() for screen
--
-- so, on a MacBook Air with a 1920x900 non-retina screen: `images = graph.fillScreen(20)`
-- images will be an array of 6 images. The array has a metatable set replicating the hs.drawing methods
-- and applies them to all elements of the array, so you can show the graph with:
#! /bin/sh
# This is the sample file used for testing during the development of hs.styledtext
# for Hammerspoon. See https://github.com/Hammerspoon/hammerspoon/pull/620
# It is based on code I found when I first discovered GeekTool, but I forget
# where it originated or how much I edited it for my purposes, so if you find
# this and feel additional attribution is warranted, please let me know.
##################
@asmagill
asmagill / constants.lua
Last active December 19, 2015 08:00
protect tables of constants in Hammerspoon
--[[
A way to protect tables of constants in Hammerspoon modules from inadvertant changes
Updated 2015-12-19: differentiate __tostring output for keyed table vs array
recursively descend, making each sub-table a constant table as well
weak key for storing original table so can be used with generated data (in testing)
notifies if wrong type passed in and returns unchanged value instead
]]--
@asmagill
asmagill / altHold.lua
Last active November 27, 2016 18:26
Capture holding alt key down for set time period in Hammerspoon
local alert = require("hs.alert")
local timer = require("hs.timer")
local eventtap = require("hs.eventtap")
local events = eventtap.event.types
local module = {}
-- You either override these here or after including this file from another, e.g.
--
@asmagill
asmagill / autohide.lua
Created August 1, 2015 19:42
Auto hide Hammerspoon console when it loses focus
--
-- Basic console auto-hide when Hammerspoon loses focus
--
-- ConsoleWatcher:start()
-- Turns on the watcher, and the Hammerspoon console, if open, will close when you enter another application
-- ConsoleWatcher:stop()
-- Turns off the watcher; Hammerspoon console open/close state is left untouched.
ConsoleWatcher = hs.application.watcher.new(function(name,event,hsapp)
if name then
@asmagill
asmagill / loopstop.lua
Last active March 12, 2019 12:34
prevent infinite loop in lua code from locking Hammerspoon up...
--
-- uses debug.sethook and a timer to break out of infinite loops in lua code within Hammerspoon
--
-- Haven't had any problems with it or false positives, but YMMV -- standard disclaimers, etc.
--
-- Updates 2015-12-21:
-- should play nicely with other hooks by storing info about it and chaining
-- you can force an "immediate" break by holding down CMD-CTRL-SHIFT-ALT-CAPSLOCK-FN all at once
-- you'll need to remove "and mods.fn" where noted below if your keyboard does not have this
-- modifier (non-laptops, I suspect)
@asmagill
asmagill / gist:4b792359c7a01da46b2d
Last active November 19, 2021 10:14
Bash resize terminal function

I forget where I found this, so if anyone wants to claim attribution, let me know and I'll add a line here. If put into /etc/profile.d/serial-console.sh, the following will auto size a serial terminal windows size and create a function for manually adjusting later. Since I access RPIs and BBBs via the serial console, their tty names are already listed in the case statement; add others if your system is different. rsz helps when you later reconnect and minicom or screen is in a different sized window...

    rsz() {
        if [[ -t 0 && $# -eq 0 ]];then
                local IFS='[;' escape geometry x y
                echo -ne '\e7\e[r\e[999;999H\e[6n\e8'
                read -sd R escape geometry
                x=${geometry##*;} y=${geometry%%;*}
                if [[ ${COLUMNS} -eq ${x} && ${LINES} -eq ${y} ]];then

echo "${TERM} ${x}x${y}"

@asmagill
asmagill / javascripttest.lua
Last active March 13, 2022 23:59
Test for hs.javascript in Hammerspoon
-- Example to test hs.javascript. This is a modified version of the code found at
-- https://trans4mind.com/personal_development/JavaScript/longnumPiMachin.htm
--
-- Note that the last 5 digits are probably wrong (this returns 5 more digits than
-- requested), but the specified N digits are provably correct (see web page for
-- details)
--
-- This results in the following on my machine:
--
-- true PI (10005)=3.