Skip to content

Instantly share code, notes, and snippets.

View mattmc3's full-sized avatar
🐍
Python!

mattmc3 mattmc3

🐍
Python!
View GitHub Profile
@mattmc3
mattmc3 / palette.sh
Last active March 5, 2023 00:21
Keyboard.io palette JSON generator
#!/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

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 / tasks.json
Created April 6, 2017 00:44
VSCode tasks for running a Makefile
// Makefile
// ${workspaceRoot} the path of the folder opened in VS Code
// ${file} the current opened file
// ${fileBasename} the current opened file's basename
// ${fileDirname} the current opened file's dirname
// ${fileExtname} the current opened file's extension
// ${cwd} the task runner's current working directory on startup
{
"version": "0.1.0",
"command": "bash",
@mattmc3
mattmc3 / logger.py
Last active December 2, 2022 01:12
Python - basic console logger
import logging
# setup simple console logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
# handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s',
'%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
@mattmc3
mattmc3 / frame-recenter.el
Created October 26, 2022 20:08
Emacs frame (window) re-center
;; 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 / split_repo.zsh
Last active July 27, 2022 03:49
Bash: split string on delimiter and echo substring before/after
giturls=(
mattmc3/antidote
git@github.com:mattmc3/antidote.git
https://github.com/mattmc3/antidote
)
for url in $giturls; do
url=${url%.git}
url=${url:gs/\:/\/}
user=${${url%/*}##*/}
repo=${url##*/}
@mattmc3
mattmc3 / frequency.zsh
Last active July 1, 2022 19:19
Colemak bigram assessment
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 / brew_export.sh
Last active May 14, 2022 17:12
Brew - better Brewfile creation (without the file)
brew_export() {
# brew bundle is a pain... it dumps to a forced Brewfile, and is not
# consistently sorted, making version controling your Brewfile in a dotfile
# repo tricky. #FixedIt
# makes a Brewfile
brew bundle dump --force
# add custom sort column
awkcmd='
@mattmc3
mattmc3 / source_me.sh
Last active May 13, 2022 23:16
Bash: get current script directory (zsh too)
# source this file
# https://stackoverflow.com/questions/9901210/bash-source0-equivalent-in-zsh
__main() {
local this_script="${BASH_SOURCE[0]:-${(%):-%x}}"
local this_dir="$(cd $(dirname "$this_script") && pwd)"
echo $this_script
echo $this_dir
}
__main
@mattmc3
mattmc3 / gpginit.zsh
Created March 22, 2022 15:39
GPG home
#!/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