commit 131cf718928fcc3dee455a6418d025fa62f975fa
parent b9ccd3e4eedc075d7484421667606a274186a4d7
Author: Ed van Bruggen <edvb@uw.edu>
Date: Sun, 18 Mar 2018 00:15:18 -0700
Replace tg and checkium projects with scripts
Diffstat:
4 files changed, 94 insertions(+), 90 deletions(-)
diff --git a/_data/projects.yml b/_data/projects.yml
@@ -21,18 +21,18 @@ main:
desc: 'add an element of nyan cat to vim'
- name: 'tim'
desc: 'extendable personal assistant'
- - name: 'tg'
- desc: 'quickly tag files'
- - name: 'checkium'
- desc: 'functions to display shell error code'
- header: 'scripts'
- name: 'ed'
desc: 'commands revolving around $EDITOR'
- name: 'sett'
desc: 'set title of a terminal'
+ - name: 'tg'
+ desc: 'quickly tag files'
+ - name: 'echk'
+ desc: 'functions to display shell error code'
+ - name: 'g'
+ desc: 'tiny git wrapper'
- name: 'calc'
desc: 'preform fast calculations on the go'
- name: '$'
desc: 'blindly copy commands from the internet'
- - name: 'g'
- desc: 'tiny git wrapper'
diff --git a/projects/checkium.md b/projects/checkium.md
@@ -1,22 +0,0 @@
----
-title: checkium
-description: 'funtions to display shell error code'
-tags: bash
----
-
-Checkium is a small collection of functions used to display the error code of
-the previous command in lots of different visual ways for use in your shell's
-prompt.
-
-## SEE ALSO
-
-View source code at: <https://gitlab.com/edvb/checkium>
-
-## AUTHOR
-
-Ed van Bruggen <edvb54@gmail.com>
-
-## LICENCE
-
-zlib License
-
diff --git a/projects/echk.md b/projects/echk.md
@@ -0,0 +1,59 @@
+---
+title: echk
+description: functions to display shell error code
+---
+
+```bash
+#!/usr/bin/env bash
+
+# echk: functions to display shell error code
+
+echk_color() {
+ # set color depending on if the last command succeed or not
+ if [[ $? -eq 0 ]]; then
+ echo -ne "[0;32m"
+ return 0
+ else
+ echo -ne "[0;31m"
+ # need to return a value so that the other functions here will
+ # know if last command ran OK or not, the previous code will return 0
+ return 1 # TODO $?
+ fi
+}
+
+echk_color_simple() {
+ if [[ $? -eq 0 ]]; then
+ return 0
+ else
+ echo -ne "[0;31m"
+ return 1
+ fi
+}
+
+echk_char() {
+ if [[ $? -eq 0 ]]; then
+ echo "$1"
+ else
+ echo "$2"
+ fi
+}
+
+echk_check() {
+ echk_char "✓" "✗ "
+}
+
+echk_face() {
+ echk_char ":)" ":("
+}
+
+echk_random_face() {
+ # set arrays for different faces to randomly choose from
+ if [[ $? -eq 0 ]]; then
+ local faces=(":)" ":D")
+ else
+ local faces=(":P" ":(" ":/" ":O" ":\" ":|" ":(" ":(")
+ fi
+
+ echo ${faces[$((RANDOM % ${#faces[*]}))]} # randomly choose an array element
+}
+```
diff --git a/projects/tg.md b/projects/tg.md
@@ -1,66 +1,33 @@
---
title: tg
-description: 'quickly tag files'
-tags: bash script cli linux
+description: quickly tag files
---
-A small bash script to rename files based on *TAG*. For example `tg old
-file.txt` changes `file.txt` to `file-old.txt`. If the file already contains
-the given *TAG* then the tag is removed from *FILE*.
-
-## OPTIONS
-
-**-e** *EXTD*
- Change the extension delimiter to *EXTD* default is `.`
-
-**-t** *TAGD*
- Change the tag delimiter to *TAGD* default is `-`
-
-**-v**
- Print version info and exit
-
-## USAGE
-
-To tag `file.txt` with the tag `old` run:
-
- $ tg old file.txt
-
-`file.txt` is now called `file-old.txt`, to change it back simply run the same
-tag with the new file name:
-
- $ tg old file-old.txt
-
-Now it is back to `file.txt`.
-
-If you use the same tag often you can always just add an alias to your shell's
-rc file such as:
-
- alias old='tg old'
-
-Now running `old file.txt` is the same as running `tg old file.txt`.
-
-You can change the default delimiter that `tg` uses to separate tags with the
-`-t` options, for example:
-
- $ tg -t _ LINUX README.txt
-
-Changes `README.txt` to `README_LINUX.txt`.
-
-This also applies for extensions using `-e`
-
- $ tg -e _ empty file_txt
-
-`file_txt` becomes `file-empty_txt`
-
-## SEE ALSO
-
-View source code at: <https://gitlab.com/edvb/tg>
-
-## AUTHOR
-
-Ed van Bruggen <edvb54@gmail.com>
-
-## LICENCE
-
-zlib License
-
+```bash
+#!/usr/bin/env bash
+
+# tg: quickly tag files
+#
+# Rename files based on the given tag. For example `tg old file.txt` changes
+# `file.txt` to `file-old.txt`. If the file already contains the suppied tag,
+# it is removed.
+
+if [[ $# != 2 ]]; then
+ printf "usage: tg TAG FILE
+"
+ exit 1
+fi
+
+if [ -z "${2##*.*}" ]; then
+ ext=".${2##*.}"
+else
+ ext=""
+fi
+file="${2%.*}"
+
+if [[ $(echo "$file" | rev | cut -d '-' -f 1 | rev) != "$1" ]]; then
+ mv "$2" "${file}-$1$ext"
+else
+ mv "$2" "$(echo "$file" | rev | cut -d '-' -f 2- | rev)$ext"
+fi
+```