From 6fae25b305d714b3ab7608fa003f1af9bf024545 Mon Sep 17 00:00:00 2001 From: Hsieh Chin Fan Date: Tue, 14 Feb 2023 13:33:23 +0800 Subject: Rename tools into bin --- bin/misc/brightness.sh | 10 ++ bin/misc/diff-highlight | 213 +++++++++++++++++++++++++++++++++++++++++ bin/misc/flash.sh | 39 ++++++++ bin/misc/mvt_decode.py | 11 +++ bin/misc/ocr | 16 ++++ bin/misc/refresh-todo.sh | 5 + bin/misc/simple_cors_server.py | 18 ++++ bin/misc/sync-gist.sh | 21 ++++ bin/misc/transfer | 22 +++++ 9 files changed, 355 insertions(+) create mode 100755 bin/misc/brightness.sh create mode 100755 bin/misc/diff-highlight create mode 100755 bin/misc/flash.sh create mode 100755 bin/misc/mvt_decode.py create mode 100755 bin/misc/ocr create mode 100755 bin/misc/refresh-todo.sh create mode 100755 bin/misc/simple_cors_server.py create mode 100755 bin/misc/sync-gist.sh create mode 100755 bin/misc/transfer (limited to 'bin/misc') diff --git a/bin/misc/brightness.sh b/bin/misc/brightness.sh new file mode 100755 index 0000000..4f548f5 --- /dev/null +++ b/bin/misc/brightness.sh @@ -0,0 +1,10 @@ +#! /usr/bin/env bash + +BACKLIGHT_DIR=/sys/class/backlight/intel_backlight + +CURRENT=$(cat $BACKLIGHT_DIR/brightness) +MAX=$(cat $BACKLIGHT_DIR/max_brightness) + +echo " $CURRENT + ( $MAX * ${1/+} )" | \ +bc | \ +cut -d'.' -f1 >$BACKLIGHT_DIR/brightness diff --git a/bin/misc/diff-highlight b/bin/misc/diff-highlight new file mode 100755 index 0000000..08c88bb --- /dev/null +++ b/bin/misc/diff-highlight @@ -0,0 +1,213 @@ +#!/usr/bin/perl + +use warnings FATAL => 'all'; +use strict; + +# Highlight by reversing foreground and background. You could do +# other things like bold or underline if you prefer. +my @OLD_HIGHLIGHT = ( + color_config('color.diff-highlight.oldnormal'), + color_config('color.diff-highlight.oldhighlight', "\x1b[7m"), + color_config('color.diff-highlight.oldreset', "\x1b[27m") +); +my @NEW_HIGHLIGHT = ( + color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]), + color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]), + color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2]) +); + +my $RESET = "\x1b[m"; +my $COLOR = qr/\x1b\[[0-9;]*m/; +my $BORING = qr/$COLOR|\s/; + +my @removed; +my @added; +my $in_hunk; + +# Some scripts may not realize that SIGPIPE is being ignored when launching the +# pager--for instance scripts written in Python. +$SIG{PIPE} = 'DEFAULT'; + +while (<>) { + if (!$in_hunk) { + print; + $in_hunk = /^$COLOR*\@/; + } + elsif (/^$COLOR*-/) { + push @removed, $_; + } + elsif (/^$COLOR*\+/) { + push @added, $_; + } + else { + show_hunk(\@removed, \@added); + @removed = (); + @added = (); + + print; + $in_hunk = /^$COLOR*[\@ ]/; + } + + # Most of the time there is enough output to keep things streaming, + # but for something like "git log -Sfoo", you can get one early + # commit and then many seconds of nothing. We want to show + # that one commit as soon as possible. + # + # Since we can receive arbitrary input, there's no optimal + # place to flush. Flushing on a blank line is a heuristic that + # happens to match git-log output. + if (!length) { + local $| = 1; + } +} + +# Flush any queued hunk (this can happen when there is no trailing context in +# the final diff of the input). +show_hunk(\@removed, \@added); + +exit 0; + +# Ideally we would feed the default as a human-readable color to +# git-config as the fallback value. But diff-highlight does +# not otherwise depend on git at all, and there are reports +# of it being used in other settings. Let's handle our own +# fallback, which means we will work even if git can't be run. +sub color_config { + my ($key, $default) = @_; + my $s = `git config --get-color $key 2>/dev/null`; + return length($s) ? $s : $default; +} + +sub show_hunk { + my ($a, $b) = @_; + + # If one side is empty, then there is nothing to compare or highlight. + if (!@$a || !@$b) { + print @$a, @$b; + return; + } + + # If we have mismatched numbers of lines on each side, we could try to + # be clever and match up similar lines. But for now we are simple and + # stupid, and only handle multi-line hunks that remove and add the same + # number of lines. + if (@$a != @$b) { + print @$a, @$b; + return; + } + + my @queue; + for (my $i = 0; $i < @$a; $i++) { + my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]); + print $rm; + push @queue, $add; + } + print @queue; +} + +sub highlight_pair { + my @a = split_line(shift); + my @b = split_line(shift); + + # Find common prefix, taking care to skip any ansi + # color codes. + my $seen_plusminus; + my ($pa, $pb) = (0, 0); + while ($pa < @a && $pb < @b) { + if ($a[$pa] =~ /$COLOR/) { + $pa++; + } + elsif ($b[$pb] =~ /$COLOR/) { + $pb++; + } + elsif ($a[$pa] eq $b[$pb]) { + $pa++; + $pb++; + } + elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') { + $seen_plusminus = 1; + $pa++; + $pb++; + } + else { + last; + } + } + + # Find common suffix, ignoring colors. + my ($sa, $sb) = ($#a, $#b); + while ($sa >= $pa && $sb >= $pb) { + if ($a[$sa] =~ /$COLOR/) { + $sa--; + } + elsif ($b[$sb] =~ /$COLOR/) { + $sb--; + } + elsif ($a[$sa] eq $b[$sb]) { + $sa--; + $sb--; + } + else { + last; + } + } + + if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) { + return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT), + highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT); + } + else { + return join('', @a), + join('', @b); + } +} + +sub split_line { + local $_ = shift; + return map { /$COLOR/ ? $_ : (split //) } + split /($COLOR*)/; +} + +sub highlight_line { + my ($line, $prefix, $suffix, $theme) = @_; + + my $start = join('', @{$line}[0..($prefix-1)]); + my $mid = join('', @{$line}[$prefix..$suffix]); + my $end = join('', @{$line}[($suffix+1)..$#$line]); + + # If we have a "normal" color specified, then take over the whole line. + # Otherwise, we try to just manipulate the highlighted bits. + if (defined $theme->[0]) { + s/$COLOR//g for ($start, $mid, $end); + chomp $end; + return join('', + $theme->[0], $start, $RESET, + $theme->[1], $mid, $RESET, + $theme->[0], $end, $RESET, + "\n" + ); + } else { + return join('', + $start, + $theme->[1], $mid, $theme->[2], + $end + ); + } +} + +# Pairs are interesting to highlight only if we are going to end up +# highlighting a subset (i.e., not the whole line). Otherwise, the highlighting +# is just useless noise. We can detect this by finding either a matching prefix +# or suffix (disregarding boring bits like whitespace and colorization). +sub is_pair_interesting { + my ($a, $pa, $sa, $b, $pb, $sb) = @_; + my $prefix_a = join('', @$a[0..($pa-1)]); + my $prefix_b = join('', @$b[0..($pb-1)]); + my $suffix_a = join('', @$a[($sa+1)..$#$a]); + my $suffix_b = join('', @$b[($sb+1)..$#$b]); + + return $prefix_a !~ /^$COLOR*-$BORING*$/ || + $prefix_b !~ /^$COLOR*\+$BORING*$/ || + $suffix_a !~ /^$BORING*$/ || + $suffix_b !~ /^$BORING*$/; +} diff --git a/bin/misc/flash.sh b/bin/misc/flash.sh new file mode 100755 index 0000000..93ce8fd --- /dev/null +++ b/bin/misc/flash.sh @@ -0,0 +1,39 @@ +#! /bin/bash + +while true; do + CARDS="$(cat ~/log/flashcards.md | shuf | head -5)" + CARD="$(<<<"$CARDS" sed -n 3p)" + + # Print the Question + <<<"$CARD" tr -s '\t' | cut -f1 + echo + tput bold; tput setaf 1 + <<<"$CARDS" tr -s '\t' | cut -f2 | tr '\n' '\t' + tput sgr0 + echo + echo + echo ---- + echo + + # Get the User Input + read -er INPUT + + # Print the Answer + ANSER=$(<<<"$CARD" tr -s '\t' | cut -f2) + echo + echo ---- + echo + + # If answer correctly, print the checked box + if [[ "$INPUT" == "$ANSER" ]]; then + tput setaf 2 + echo '☑' + tput setaf 7 + else + echo $ANSER + fi + + echo + read + tput clear +done diff --git a/bin/misc/mvt_decode.py b/bin/misc/mvt_decode.py new file mode 100755 index 0000000..7c9ac89 --- /dev/null +++ b/bin/misc/mvt_decode.py @@ -0,0 +1,11 @@ +#! /bin/env python3 + +import mapbox_vector_tile +import sys + +mvt = sys.argv[1] + +with open(mvt, 'rb') as f: + data = f.read() + decoded_data = mapbox_vector_tile.decode(data) + print(decoded_data) diff --git a/bin/misc/ocr b/bin/misc/ocr new file mode 100755 index 0000000..26594fe --- /dev/null +++ b/bin/misc/ocr @@ -0,0 +1,16 @@ +#!/bin/bash +# Dependencies: tesseract-ocr imagemagick scrot xsel + +SCR_IMG=`mktemp` +trap "rm $SCR_IMG*" EXIT + +scrot -s $SCR_IMG.png -q 100 +# increase image quality with option -q from default 75 to 100 + +mogrify -modulate 100,0 -resize 400% $SCR_IMG.png +#should increase detection rate + +tesseract $SCR_IMG.png $SCR_IMG &> /dev/null +cat $SCR_IMG.txt | trans -t zh-TW --brief --engine bing | cat $SCR_IMG.txt - | tr '\n' ' ' | xargs -I{} notify-send --urgency=critical translation '{}' + +exit diff --git a/bin/misc/refresh-todo.sh b/bin/misc/refresh-todo.sh new file mode 100755 index 0000000..7931536 --- /dev/null +++ b/bin/misc/refresh-todo.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +# $1 as file, $2 as topic (Daily, Weekly, Monthly) +# change markdown check-list to empty +sed -i "/^## $2/,/^$/ s/\[.\]/\[ \]/" $1 diff --git a/bin/misc/simple_cors_server.py b/bin/misc/simple_cors_server.py new file mode 100755 index 0000000..9d2c898 --- /dev/null +++ b/bin/misc/simple_cors_server.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 +# encoding: utf-8 +"""Use instead of `python3 -m http.server` when you need CORS""" + +from http.server import HTTPServer, SimpleHTTPRequestHandler + + +class CORSRequestHandler(SimpleHTTPRequestHandler): + def end_headers(self): + self.send_header('Access-Control-Allow-Origin', '*') + self.send_header('Access-Control-Allow-Methods', 'GET') + self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate') + return super(CORSRequestHandler, self).end_headers() + + +httpd = HTTPServer(('localhost', 8003), CORSRequestHandler) +print('check localhost:8003') +httpd.serve_forever() diff --git a/bin/misc/sync-gist.sh b/bin/misc/sync-gist.sh new file mode 100755 index 0000000..86cca04 --- /dev/null +++ b/bin/misc/sync-gist.sh @@ -0,0 +1,21 @@ +#! /bin/bash + +set -o pipefail +set -e + +repo=~/gist/b0d2e7e67aa50298fdf8111ae7466b56 + +while read -r commit; do + + cd $repo + git checkout $commit + message="$(git show $commit | sed -n '1,4 d; /^diff/ q; s/^ //p')" + + cd ~/git/Bash-Snippets + cp $repo/gist gist/gist + git add gist/gist && git commit -m "$message" || true +done + +cd $repo +git checkout master + diff --git a/bin/misc/transfer b/bin/misc/transfer new file mode 100755 index 0000000..1fc2117 --- /dev/null +++ b/bin/misc/transfer @@ -0,0 +1,22 @@ +#! /bin/env sh + +if [ $# -eq 0 ];then + echo "No arguments specified.\nUsage:\n transfer \n ... | transfer ">&2; + return 1; +fi; +if tty -s; then + file="$1"; file_name=$(basename "$file"); + if [ ! -e "$file" ]; then + echo "$file: No such file or directory">&2; + return 1; + fi; + if [ -d "$file" ];then + file_name="$file_name.zip" ,; + (cd "$file" && zip -r -q - .) | curl -w '\n' --progress-bar --upload-file "-" "https://topo.tw/up/$file_name" | tee /dev/null; + else + cat "$file" | curl -w '\n' --progress-bar --upload-file "-" "https://topo.tw/up/$file_name" | tee /dev/null; + fi; +else + file_name=$1; + curl -w '\n' --progress-bar --upload-file "-" "https://topo.tw/up/$file_name" | tee /dev/null; +fi; -- cgit v1.2.3-70-g09d2