Skip to content

Instantly share code, notes, and snippets.

@doekman
Last active June 7, 2020 11:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doekman/d6743e95bfb5f3d9491d3ec7b4a6e607 to your computer and use it in GitHub Desktop.
Save doekman/d6743e95bfb5f3d9491d3ec7b4a6e607 to your computer and use it in GitHub Desktop.
Shows the `awesomecsv` list by using `ok-bash` in the terminal (and navigate to these links too)
#!/usr/bin/env bash
# Unofficial Bash Strict Mode
set -euo pipefail
IFS=$'\n\t'
# import ok
if [[ -r "$_OK__PATH_TO_ME/ok.sh" ]]; then
. "$_OK__PATH_TO_ME/ok.sh" prompt '=> ' prompt_default
fi
function to_ok_format {
python -c "import codecs, csv, sys
def ansi_format(start, text, end): return '\x1B[{}m{}\x1B[{}m'.format(start, text, end)
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader( [line.encode('utf-8') for line in unicode_csv_data],
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
sys.stdin = codecs.getreader('utf_8')(sys.stdin)
sys.stdout = codecs.getwriter('utf_8')(sys.stdout)
sys.stderr = codecs.getwriter('utf_8')(sys.stderr)
csv_reader = unicode_csv_reader(sys.stdin)
last_header = ''
for row in csv_reader:
if last_header != row[0]:
last_header = row[0]
print '# %s' % row[0]
# opens URL from command line; see for other OS's: https://dwheeler.com/essays/open-files-urls.html
print 'open %s # %s: %s' % (row[2], ansi_format('4', row[1], '24'), row[3])
"
}
# Fetch CSV, remove heading (1st line), prepend awesome URL's to the list, rewrite it to .ok-format, and stream it to OK
FIRST_LINES=$',AwesomeCSV,https://github.com/secretGeek/awesomecsv,🕶️ A curated list of awesome tools for dealing with awesome CSV (here are links below stored)
,a-w-e-s-o-m-e,https://awesome.re/,😎 Curated list of awesome lists
'
curl -s -f https://raw.githubusercontent.com/secretGeek/AwesomeCSV/master/awesomecsv.csv | sed '1d' | cat <(printf "$FIRST_LINES") - | to_ok_format | ok -f - "$@"
#naive implementation (doesn't work well with quoted strings including comma's)
#curl -s https://raw.githubusercontent.com/secretGeek/AwesomeCSV/master/awesomecsv.csv | sed -e 1d | awk -F ',' '{if(last_cat!=$1) { print "# " $1; last_cat=$1;} print "open " $3 " # " $2;}' | ok -f - "$@"
# Requires: https://github.com/secretGeek/ok-bash
# Gist: https://gist.github.com/doekman/d6743e95bfb5f3d9491d3ec7b4a6e607
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment