Skip to content

Instantly share code, notes, and snippets.

Avatar
🐍
Python!

mattmc3 mattmc3

🐍
Python!
View GitHub Profile
@mattmc3
mattmc3 / palette.sh
Last active March 5, 2023 00:21
Keyboard.io palette JSON generator
View palette.sh
#!/bin/sh
# Author: mattmc3
# Copyright: 2023
# License: MIT
print_err() {
printf '%s\n' "$my_name: Error: $1" >&2
exit ${2:-1}
}
@mattmc3
mattmc3 / pipetest.md
Last active February 25, 2023 03:09
Python read stdin arguments from pipe
View pipetest.md

Pipetest

clitest and stdin don't always work well together. StackOverflow wasn't much help (shocked pikachu)! Buried deep in the interwebs is the bash advice to check for a TTY and whether stdin was piped with this: if [ ! -t 0 ] && [ -p /dev/stdin ];. Using this, I wanted to see how to do something similar in Python to make clitest work. Even deeper on the internet was this gem, which led me to the answer - use stat in Python to test whether stdin is a FIFO (pipe).

Thus, the answer to this hours long search is simply this function:

@mattmc3
mattmc3 / foo.zsh
Created February 3, 2023 01:38
Zsh collect args
View foo.zsh
# Collect args
# -- traditional, piped|, <redirected
function collect_args {
local data args=()
if [[ ! -t 0 ]]; then
while IFS= read -r data || [[ -n "$data" ]]; do
args+=("$data")
done
fi
@mattmc3
mattmc3 / frame-recenter.el
Created October 26, 2022 20:08
Emacs frame (window) re-center
View frame-recenter.el
;; https://christiantietze.de/posts/2022/04/emacs-center-window-current-monitor-simplified/
(defun my/frame-recenter (&optional frame)
"Center FRAME on the screen.
FRAME can be a frame name, a terminal name, or a frame.
If FRAME is omitted or nil, use currently selected frame."
(interactive)
(unless (eq 'maximised (frame-parameter nil 'fullscreen))
(modify-frame-parameters
frame '((user-position . t) (top . 0.5) (left . 0.5)))))
@mattmc3
mattmc3 / auto_dircmd.zsh
Created September 15, 2022 12:22
Zsh auto_cd-like feature implementation
View auto_dircmd.zsh
setopt no_auto_cd
function auto_dircmd {
# Run a command when PS1 contains a valid directory, similar to auto_cd
if [[ -d "$PWD/$BUFFER" ]] &&
(( ! $+functions[$BUFFER] )) &&
(( ! $+commands[$BUFFER] ))
then
BUFFER="ls \"./$BUFFER\""
fi
@mattmc3
mattmc3 / piped_input_example.zsh
Created August 20, 2022 12:54
Zsh - append piped/redirected input to arg array
View piped_input_example.zsh
# Example to show how to append piped/redirected input to arg array
#
# More info:
# `man test` (lookup -t flag)
# `man zshbuiltins` (lookup set builtin)
#
# piped_input_example
# piped_input_example --flag arg
# piped_input_example --opt1 -x -y -z param1 param2 <<< "zzz"
# echo "abc" | piped_input_example --opt1 -x -y -z param1 param2
@mattmc3
mattmc3 / plugin-load.zsh
Last active July 30, 2022 14:33
Zsh Unplugged Enhanced
View plugin-load.zsh
#!/usr/bin/env zsh
##? plugin-load - load plugins without a fancy plugin manager
##?
##? usage: plugin-load [-h|--help]
##? plugin-load [-n|--no-source] [-d|--defer] [-f|--fpath] [-p|--path]
##? [-u|--use-dir <plugin-subdir>] [<repo...>]
function plugin-load {
local use_dir flag_no_source flag_fpath flag_path flag_defer repos=()
@mattmc3
mattmc3 / frequency.zsh
Last active July 1, 2022 19:19
Colemak bigram assessment
View frequency.zsh
function freq {
for c in $@; do
for c2 in $@; do
grep "$c$c2" /usr/share/dict/words | wc -l | tr -d '\n' && echo " $c$c2"
done
done
}
# least frequent home row bigrams
freq a r s t d h n e i o | sort -d | uniq | head -n 10
@mattmc3
mattmc3 / cd-ls.zsh
Created June 30, 2022 23:21
Zsh cd-ls
View cd-ls.zsh
# 'ls' after every 'cd'
if ! (( $chpwd_functions[(I)chpwd_cdls] )); then
chpwd_functions+=(chpwd_cdls)
fi
function chpwd_cdls() {
if [[ -o interactive ]]; then
emulate -L zsh
ls
fi
}
@mattmc3
mattmc3 / gpginit.zsh
Created March 22, 2022 15:39
GPG home
View gpginit.zsh
#!/usr/bin/env zsh
GNUPGHOME=${XDG_DATA_HOME:-$HOME/.local/share}/gnupg
mkdir -p $GNUPGHOME
chown -R $(whoami) $GNUPGHOME
chmod 600 $GNUPGHOME/*
chmod 700 $GNUPGHOME