From 4e8f036b73a71d02f5909f4f28898a79c2311147 Mon Sep 17 00:00:00 2001 From: Gabriel Arazas Date: Sun, 7 Mar 2021 13:49:54 +0800 Subject: [PATCH] Add a mathematical character selection script Also updated the gitignore generation script with a little bit of checking on it. --- bin/choose-math-char | 47 ++++++++++++++++++++++++++++++++++++++++++ bin/generate-gitignore | 2 +- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100755 bin/choose-math-char diff --git a/bin/choose-math-char b/bin/choose-math-char new file mode 100755 index 0000000..bc34650 --- /dev/null +++ b/bin/choose-math-char @@ -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 <' and recurse into ''. +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 diff --git a/bin/generate-gitignore b/bin/generate-gitignore index cb51995..d2d7fce 100755 --- a/bin/generate-gitignore +++ b/bin/generate-gitignore @@ -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 ',')