diff options
Diffstat (limited to '.local/bin/development')
-rwxr-xr-x | .local/bin/development/dev | 179 | ||||
-rwxr-xr-x | .local/bin/development/getcomproot | 14 | ||||
-rwxr-xr-x | .local/bin/development/gitpush | 10 | ||||
-rwxr-xr-x | .local/bin/development/gitw | 18 | ||||
-rwxr-xr-x | .local/bin/development/ipy | 4 | ||||
-rwxr-xr-x | .local/bin/development/ipytest | 23 | ||||
-rwxr-xr-x | .local/bin/development/nx | 148 | ||||
-rwxr-xr-x | .local/bin/development/opout | 12 | ||||
-rwxr-xr-x | .local/bin/development/pipsearch | 4 | ||||
-rwxr-xr-x | .local/bin/development/py | 4 | ||||
-rwxr-xr-x | .local/bin/development/pyh | 12 | ||||
-rwxr-xr-x | .local/bin/development/texclear | 9 |
12 files changed, 437 insertions, 0 deletions
diff --git a/.local/bin/development/dev b/.local/bin/development/dev new file mode 100755 index 0000000..76f1772 --- /dev/null +++ b/.local/bin/development/dev @@ -0,0 +1,179 @@ +#!/bin/sh + +err() { [ "$DEV_SUPRESS_ERRORS" = 1 ] || + printf "dev: %s\n" "$@" >&2; exit 5; } + +[ "$#" -lt 2 ] && { err "dev - execute action based on file +USAGE: dev <ACTION> <FILE> [ARGS...]"; } + +action="$1" +file="$2" +basename="${file##*/}" +shift 2 + +[ -e "$file" ] || err "$file: No such file or directory" + +# auxilary functions +findweb() { find "$@" -or -name '*.html'; } + + + +### ################################################################# +### BEGIN TASKS +### ################################################################# + +sh_lint() { shellcheck -x "$file"; } +sh_run() { $(sed -n '1s|^#!/.*/\(.*\)|\1|p' "$file") "$file" "$@"; } +zsh_run() { zsh "$file" "$@"; } +awk_run() { awk -f "$file" "$@"; } +pl_run() { perl "$file" "$@"; } + +md_run() { glow --pager --style dark "$file"; } +md_test() { pandoc -t pdf "$file" | zathura -; } +tex_compile() { pdflatex -output-directory="$(dirname "$file")" "$file"; } +tex_clean() { find "$(dirname "${1}")" -regex \ + '.*\(_minted.*\|.*\.\(4tc\|xref\|tmp\|pyc\|pyg\|pyo\|fls\|vrb\|fdb_latexmk\|bak\|swp\|aux\|log\|synctex\(busy\)\|lof\|lot\|maf\|idx\|mtc\|mtc0\|nav\|out\|snm\|toc\|bcf\|run\.xml\|synctex\.gz\|blg\|bbl\)\)' -delete; } +html_format() { tidy -q "$file" 2>/dev/null; } +html_lint() { tidy -q "$file" >/dev/null; } +html_run() { w3m "$file"; } +js_test() { node "$file"; } +json_format() { jq . "$file"; } +php_run() { php "$file" "$@"; } + +c_compile() { make "${file%.c}"; } +c_run() { "$(realpath "${file%.c}")" "$@"; } +c_clean() { rm -v "${file%.c}"; } + +go_format() { gofmt "$file"; } +go_lint() { gofmt -d "$file"; } +go_compile() { go build "$file"; } +go_run() { "$(realpath "${file%.go}")" "$@"; } +go_test() { go run "$file" "$@"; } +go_serve() { findweb . -name '*.go' | entr -r sh -c "printf '\n\n$ go run *.go\n'; go run *.go"; } + +c_test() { + out="${file%.c}" + # ARGS="$(sed -nE -e 's|.*#include\s*<(.*)>|-l\1|gp' "$file" | + # paste -d' ' -s)" + # echo "$ clang -o $out $file $ARGS" + # clang -o "$out" "$file" $ARGS && "$out" "$@" + make "$out" && echo && "$(realpath "$out")" "$@" +} + +cpp_test() { + out="$(realpath "${file%.cpp}")" + if [ -f GNUmakefile ] || [ -f makefile ] || [ -f Makefile ]; then + make && "${out%/*}/main" "$@" + else + g++ -o "$out" "$file" && "$out" "$@" + fi +} + +ino_test() { + [ -t 1 ] || set -- --no-color + if [ -c /dev/ttyACM0 ]; then + arduino-cli compile --fqbn arduino:avr:uno "$file" "$@" + arduino-cli upload --fqbn arduino:avr:uno --port /dev/ttyACM0 "$file" "$@" + if [ -t 1 ]; then + picocom /dev/ttyACM0 + else + stty -F /dev/ttyACM0 raw 9600 + cat /dev/ttyACM0 + fi + else + arduino-cli compile --fqbn arduino:avr:uno --output-dir . "$file" "$@" + rm -- *.eep *.elf *.bin *.with_bootloader.hex + fi +} + +java_test() { + if [ -f build.xml ]; then + ant + build="$(find . -name 'classes' -or -name 'build' | tail -1)" + class="$({ cd "$build" && + find . -name "$(basename "$file" .java).class"; } | + tail -1 | cut -c 3- | sed 's|.class||g' | tr '/' '.')" + else + mkdir -pv build + build="$(dirname "$file")/build" + class="$(basename "$file" .java)" + javac -d "$build" "$file" + fi + java -cp "$build:lib/*" "$class" +} + +kt_test() { + out="${file%.*}.jar" + bash /opt/android-studio/plugins/Kotlin/kotlinc/bin/kotlinc "$file" -include-runtime -d "$out" + java -jar "$out" + rm "$out" +} + +py_serve() { findweb . -name '*.py' | entr -r python "$file"; } +# py_lint() { +# [ -t 1 ] && printf '\033[33m' +# pycodestyle "$file" >&2 +# [ -t 1 ] && printf '\033[0;39m' +# } + +py_run() { + python "$file" "$@" + # if [ -x "${XDG_DATA_HOME:-$HOME/.local/share}/virtualenvs/main/bin/python" ]; then + # "${XDG_DATA_HOME:-$HOME/.local/share}/virtualenvs/main/bin/python" "$file" "$@" + # else + # python "$file" "$@" + # fi +} + +### ################################################################# +### END TASKS +### ################################################################# + + + +filetype=$(file --brief --mime-type "$file") +case $filetype in + text/x-c) filetype_ext="c" ;; + text/x-script.python) filetype_ext="py" ;; + text/x-shellscript) filetype_ext="sh" ;; + text/html) filetype_ext="html" ;; + application/javascript) filetype_ext="js" ;; + text/x-php) filetype_ext="php" ;; + text/x-awk) filetype_ext="awk" ;; +esac + +if [ "${basename#*.}" != "${basename}" ]; then + file_ext="${basename##*.}" +else + [ -z "$filetype_ext" ] && err "no file type association for $filetype" + NO_BASENAME=1 +fi + +execute_task() { + [ "$NO_BASENAME" != 1 ] && { + file_task="${file_ext}_${action}" + [ "${tasks#*"[$file_task]"}" != "$tasks" ] && + { $file_task; local ret=$?; local tasked=1; } + } + [ "$tasked" != 1 ] && { + filetype_task="${filetype_ext}_${action}" + [ "${tasks#*"[$filetype_task]"}" != "$tasks" ] && + { $filetype_task; local ret=$?; local tasked=1; } + } + [ "$tasked" != 1 ] && return 1 + [ "$ret" != 0 ] && printf "\nshell returned %s\n" "$ret" >&2 + return 0 +} + +tasks="$(sed -n "/^### BEGIN TASKS$/,/^### END TASKS$/ s/\(^[a-zA-Z0-9_]\+\)().*/[\1]/p" "$0")" + +if [ "$action" = test ]; then + execute_task && exit + for action in lint compile run; do + execute_task && tested=1 + done + [ "$tested" != 1 ] && err "no tests for .${file_ext:-$filetype_ext} file" + exit 0 +fi + +execute_task || err "no $action action for .${file_ext:-$filetype_ext} file" diff --git a/.local/bin/development/getcomproot b/.local/bin/development/getcomproot new file mode 100755 index 0000000..16e946a --- /dev/null +++ b/.local/bin/development/getcomproot @@ -0,0 +1,14 @@ +#!/bin/bash + +# author: Luke Smith +# +# A helper script for LaTeX/groff files used by `compiler` and `opout`. +# The user can add the root file of a larger project as a comment as below: +# % root = mainfile.tex +# And the compiler script will run on that instead of the opened file. + +texroot="$(grep -i "^.\+\s*root\s*=\s*\S\+" "$1")" +texroot="${texroot##*=}" +texroot="${texroot//[\"\' ]}" + +[ -f "$texroot" ] && readlink -f "$texroot" || exit 1 diff --git a/.local/bin/development/gitpush b/.local/bin/development/gitpush new file mode 100755 index 0000000..97aa43b --- /dev/null +++ b/.local/bin/development/gitpush @@ -0,0 +1,10 @@ +#!/bin/sh + +case "$(git config --get remote.origin.url)" in + *csstudent41*) sshadd ~/.ssh/github.com_id_ed25519 ;; + *labstudent41*) sshadd ~/.ssh/labstudent41_id_ed25519 ;; +esac + +git add --all +git commit # --allow-empty-message +git push -u origin diff --git a/.local/bin/development/gitw b/.local/bin/development/gitw new file mode 100755 index 0000000..881c81c --- /dev/null +++ b/.local/bin/development/gitw @@ -0,0 +1,18 @@ +#!/bin/sh + +# git-web: open the url of current git repositary in a web browser. +# USAGE: gitw [remote] +# 'remote' is set to origin if not provided. + +err() { printf "gitw: %s\n" "$@" >&2; exit 1; } + +git rev-parse --is-inside-work-tree >/dev/null || exit + +remote="${1:-origin}" +url="$(git remote get-url "$remote" | + sed "s|^git@\(\S\+\):|https://\1/|")" || exit + +[ -z "$url" ] || [ "$remote" = "$url" ] && + err "remote has no url: $remote" + +xdg-open "$url" diff --git a/.local/bin/development/ipy b/.local/bin/development/ipy new file mode 100755 index 0000000..1f39550 --- /dev/null +++ b/.local/bin/development/ipy @@ -0,0 +1,4 @@ +#!/bin/sh + +unset PYTHONSTARTUP +"${XDG_DATA_HOME:-$HOME/.local/share}/virtualenvs/main/bin/ipython" "$@" diff --git a/.local/bin/development/ipytest b/.local/bin/development/ipytest new file mode 100755 index 0000000..17a5a55 --- /dev/null +++ b/.local/bin/development/ipytest @@ -0,0 +1,23 @@ +#!/bin/sh +# create a 'dev' firefox profile and use it in selenium + +[ -z "$FF_DEV_PROFILE" ] && [ -d ~/.librewolf ] && + FF_DEV_PROFILE="$(find ~/.librewolf/ -maxdepth 1 -type d -name '????????.dev')" + +[ -z "$FF_DEV_PROFILE" ] && [ -d ~/.librewolf ] && + FF_DEV_PROFILE="$(find ~/.mozilla/ -maxdepth 1 -type d -name '????????.dev')" + +ipy -i -c " +from selenium import webdriver +from selenium.webdriver.common.by import By + +options = webdriver.FirefoxOptions() +options.binary_location = '/usr/bin/librewolf' +options.profile = webdriver.FirefoxProfile(\"$FF_DEV_PROFILE\") + +d = webdriver.Firefox(options=options) +d.get(\"${1:-http://127.0.0.1}\") + +import atexit +atexit.register(d.quit) +" diff --git a/.local/bin/development/nx b/.local/bin/development/nx new file mode 100755 index 0000000..862be99 --- /dev/null +++ b/.local/bin/development/nx @@ -0,0 +1,148 @@ +#!/bin/sh + +help() { + echo "nx - create new script" + echo "USAGE: nx <script_name>" +} + +err() { printf '%s: %s\n' "$0" "$@" >&2; exit 1; } +warn() { + printf '%s: %s [Y/n] ' "$0" "$@" >&2; read -r arg + case "$arg" in ''|y|Y) ;; *) exit ;; esac +} + +while getopts 'h' o; do case "$o" in + h) help >&2; exit ;; + *) err "invalid option -- '$OPTARG'" ;; +esac done +shift $((OPTIND - 1)) + +[ "$#" -lt 1 ] && help && exit 1 +[ "$#" -gt 1 ] && err "too many arguments. Only one expected" + + +new_script() { + cat << EOF > "$1" +#!/bin/sh + +help() { echo "$1 - + +USAGE: + $1 [OPTION]... + +OPTIONS: + -h show this help message"; } + +err() { printf '$1: %s\n' "\$@" >&2; exit 1; } +while getopts 'h' o; do case "\$o" in + h) help; exit ;; + *) err "invalid option -- '\$OPTARG'" ;; +esac done +shift \$((OPTIND - 1)) + +[ "\$#" -lt 1 ] && help >&2 && exit 1 + +EOF + + chmod u+x -- "$1" || exit +} + + +new_c_file() { + cat << EOF > "$1" +#include <stdio.h> + +int main() +{ + printf("Hello\n"); + + return 0; +} +EOF +} + + +new_cpp_file() { + cat << EOF > "$1" +#include <iostream> + +using namespace std; + +int main() +{ + cout << "Hello" << endl; + + cin.get(); + return 0; +} +EOF +} + + +new_py_file() { + cat << EOF > "$1" +import timeit + +print("Finished in %fs" % timeit.timeit()) +EOF +} + + +new_java_file() { + PATTERN="Scanner" + VIMCMDS="normal!2n\n noh" + cat << EOF > "$1" +import java.util.Scanner; + +class ${1%.java} { + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + + System.out.println("Finished!"); + sc.close + } +} +EOF +} + + +file="$1" + +new_file() { + case "$file" in + *.c) new_c_file "$file" ;; + *.cpp) new_cpp_file "$file" ;; + *.py) new_py_file "$file" ;; + *.java) new_java_file "$file" ;; + *) new_script "$file" ;; + esac +} + +if [ -f "$file" ]; then + [ ! -w "$file" ] && chmod u+rw "$file" + if [ -s "$file" ]; then + warn "'$file' already exists, edit existing file?" || exit + else + warn "an empty file '$file' already exists, fill and edit?" && + new_file "$file" + fi +elif [ -e "$file" ]; then + err "'$file' already exists and is a non-regular file" +else + new_file "$file" +fi + +ls -lF --color -- "$file" + +case "$EDITOR" in + '') echo "$0: \$EDITOR variable not set" >&2 && exit 1 ;; + vi|*vim*) + VIMCONF="$(mktemp)"; export VIMCONF + [ -n "$VIOPTS" ] && echo "set $VIOPTS" >> "$VIMCONF" + [ -n "$PATTERN" ] && echo "/$PATTERN" >> "$VIMCONF" + [ -n "$VIMCMDS" ] && echo "$VIMCMDS" >> "$VIMCONF" + $EDITOR -S "$VIMCONF" "$file" + rm -f "$VIMCONF" + ;; + *) $EDITOR "$file" ;; +esac; exit diff --git a/.local/bin/development/opout b/.local/bin/development/opout new file mode 100755 index 0000000..3adc8fd --- /dev/null +++ b/.local/bin/development/opout @@ -0,0 +1,12 @@ +#!/bin/sh + +basename="${1%.*}" +err() { printf "opout: %s\n" "$@" >&2; exit 1; } + +case "${*}" in + '') err "filename required" ;; + *.tex|*.sil|*.m[dse]|*.[rR]md|*.mom|*.[0-9]) target="$(getcomproot "$1" || echo "$1")" ; setsid -f "${OPENER:-xdg-open}" "${target%.*}".pdf >/dev/null 2>&1 ;; + *.html) setsid -f xdg-open "$basename".html >/dev/null 2>&1 ;; + *.sent) setsid -f sent "$1" >/dev/null 2>&1 ;; + *) err "file extension unmatched" +esac diff --git a/.local/bin/development/pipsearch b/.local/bin/development/pipsearch new file mode 100755 index 0000000..0d1d495 --- /dev/null +++ b/.local/bin/development/pipsearch @@ -0,0 +1,4 @@ +#!/bin/sh + +{ sleep 1 && tmux send-keys /search\ results Enter; } & +tmux new w3m "http://pypi.org/search?q=$*" diff --git a/.local/bin/development/py b/.local/bin/development/py new file mode 100755 index 0000000..af32c95 --- /dev/null +++ b/.local/bin/development/py @@ -0,0 +1,4 @@ +#!/bin/sh + +"${XDG_DATA_HOME:-$HOME/.local/share}/virtualenvs/main/bin/python" "$@" + diff --git a/.local/bin/development/pyh b/.local/bin/development/pyh new file mode 100755 index 0000000..fdd524a --- /dev/null +++ b/.local/bin/development/pyh @@ -0,0 +1,12 @@ +#!/bin/sh + +# python-help: get help for keywords in python + +[ -x "${XDG_DATA_HOME:-$HOME/.local/share}/virtualenvs/main/bin/python" ] && + alias python="${XDG_DATA_HOME:-$HOME/.local/share}/virtualenvs/main/bin/python" + +if [ "$#" -gt 0 ]; then + python -c "help('$*')" +else + python -c "help()" +fi diff --git a/.local/bin/development/texclear b/.local/bin/development/texclear new file mode 100755 index 0000000..6ad3c12 --- /dev/null +++ b/.local/bin/development/texclear @@ -0,0 +1,9 @@ +#!/bin/sh + +# Clears the build files of a LaTeX/XeLaTeX build. +# I have vim run this file whenever I exit a .tex file. + +[ "${1##*.}" = "tex" ] && { + find "$(dirname "${1}")" -regex '.*\(_minted.*\|.*\.\(4tc\|xref\|tmp\|pyc\|pyg\|pyo\|fls\|vrb\|fdb_latexmk\|bak\|swp\|aux\|log\|synctex\(busy\)\|lof\|lot\|maf\|idx\|mtc\|mtc0\|nav\|out\|snm\|toc\|bcf\|run\.xml\|synctex\.gz\|blg\|bbl\)\)' -delete +} || printf "Provide a .tex file.\n" + |