From 6617db5d44b2d4a4c2feba0aa72b9657c0698e4b Mon Sep 17 00:00:00 2001 From: Anthony Axenov Date: Wed, 12 Nov 2025 18:17:47 +0800 Subject: [PATCH] utils --- README.md | 3 +- utils/.local/bin/clock | 3 ++ utils/.local/bin/copy | 18 ++++++++++ utils/.local/bin/dc | 26 ++++++++++++++ utils/.local/bin/extract | 22 ++++++++++++ utils/.local/bin/flushdns | 3 ++ utils/.local/bin/httpcode | 69 ++++++++++++++++++++++++++++++++++++++ utils/.local/bin/len | 5 +++ utils/.local/bin/line | 6 ++++ utils/.local/bin/mksh | 23 +++++++++++++ utils/.local/bin/now | 3 ++ utils/.local/bin/pasta | 13 +++++++ utils/.local/bin/perm | 6 ++++ utils/.local/bin/running | 13 +++++++ utils/.local/bin/scratch | 8 +++++ utils/.local/bin/serve | 25 ++++++++++++++ utils/.local/bin/timer | 11 ++++++ utils/.local/bin/trash | 6 ++++ utils/.local/bin/tryna | 8 +++++ utils/.local/bin/trynafail | 8 +++++ utils/.local/bin/url2md | 14 ++++++++ utils/.local/bin/waitfor | 13 +++++++ 22 files changed, 305 insertions(+), 1 deletion(-) create mode 100755 utils/.local/bin/clock create mode 100755 utils/.local/bin/copy create mode 100755 utils/.local/bin/dc create mode 100755 utils/.local/bin/extract create mode 100755 utils/.local/bin/flushdns create mode 100755 utils/.local/bin/httpcode create mode 100755 utils/.local/bin/len create mode 100755 utils/.local/bin/line create mode 100755 utils/.local/bin/mksh create mode 100755 utils/.local/bin/now create mode 100755 utils/.local/bin/pasta create mode 100755 utils/.local/bin/perm create mode 100755 utils/.local/bin/running create mode 100755 utils/.local/bin/scratch create mode 100755 utils/.local/bin/serve create mode 100755 utils/.local/bin/timer create mode 100755 utils/.local/bin/trash create mode 100755 utils/.local/bin/tryna create mode 100755 utils/.local/bin/trynafail create mode 100755 utils/.local/bin/url2md create mode 100755 utils/.local/bin/waitfor diff --git a/README.md b/README.md index 5778cfe..36eee60 100644 --- a/README.md +++ b/README.md @@ -17,8 +17,9 @@ stow -v git -D Target (`-t`) defaults to the parent of `pwd`, so if you clone this repo not in `$HOME` then you MUST explicitly provide `-t ~` or `-t $HOME` every time. -## Documentation +## Documentation and sources * * * +* https://github.com/jimeh/git-aware-stow diff --git a/utils/.local/bin/clock b/utils/.local/bin/clock new file mode 100755 index 0000000..82ca65b --- /dev/null +++ b/utils/.local/bin/clock @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +watch -tn 1 date '+%l:%M:%S%p' diff --git a/utils/.local/bin/copy b/utils/.local/bin/copy new file mode 100755 index 0000000..fde9f51 --- /dev/null +++ b/utils/.local/bin/copy @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +set -e +set -u + +if hash pbcopy 2>/dev/null; then + exec pbcopy +elif hash xclip 2>/dev/null; then + exec xclip -selection clipboard +elif hash putclip 2>/dev/null; then + exec putclip +else + rm -f /tmp/clipboard 2> /dev/null + if [ $# -eq 0 ]; then + cat > /tmp/clipboard + else + cat "$1" > /tmp/clipboard + fi +fi diff --git a/utils/.local/bin/dc b/utils/.local/bin/dc new file mode 100755 index 0000000..6a1ba11 --- /dev/null +++ b/utils/.local/bin/dc @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +CONTAINER="my-container" # the name of the container in which to 'exec' something +CONFIG="$(dirname $([ -L $0 ] && readlink -f $0 || echo $0))/docker-compose.yml" # path to compose yml file +CMD="docker-compose -f $CONFIG" # docker-compose command +APP_URL='http://localhost:8000/' + +open_browser() { + if which xdg-open > /dev/null; then + xdg-open "$1" /dev/null 2>&1 & disown + elif which gnome-open > /dev/null; then + gnome-open "$1" /dev/null 2>&1 & disown + fi +} + +case "$1" in + '' | 'help' ) echo -e "Provide one of operations: \t start, stop, up, down, restart, rebuild, open"; + echo "Otherwise all args will be passed to 'docker exec -ti $CONTAINER ...'" ;; + 'open' ) open_browser $APP_URL ;; + 'up' ) $CMD up -d --build ;; # build and start containers + 'down' ) $CMD down --remove-orphans ;; # stop and remove containers + 'start' ) $CMD start ;; # start containers + 'stop' ) $CMD stop ;; # stop containers + 'restart' ) $CMD stop && $CMD start ;; # restart containers + 'rebuild' ) $CMD down --remove-orphans && $CMD up -d --build ;; # rebuild containers + * ) docker exec -ti $CONTAINER $@ # exec anything in container +esac diff --git a/utils/.local/bin/extract b/utils/.local/bin/extract new file mode 100755 index 0000000..5739669 --- /dev/null +++ b/utils/.local/bin/extract @@ -0,0 +1,22 @@ +#!/usr/bin/env bash +if [ -f "$1" ]; then + case "$1" in + *.tar.bz2) tar -jxvf "$1" ;; + *.tar.gz) tar -zxvf "$1" ;; + *.bz2) bunzip2 "$1" ;; + *.dmg) hdiutil mount "$1" ;; + *.gz) gunzip "$1" ;; + *.tar) tar -xvf "$1" ;; + *.tbz2) tar -jxvf "$1" ;; + *.tgz) tar -zxvf "$1" ;; + *.zip) unzip "$1" ;; + *.ZIP) unzip "$1" ;; + *.pax) cat "$1" | pax -r ;; + *.pax.Z) uncompress "$1" --stdout | pax -r ;; + *.rar) unrar x "$1" ;; + *.Z) uncompress "$1" ;; + *) echo "'$1' cannot be extracted/mounted via extract()" ;; + esac +else + echo "'$1' is not a valid file" +fi diff --git a/utils/.local/bin/flushdns b/utils/.local/bin/flushdns new file mode 100755 index 0000000..0b3aac3 --- /dev/null +++ b/utils/.local/bin/flushdns @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +sudo resolvectl flush-caches diff --git a/utils/.local/bin/httpcode b/utils/.local/bin/httpcode new file mode 100755 index 0000000..ca8cbd2 --- /dev/null +++ b/utils/.local/bin/httpcode @@ -0,0 +1,69 @@ +#!/usr/bin/env bash +set -e +set -u + +statuses="100 Continue +101 Switching Protocols +102 Processing +200 OK +201 Created +202 Accepted +203 Non-Authoritative Information +204 No Content +205 Reset Content +206 Partial Content +207 Multi-Status +208 Already Reported +300 Multiple Choices +301 Moved Permanently +302 Found +303 See Other +304 Not Modified +305 Use Proxy +307 Temporary Redirect +400 Bad Request +401 Unauthorized +402 Payment Required +403 Forbidden +404 Not Found +405 Method Not Allowed +406 Not Acceptable +407 Proxy Authentication Required +408 Request Timeout +409 Conflict +410 Gone +411 Length Required +412 Precondition Failed +413 Request Entity Too Large +414 Request-URI Too Large +415 Unsupported Media Type +416 Request Range Not Satisfiable +417 Expectation Failed +418 I'm a teapot +420 Blaze it +422 Unprocessable Entity +423 Locked +424 Failed Dependency +425 No code +426 Upgrade Required +428 Precondition Required +429 Too Many Requests +431 Request Header Fields Too Large +449 Retry with +500 Internal Server Error +501 Not Implemented +502 Bad Gateway +503 Service Unavailable +504 Gateway Timeout +505 HTTP Version Not Supported +506 Variant Also Negotiates +507 Insufficient Storage +509 Bandwidth Limit Exceeded +510 Not Extended +511 Network Authentication Required" + +if [ $# -eq 0 ]; then + echo "$statuses" +else + echo "$statuses" | grep -i --color=never "$@" +fi diff --git a/utils/.local/bin/len b/utils/.local/bin/len new file mode 100755 index 0000000..46978e1 --- /dev/null +++ b/utils/.local/bin/len @@ -0,0 +1,5 @@ +#!/usr/bin/env bash +set -e +set -u + +echo -n "$@" | wc -c | awk '{print $1}' diff --git a/utils/.local/bin/line b/utils/.local/bin/line new file mode 100755 index 0000000..e0da158 --- /dev/null +++ b/utils/.local/bin/line @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -e +set -u + +lineno="$1"; shift +sed -n "${lineno}p" -- "$@" diff --git a/utils/.local/bin/mksh b/utils/.local/bin/mksh new file mode 100755 index 0000000..1598771 --- /dev/null +++ b/utils/.local/bin/mksh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +if [ ! $# -eq 1 ]; then + echo 'mksh takes one argument' 1>&2 + exit 1 +elif [ -e "$1" ]; then + echo "$1 already exists" 1>&2 + exit 1 +fi + +echo '#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +' > "$1" + +chmod u+x "$1" + +nano "$1" diff --git a/utils/.local/bin/now b/utils/.local/bin/now new file mode 100755 index 0000000..20359c5 --- /dev/null +++ b/utils/.local/bin/now @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +date '+%d.%m.%Y %H:%M:%S' diff --git a/utils/.local/bin/pasta b/utils/.local/bin/pasta new file mode 100755 index 0000000..d04e0a3 --- /dev/null +++ b/utils/.local/bin/pasta @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e +set -u + +if hash pbpaste 2>/dev/null; then + exec pbpaste +elif hash xclip 2>/dev/null; then + exec xclip -selection clipboard -o +elif [[ -e /tmp/clipboard ]]; then + exec cat /tmp/clipboard +else + echo '' +fi diff --git a/utils/.local/bin/perm b/utils/.local/bin/perm new file mode 100755 index 0000000..64d8a3a --- /dev/null +++ b/utils/.local/bin/perm @@ -0,0 +1,6 @@ +#!/usr/bin/env bash + +# TODO permissions via arguments + +find . -type f -exec chmod 0664 {} + +find . -type d -exec chmod 0775 {} + diff --git a/utils/.local/bin/running b/utils/.local/bin/running new file mode 100755 index 0000000..268e312 --- /dev/null +++ b/utils/.local/bin/running @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e +set -u + +process_list="$(ps -eo 'pid command')" +if [[ $# != 0 ]]; then + process_list="$(echo "$process_list" | grep -Fiw "$@")" +fi + +echo "$process_list" | + grep -Fv "${BASH_SOURCE[0]}" | + grep -Fv grep | + GREP_COLORS='mt=00;35' grep -E --colour=auto '^\s*[[:digit:]]+' diff --git a/utils/.local/bin/scratch b/utils/.local/bin/scratch new file mode 100755 index 0000000..c8d74ce --- /dev/null +++ b/utils/.local/bin/scratch @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +file="$(mktemp)" +echo "Editing $file" +exec "$EDITOR" "$file" diff --git a/utils/.local/bin/serve b/utils/.local/bin/serve new file mode 100755 index 0000000..3b46767 --- /dev/null +++ b/utils/.local/bin/serve @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +port='8888' +if [ $# -eq 1 ]; then + port="$1" +fi + +if hash php 2>/dev/null; then + exec php -S "localhost:$port" +elif hash python3 2>/dev/null; then + exec python3 -m http.server "$port" +elif hash python 2>/dev/null; then + major_version="$(python -c 'import platform as p;print(p.python_version_tuple()[0])')" + if [[ "$major_version" == '3' ]]; then + exec python -m http.server "$port" + else + exec python -m SimpleHTTPServer "$port" + fi +else + echo 'unable to start HTTP server' 1>&2 + exit 1 +fi diff --git a/utils/.local/bin/timer b/utils/.local/bin/timer new file mode 100755 index 0000000..f092805 --- /dev/null +++ b/utils/.local/bin/timer @@ -0,0 +1,11 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +sleep "$1" +notify-send 'Timer complete!' \ + -u normal \ + -t 10000 \ + -i clock \ + -a 'Timer script' diff --git a/utils/.local/bin/trash b/utils/.local/bin/trash new file mode 100755 index 0000000..72d85b2 --- /dev/null +++ b/utils/.local/bin/trash @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +gio trash "$@" diff --git a/utils/.local/bin/tryna b/utils/.local/bin/tryna new file mode 100755 index 0000000..813be3d --- /dev/null +++ b/utils/.local/bin/tryna @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -u + +"$@" +while [[ "$?" -eq 0 ]]; do + sleep 0.5 + "$@" +done diff --git a/utils/.local/bin/trynafail b/utils/.local/bin/trynafail new file mode 100755 index 0000000..eb127e4 --- /dev/null +++ b/utils/.local/bin/trynafail @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +set -u + +"$@" +while [[ "$?" -ne 0 ]]; do + sleep 0.5 + "$@" +done diff --git a/utils/.local/bin/url2md b/utils/.local/bin/url2md new file mode 100755 index 0000000..79ad4ab --- /dev/null +++ b/utils/.local/bin/url2md @@ -0,0 +1,14 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +curl "https://r.jina.ai/$1" \ + -sS \ + -H "DNT: 1" \ + -H "X-Base: final" \ + -H "X-Engine: direct" \ + -H "X-Md-Em-Delimiter: *" \ + -H "X-Md-Heading-Style: setext" \ + -H "X-Md-Link-Reference-Style: collapsed" \ + -H "X-Md-Link-Style: referenced" diff --git a/utils/.local/bin/waitfor b/utils/.local/bin/waitfor new file mode 100755 index 0000000..e2d7602 --- /dev/null +++ b/utils/.local/bin/waitfor @@ -0,0 +1,13 @@ +#!/usr/bin/env bash +set -e +set -u +set -o pipefail + +if hash systemd-inhibit 2>/dev/null; then + systemd-inhibit \ + --who=waitfor \ + --why="Awaiting PID $1" \ + tail --pid="$1" -f /dev/null +else + tail --pid="$1" -f /dev/null +fi