Skip to content

Instantly share code, notes, and snippets.

View linktohack's full-sized avatar

Quang-Linh LE linktohack

View GitHub Profile
@linktohack
linktohack / scratch.fnl
Created October 23, 2023 14:14
Naive implementation of home row mods in pure Hammerspoon
(local fennel (require :fennel))
(local pp fennel.view)
;; CAGS
(local mod-map {:a :ctrl :s :alt :d :cmd :f :shift
:j :rightshift :k :rightcmd :l :rightalt ";" :rightctrl})
(local flags-map {:deviceLeftAlternate :alt
:deviceLeftCommand :cmd
:deviceLeftControl :ctrl
@linktohack
linktohack / date-fns-tz-test.ts
Created June 22, 2023 15:32
Understand date-fns-tz
const utcDate = new Date("2022-06-15T05:00:00Z");
console.log("utcDate", utcDate, utcDate.toString());
const utcDate2 = set(utcDate, { hours: 1 }); // Always set in curren time
console.log("utcDate2", utcDate2, utcDate2.toString());
const zonedDate = utcToZonedTime(utcDate, "Asia/Jakarta"); // 5 + 7 = 12 in current timezone (pretend to be in local)
console.log("zoneDate", zonedDate, zonedDate.toString());
const zonedDate2 = set(zonedDate, { hours: 1 });
@linktohack
linktohack / *scratch*.el
Last active May 4, 2023 14:32
Generate ovpn from a list of IP, result from ipinfo.io
# remote-random
cat *.json | jq -s -r 'sort_by(.country) | .[] | "remote \(.ip) 1193 # \(.country)-\(.city)"'
# remote
cat *.json | jq -s -r "sort_by(.country) | .[] | \"cat ~/.config/default.ovpn | sed 's/remote XXX/remote \(.ip) 1193 # \(.country)-\(.city)/' > '\(.country)-\(.city)-\(.ip).ovpn'\""
@linktohack
linktohack / *scratch*.el
Last active March 23, 2023 10:04
Remove extranouse org ID
;; evaluate this to remove all IDs that are neither: a target, has attachment(s) nor timestamp (synced with caldav)
;; assume everything is in `~/org', including archived files
;; require `rg', `jq' and `awk'
(let* ((default-directory (expand-file-name "~/org"))
(ids (-> (shell-command-to-string
"rg -a -g '*.org*' --json '^:ID:\s*([A-Za-z00-9\-])+\s*$' | jq -r '.data.submatches | select(.) | .[].match.text' | awk '{ print $2 }' | sort")
(string-trim)
(split-string "\n")))
(targets (-> (shell-command-to-string
(format "rg -a -g '*.org*' --json '\\[id:(%s)]' | jq -r '.data.submatches | select(.) | .[].match.text[4:-1]' | sort" (string-join ids "|")))
@linktohack
linktohack / *new*.txt
Last active February 17, 2023 00:13
Firestore maintainancew
< Citations.jsonl | curl -vv -H 'Content-Type: application/octet-stream' http://127.0.0.1:5001/hatchi-mobile/us-central1/maintainance\?secret\=$SECRET\&action\=importCol\&col\=Citations\&withSubCollections\=1 --data-binary @- ; \
< Concours.jsonl | curl -vv -H 'Content-Type: application/octet-stream' http://127.0.0.1:5001/hatchi-mobile/us-central1/maintainance\?secret\=$SECRET\&action\=importCol\&col\=Concours\&withSubCollections\=1 --data-binary @- ; \
< Conseils.jsonl | curl -vv -H 'Content-Type: application/octet-stream' http://127.0.0.1:5001/hatchi-mobile/us-central1/maintainance\?secret\=$SECRET\&action\=importCol\&col\=Conseils\&withSubCollections\=1 --data-binary @- ; \
< Devises.jsonl | curl -vv -H 'Content-Type: application/octet-stream' http://127.0.0.1:5001/hatchi-mobile/us-central1/maintainance\?secret\=$SECRET\&action\=importCol\&col\=Devises\&withSubCollections\=1 --data-binary @- ; \
< Dictionnary_mot_interdit.jsonl | curl -vv -H 'Content-Type: application/octet-stream' http://127.0.0.1:5001/ha
@linktohack
linktohack / *scratch*.el
Created February 10, 2023 08:36
Elisp merge lists, keep order
(defun merge-lists (&rest lists)
"Merge list, keep elements' order the same as where they are in the original lists.
E.g `(merge-list '(1 2) '(3 4 5))' should returns `(1 3 2 4 5)'."
(let ((copy (mapcar 'copy-sequence lists))
(result))
(while copy
(dolist (lst copy)
(when-let ((elt (pop lst))) ; pop doesn't change `lst'
(push elt result)))
@linktohack
linktohack / loki-pattern.txt
Created December 16, 2022 17:42
Pattern to match loki's log (Nginx and LB app)
api.driver.cloud.private
api.driver.cloud.transalliance.eu
|~ "(?i)error"
|= `ms {`
| pattern `<ts> stdout F <method> <url> <code> <ms>ms {`
@linktohack
linktohack / touchid.sh
Created September 14, 2022 15:05
Enable TouchID for sudo
sudo sed -i '' '2i\
auth sufficient pam_tid.so
' /etc/pam.d/sudo
@linktohack
linktohack / *scratch*.sh
Created May 23, 2022 08:35
Barcode generator with ImageMagick
for i in `seq 150 650`; do
convert -size 1016x200 -background white -fill black -font Libre-Barcode-39-Regular -pointsize 160 -gravity center caption:"*66250${i}*" -gravity south -extent 1016x260 -resize 1016x648\! "66250${i}.png"
done
convert *.png 0.pdf
@linktohack
linktohack / coroutinize.fnl
Created March 28, 2022 23:05
Lua coroutinize
;; -- Coroutinize
(fn coroutinize [f ...]
"Coroutinize a function `f' allow to to call functions with a callback within its body naturally
via coroutine, eliminate the callback hell.
These function often need to be evaluated in main thread or where `coroutine.resume' is not
availabe (Hammerspoon). The functions should to be wraped within `(fn [resolve] (f ... resolve))'
and yielded.They will be evaluated (in main thread) and come back via `coroutine.yield'