Add a mathematical character selection script

Also updated the gitignore generation script with a little bit of
checking on it.
This commit is contained in:
Gabriel Arazas 2021-03-07 13:49:54 +08:00
parent 2ab1c315bd
commit 4e8f036b73
2 changed files with 48 additions and 1 deletions

47
bin/choose-math-char Executable file
View File

@ -0,0 +1,47 @@
#!/usr/bin/env osh
# Create an interface for select Unicode characters with rofi and copy it into the clipboard.
# The characters are extracted from the Unicode Character Database (https://www.unicode.org/ucd/).
# Dependencies:
# * Oil shell
# * curl
# * python (at least v3.7)
# * rofi
# * awk
# Set to use Oil features and strictness.
shopt --set strict:all oil:all
var UCD_VERSION = "13.0.0"
var UCD_XML_URL = "https://www.unicode.org/Public/$UCD_VERSION/ucdxml/ucd.nounihan.grouped.zip"
var CACHE = ${XDG_CACHE_DIR:-$HOME/.cache/unicode-character-database}
mkdir -p $CACHE
if test ! -f $CACHE/ucd.zip {
curl $UCD_XML_URL --output $CACHE/ucd.zip --silent
}
# The remaining thing is coded with Python because I want to use the UCD with no Unihan data and the grouped variation.
# Compared to the other variations, it is only ~6MB compared to ~55MB for the flat variation.
# Also, it requires more conditional handling than a simple shell script at this point.
# I could've made this script entirely in Python but I want to see what Oil shell is capable of.
python <<CODE | rofi -dmenu -p "Choose mathematical character" | awk '{ print $1 }' | xclip -selection clipboard
import xml.etree.ElementTree as ET
root = ET.fromstring('''$(unzip -p $CACHE/ucd.zip ucd.nounihan.grouped.xml)''')
# Print '<char>' and recurse into '<group>'.
def print_char(element):
if element.tag == '{http://www.unicode.org/ns/2003/ucd/1.0}char':
alias = element.get('na') if element.get('na') else element.get('na1')
codepoint = int(element.get('cp'), 16)
print("{code} {alias}".format(code=chr(codepoint), alias=alias))
elif element.tag == '{http://www.unicode.org/ns/2003/ucd/1.0}group':
for child in list(element):
print_char(child)
math_nodes = root.findall('./{http://www.unicode.org/ns/2003/ucd/1.0}repertoire//*[@Math="Y"]')
for point in math_nodes:
print_char(point)
CODE

View File

@ -18,7 +18,7 @@ CACHE_FILE="${XDG_CACHE_DIR:-$HOME/.cache}/gitignore-io.langs.json"
# Check if the language list is downloaded for the last hour (3600 seconds).
if [ ! -e $CACHE_FILE ] || test $(expr $(date "+%s") - $(date -r $CACHE_FILE "+%s")) -gt 3600
then
curl --silent --location --output $CACHE_FILE "https://gitignore.io/api/list?format=json"
ping "gitignore.io" --count 4 && curl --silent --location --output $CACHE_FILE "https://gitignore.io/api/list?format=json"
fi
KEYS=$(jq 'keys | .[] | @text' --raw-output $CACHE_FILE | fzf --multi | while read lang; do echo " .[\"$lang\"].contents"; done | paste -s -d ',')