aboutsummaryrefslogtreecommitdiffhomepage
path: root/bin/misc
diff options
context:
space:
mode:
Diffstat (limited to 'bin/misc')
-rwxr-xr-xbin/misc/brightness.sh10
-rwxr-xr-xbin/misc/diff-highlight213
-rwxr-xr-xbin/misc/flash.sh39
-rwxr-xr-xbin/misc/mvt_decode.py11
-rwxr-xr-xbin/misc/ocr16
-rwxr-xr-xbin/misc/refresh-todo.sh5
-rwxr-xr-xbin/misc/simple_cors_server.py18
-rwxr-xr-xbin/misc/sync-gist.sh21
-rwxr-xr-xbin/misc/transfer22
9 files changed, 355 insertions, 0 deletions
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 @@
1#! /usr/bin/env bash
2
3BACKLIGHT_DIR=/sys/class/backlight/intel_backlight
4
5CURRENT=$(cat $BACKLIGHT_DIR/brightness)
6MAX=$(cat $BACKLIGHT_DIR/max_brightness)
7
8echo " $CURRENT + ( $MAX * ${1/+} )" | \
9bc | \
10cut -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 @@
1#!/usr/bin/perl
2
3use warnings FATAL => 'all';
4use strict;
5
6# Highlight by reversing foreground and background. You could do
7# other things like bold or underline if you prefer.
8my @OLD_HIGHLIGHT = (
9 color_config('color.diff-highlight.oldnormal'),
10 color_config('color.diff-highlight.oldhighlight', "\x1b[7m"),
11 color_config('color.diff-highlight.oldreset', "\x1b[27m")
12);
13my @NEW_HIGHLIGHT = (
14 color_config('color.diff-highlight.newnormal', $OLD_HIGHLIGHT[0]),
15 color_config('color.diff-highlight.newhighlight', $OLD_HIGHLIGHT[1]),
16 color_config('color.diff-highlight.newreset', $OLD_HIGHLIGHT[2])
17);
18
19my $RESET = "\x1b[m";
20my $COLOR = qr/\x1b\[[0-9;]*m/;
21my $BORING = qr/$COLOR|\s/;
22
23my @removed;
24my @added;
25my $in_hunk;
26
27# Some scripts may not realize that SIGPIPE is being ignored when launching the
28# pager--for instance scripts written in Python.
29$SIG{PIPE} = 'DEFAULT';
30
31while (<>) {
32 if (!$in_hunk) {
33 print;
34 $in_hunk = /^$COLOR*\@/;
35 }
36 elsif (/^$COLOR*-/) {
37 push @removed, $_;
38 }
39 elsif (/^$COLOR*\+/) {
40 push @added, $_;
41 }
42 else {
43 show_hunk(\@removed, \@added);
44 @removed = ();
45 @added = ();
46
47 print;
48 $in_hunk = /^$COLOR*[\@ ]/;
49 }
50
51 # Most of the time there is enough output to keep things streaming,
52 # but for something like "git log -Sfoo", you can get one early
53 # commit and then many seconds of nothing. We want to show
54 # that one commit as soon as possible.
55 #
56 # Since we can receive arbitrary input, there's no optimal
57 # place to flush. Flushing on a blank line is a heuristic that
58 # happens to match git-log output.
59 if (!length) {
60 local $| = 1;
61 }
62}
63
64# Flush any queued hunk (this can happen when there is no trailing context in
65# the final diff of the input).
66show_hunk(\@removed, \@added);
67
68exit 0;
69
70# Ideally we would feed the default as a human-readable color to
71# git-config as the fallback value. But diff-highlight does
72# not otherwise depend on git at all, and there are reports
73# of it being used in other settings. Let's handle our own
74# fallback, which means we will work even if git can't be run.
75sub color_config {
76 my ($key, $default) = @_;
77 my $s = `git config --get-color $key 2>/dev/null`;
78 return length($s) ? $s : $default;
79}
80
81sub show_hunk {
82 my ($a, $b) = @_;
83
84 # If one side is empty, then there is nothing to compare or highlight.
85 if (!@$a || !@$b) {
86 print @$a, @$b;
87 return;
88 }
89
90 # If we have mismatched numbers of lines on each side, we could try to
91 # be clever and match up similar lines. But for now we are simple and
92 # stupid, and only handle multi-line hunks that remove and add the same
93 # number of lines.
94 if (@$a != @$b) {
95 print @$a, @$b;
96 return;
97 }
98
99 my @queue;
100 for (my $i = 0; $i < @$a; $i++) {
101 my ($rm, $add) = highlight_pair($a->[$i], $b->[$i]);
102 print $rm;
103 push @queue, $add;
104 }
105 print @queue;
106}
107
108sub highlight_pair {
109 my @a = split_line(shift);
110 my @b = split_line(shift);
111
112 # Find common prefix, taking care to skip any ansi
113 # color codes.
114 my $seen_plusminus;
115 my ($pa, $pb) = (0, 0);
116 while ($pa < @a && $pb < @b) {
117 if ($a[$pa] =~ /$COLOR/) {
118 $pa++;
119 }
120 elsif ($b[$pb] =~ /$COLOR/) {
121 $pb++;
122 }
123 elsif ($a[$pa] eq $b[$pb]) {
124 $pa++;
125 $pb++;
126 }
127 elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
128 $seen_plusminus = 1;
129 $pa++;
130 $pb++;
131 }
132 else {
133 last;
134 }
135 }
136
137 # Find common suffix, ignoring colors.
138 my ($sa, $sb) = ($#a, $#b);
139 while ($sa >= $pa && $sb >= $pb) {
140 if ($a[$sa] =~ /$COLOR/) {
141 $sa--;
142 }
143 elsif ($b[$sb] =~ /$COLOR/) {
144 $sb--;
145 }
146 elsif ($a[$sa] eq $b[$sb]) {
147 $sa--;
148 $sb--;
149 }
150 else {
151 last;
152 }
153 }
154
155 if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
156 return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
157 highlight_line(\@b, $pb, $sb, \@NEW_HIGHLIGHT);
158 }
159 else {
160 return join('', @a),
161 join('', @b);
162 }
163}
164
165sub split_line {
166 local $_ = shift;
167 return map { /$COLOR/ ? $_ : (split //) }
168 split /($COLOR*)/;
169}
170
171sub highlight_line {
172 my ($line, $prefix, $suffix, $theme) = @_;
173
174 my $start = join('', @{$line}[0..($prefix-1)]);
175 my $mid = join('', @{$line}[$prefix..$suffix]);
176 my $end = join('', @{$line}[($suffix+1)..$#$line]);
177
178 # If we have a "normal" color specified, then take over the whole line.
179 # Otherwise, we try to just manipulate the highlighted bits.
180 if (defined $theme->[0]) {
181 s/$COLOR//g for ($start, $mid, $end);
182 chomp $end;
183 return join('',
184 $theme->[0], $start, $RESET,
185 $theme->[1], $mid, $RESET,
186 $theme->[0], $end, $RESET,
187 "\n"
188 );
189 } else {
190 return join('',
191 $start,
192 $theme->[1], $mid, $theme->[2],
193 $end
194 );
195 }
196}
197
198# Pairs are interesting to highlight only if we are going to end up
199# highlighting a subset (i.e., not the whole line). Otherwise, the highlighting
200# is just useless noise. We can detect this by finding either a matching prefix
201# or suffix (disregarding boring bits like whitespace and colorization).
202sub is_pair_interesting {
203 my ($a, $pa, $sa, $b, $pb, $sb) = @_;
204 my $prefix_a = join('', @$a[0..($pa-1)]);
205 my $prefix_b = join('', @$b[0..($pb-1)]);
206 my $suffix_a = join('', @$a[($sa+1)..$#$a]);
207 my $suffix_b = join('', @$b[($sb+1)..$#$b]);
208
209 return $prefix_a !~ /^$COLOR*-$BORING*$/ ||
210 $prefix_b !~ /^$COLOR*\+$BORING*$/ ||
211 $suffix_a !~ /^$BORING*$/ ||
212 $suffix_b !~ /^$BORING*$/;
213}
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 @@
1#! /bin/bash
2
3while true; do
4 CARDS="$(cat ~/log/flashcards.md | shuf | head -5)"
5 CARD="$(<<<"$CARDS" sed -n 3p)"
6
7 # Print the Question
8 <<<"$CARD" tr -s '\t' | cut -f1
9 echo
10 tput bold; tput setaf 1
11 <<<"$CARDS" tr -s '\t' | cut -f2 | tr '\n' '\t'
12 tput sgr0
13 echo
14 echo
15 echo ----
16 echo
17
18 # Get the User Input
19 read -er INPUT
20
21 # Print the Answer
22 ANSER=$(<<<"$CARD" tr -s '\t' | cut -f2)
23 echo
24 echo ----
25 echo
26
27 # If answer correctly, print the checked box
28 if [[ "$INPUT" == "$ANSER" ]]; then
29 tput setaf 2
30 echo '☑'
31 tput setaf 7
32 else
33 echo $ANSER
34 fi
35
36 echo
37 read
38 tput clear
39done
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 @@
1#! /bin/env python3
2
3import mapbox_vector_tile
4import sys
5
6mvt = sys.argv[1]
7
8with open(mvt, 'rb') as f:
9 data = f.read()
10 decoded_data = mapbox_vector_tile.decode(data)
11 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 @@
1#!/bin/bash
2# Dependencies: tesseract-ocr imagemagick scrot xsel
3
4SCR_IMG=`mktemp`
5trap "rm $SCR_IMG*" EXIT
6
7scrot -s $SCR_IMG.png -q 100
8# increase image quality with option -q from default 75 to 100
9
10mogrify -modulate 100,0 -resize 400% $SCR_IMG.png
11#should increase detection rate
12
13tesseract $SCR_IMG.png $SCR_IMG &> /dev/null
14cat $SCR_IMG.txt | trans -t zh-TW --brief --engine bing | cat $SCR_IMG.txt - | tr '\n' ' ' | xargs -I{} notify-send --urgency=critical translation '{}'
15
16exit
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 @@
1#!/bin/bash
2
3# $1 as file, $2 as topic (Daily, Weekly, Monthly)
4# change markdown check-list to empty
5sed -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 @@
1#!/usr/bin/env python3
2# encoding: utf-8
3"""Use instead of `python3 -m http.server` when you need CORS"""
4
5from http.server import HTTPServer, SimpleHTTPRequestHandler
6
7
8class CORSRequestHandler(SimpleHTTPRequestHandler):
9 def end_headers(self):
10 self.send_header('Access-Control-Allow-Origin', '*')
11 self.send_header('Access-Control-Allow-Methods', 'GET')
12 self.send_header('Cache-Control', 'no-store, no-cache, must-revalidate')
13 return super(CORSRequestHandler, self).end_headers()
14
15
16httpd = HTTPServer(('localhost', 8003), CORSRequestHandler)
17print('check localhost:8003')
18httpd.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 @@
1#! /bin/bash
2
3set -o pipefail
4set -e
5
6repo=~/gist/b0d2e7e67aa50298fdf8111ae7466b56
7
8while read -r commit; do
9
10 cd $repo
11 git checkout $commit
12 message="$(git show $commit | sed -n '1,4 d; /^diff/ q; s/^ //p')"
13
14 cd ~/git/Bash-Snippets
15 cp $repo/gist gist/gist
16 git add gist/gist && git commit -m "$message" || true
17done
18
19cd $repo
20git checkout master
21
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 @@
1#! /bin/env sh
2
3if [ $# -eq 0 ];then
4 echo "No arguments specified.\nUsage:\n transfer <file|directory>\n ... | transfer <file_name>">&2;
5 return 1;
6fi;
7if tty -s; then
8 file="$1"; file_name=$(basename "$file");
9 if [ ! -e "$file" ]; then
10 echo "$file: No such file or directory">&2;
11 return 1;
12 fi;
13 if [ -d "$file" ];then
14 file_name="$file_name.zip" ,;
15 (cd "$file" && zip -r -q - .) | curl -w '\n' --progress-bar --upload-file "-" "https://topo.tw/up/$file_name" | tee /dev/null;
16 else
17 cat "$file" | curl -w '\n' --progress-bar --upload-file "-" "https://topo.tw/up/$file_name" | tee /dev/null;
18 fi;
19else
20 file_name=$1;
21 curl -w '\n' --progress-bar --upload-file "-" "https://topo.tw/up/$file_name" | tee /dev/null;
22fi;