Skip to content

Instantly share code, notes, and snippets.

@mezcel
Last active November 4, 2020 08:27
Show Gist options
  • Save mezcel/508c384d9200933761fa8ecbc4f4698c to your computer and use it in GitHub Desktop.
Save mezcel/508c384d9200933761fa8ecbc4f4698c to your computer and use it in GitHub Desktop.
CACHE_TODO

cache-todo (ctodo)

About

git clone https://gist.github.com/508c384d9200933761fa8ecbc4f4698c.git ~/gist.github/mezcel/cache-todo/.gist

  • Jot down notes in a "to-do" chrono log style fashion.
  • Additional use case:
    • Quickly populate a tmp directory for ssh file transfers.

The focus is on just jotting notes, not editing, or organizing them.*

Install

Run the installer script. install.sh

The installer will write an alias variable in ~/.bashrc, which will launch the ./ctodo.bash application script.

Run

  • A hidden directory, named ~/.cache_todo, will hold markdown notes and its optional .html equivalents.
  • New note files are auto generated based on the day the note is taken.

Terminal UI Example:

## Append a note into the defult daily note file.
ctodo -a "Don't forget about that thing."

Argument Commands

function Commands Example Input Description
append file --append or -a ctodo -a "This is bulleted note string." Append a note to a default text file.
heading divider --heading or -h ctodo -h "Heading for a new sequence of notes." Make a heading divider.
load a file --file or -f ctodo -f ./file-path.txt Copy a file into the ~/.cache_todo folder
heading divider --list or -l ctodo -l View notes in elinks
load a file --delete or -d ctodo -d Delete the ~/.cache_todo folder
## Visual Studio 2019 EditorConfig
indent_style = space
indent_size = 4
## Editor Guidelines
## https://marketplace.visualstudio.com/items?itemName=PaulHarrington.EditorGuidelines
## All files
[*]
# Named color format
guidelines_style = .2px solid Lime
guidelines = 4, 80, 120
## win10 line endins
*.bat eol=crlf
*.ps1 eol=crlf
.editorconfig eol=crlf
## Linux/Posix line endings
*.sh eol=lf
*.bash eol=lf
Makefile eol=lf
## ignore cs2019 & vscode settings
.vscode
.vs
#!/bin/bash
## #####################################################################
## CACHE_TODO ( ctodo )
## #####################################################################
## About:
## Jot down notes in a "to-do" chrono log style fashion.
## Argument flags:
## -a | Append a note to a default text file.
## ctodo -a "This is bulleted note string."
## -h | Heading for a new sequence of notes.
## ctodo -h "Heading for a new sequence of notes."
## -f | Copy a file into the "~/.cache_todo" folder
## ctodo -f <file-path>
## -l | View notes in elinks
## ctodo -l
## -d | Delete the "~/.cache_todo" folder
## ctodo -d
## Git:
## https://gist.github.com/508c384d9200933761fa8ecbc4f4698c.git
## #####################################################################
function Tput_Colors {
## Foreground Color using ANSI escape
FG_BLACK=$(tput setaf 0)
FG_RED=$(tput setaf 1)
FG_GREEN=$(tput setaf 2)
FG_YELLOW=$(tput setaf 3)
FG_BLUE=$(tput setaf 4)
FG_MAGENTA=$(tput setaf 5)
FG_CYAN=$(tput setaf 6)
FG_WHITE=$(tput setaf 7)
FG_NoColor=$(tput sgr0)
## Background Color using ANSI escape
BG_BLACK=$(tput setab 0)
BG_RED=$(tput setab 1)
BG_GREEN=$(tput setab 2)
BG_YELLOW=$(tput setab 3)
BG_BLUE=$(tput setab 4)
BG_MAGENTA=$(tput setab 5)
BG_CYAN=$(tput setab 6)
BG_WHITE=$(tput setab 7)
BG_NoColor=$(tput sgr0)
## set mode using ANSI escape
MODE_BOLD=$(tput bold)
MODE_DIM=$(tput dim)
MODE_BEGIN_UNDERLINE=$(tput smul)
MODE_EXIT_UNDERLINE=$(tput rmul)
MODE_REVERSE=$(tput rev)
MODE_ENTER_STANDOUT=$(tput smso)
MODE_EXIT_STANDOUT=$(tput rmso)
# clear styles using ANSI escape
STYLES_OFF=$(tput sgr0)
FGBG_NoColor=$(tput sgr0)
}
function about {
echo -e "$FG_CYAN \
\n## ##################################################################### \
\n## CACHE_TODO ( ctodo ) \
\n## ##################################################################### \
\n## \
\n## About: \
\n## Jot down notes in a \"to-do\" chrono log style fashion. \
\n## \
\n## Argument flags:\
\n## -a | Append a note to a default text file. \
\n## ctodo -a \"This is bulleted note string.\" \
\n## \
\n## -h | Heading for a new sequence of notes. \
\n## ctodo -h \"Heading for a new sequence of notes.\" \
\n## \
\n## -f | Copy a file into the \"~/.cache_todo\" folder \
\n## ctodo -f <file-path> \
\n## \
\n## -l | View notes in elinks \
\n## ctodo -l \
\n## \
\n## -d | Delete the \"~/.cache_todo\" folder \
\n## ctodo -d \
\n## \
\n## Git: \
\n## https://gist.github.com/508c384d9200933761fa8ecbc4f4698c.git \
\n## \
\n## ##################################################################### \
\n$STYLES_OFF"
}
function initialize_file {
## Create a daily note file
notesDirectory=~/.cache_todo
nowDay=$(date +%d%b%Y)
noteMD=$notesDirectory/ctodo_note_$nowDay.md
noteHTML=$notesDirectory/ctodo_note_$nowDay.html
mkdir -p $notesDirectory
if [ ! -f $noteMD ]; then
echo -e "# ctodo_note_$nowDay\n" > $noteMD
echo -e "|time|note|" >> $noteMD
echo -e "|:---|:---|" >> $noteMD
echo -e "$noteMD created."
fi
}
function append_file {
if [ -f $noteMD ]; then
nowTime=$(date +%H%M)
echo -e "|<span style=\"color:blue;\">$nowTime</span>|<span style=\"color:green;\">$note</span>|" >> $noteMD
echo -e "$FG_GREEN\tString:\t \"$note\"\n\tNote File: $noteMD\n$STYLES_OFF"
## Generate an optional html file equivalent
#isPandoc=$(command -v pandoc)
#if [ $isPandoc == "/usr/bin/pandoc" ]; then
command -v pandoc &>/dev/null
if [ $? -eq 0 ]; then
pandoc $noteMD > $noteHTML
echo -e "$FG_YELLOW\tGenerated $noteHTML using pandoc.\n$STYLES_OFF"
fi
fi
}
function section_heading {
echo -e "\n## $note \n" >> $noteMD
echo -e "|time|note|" >> $noteMD
echo -e "|:---|:---|" >> $noteMD
echo -e "$FG_GREEN\tHeading:\t \"$note\"\n\tNote File: $noteMD.\n$STYLES_OFF"
}
function clear_vars {
unset notesDirectory
unset note
unset noteMD
unset nowDay
unset nowTime
}
## #############################################################################
## Run
## #############################################################################
Tput_Colors
initialize_file
while [ ! -z "$1" ]; do
case "$1" in
--append|-a )
shift
note=$1
if [ ! -z "$note" ]; then
echo -e "$FG_MAGENTA\nAppend file: $noteMD $STYLES_OFF"
append_file
fi
;;
--heading|-h )
shift
note=$1
if [ ! -z "$note" ]; then
echo -e "$FG_MAGENTA\nAppend file: $noteMD $STYLES_OFF"
section_heading
fi
;;
--file|-f )
shift
note=$1
note=$(readlink -f $note)
if [ ! -z "$note" ]; then
## Copy file
cp $note $notesDirectory
if [ -f $note ]; then
echo -e "$FG_MAGENTA\nAppend file: $noteMD $STYLES_OFF"
## full file path
note=$(readlink -f $note)
note="Resource: \"$note\" was copied into \"$notesDirectory\""
append_file
echo -e "${FG_MAGENTA}$note\n$STYLES_OFF"
else
echo -e "$FG_RED\nResource: \"$note\" was NOT copied into \"$notesDirectory\" $STYLES_OFF"
fi
fi
;;
--list|-l )
shift
## Browse to lists in elinks
isElinks=$(command -v elinks)
#if [ $isElinks == "/usr/bin/elinks" ]; then
#command -v elinks &>/dev/null
if [ $? -eq 0 ]; then
elinks $notesDirectory
fi
;;
--delete|-d )
shift
## Delete notesDirectory=~/.cache_todo
echo "${BG_RED}$FG_WHITE"
read -e -p "Delete the $notesDirectory directory? [ y/N ]: " -i "n" yn
echo "$STYLES_OFF"
case $yn in
[Yy]* )
rm -rf $notesDirectory
echo -e "${FG_YELLOW}$notesDirectory deleted.$STYLES_OFF"
;;
esac
;;
* )
echo -e "${FG_RED}\nError:\n\tInvalid argument input.$STYLES_OFF"
about
exit
;;
esac
shift
done
clear_vars
#!/bin/bash
## Decorative tty colors
function Tput_Colors {
## Foreground Color using ANSI escape
FG_BLACK=$(tput setaf 0)
FG_RED=$(tput setaf 1)
FG_GREEN=$(tput setaf 2)
FG_YELLOW=$(tput setaf 3)
FG_BLUE=$(tput setaf 4)
FG_MAGENTA=$(tput setaf 5)
FG_CYAN=$(tput setaf 6)
FG_WHITE=$(tput setaf 7)
FG_NoColor=$(tput sgr0)
## Background Color using ANSI escape
BG_BLACK=$(tput setab 0)
BG_RED=$(tput setab 1)
BG_GREEN=$(tput setab 2)
BG_YELLOW=$(tput setab 3)
BG_BLUE=$(tput setab 4)
BG_MAGENTA=$(tput setab 5)
BG_CYAN=$(tput setab 6)
BG_WHITE=$(tput setab 7)
BG_NoColor=$(tput sgr0)
## set mode using ANSI escape
MODE_BOLD=$(tput bold)
MODE_DIM=$(tput dim)
MODE_BEGIN_UNDERLINE=$(tput smul)
MODE_EXIT_UNDERLINE=$(tput rmul)
MODE_REVERSE=$(tput rev)
MODE_ENTER_STANDOUT=$(tput smso)
MODE_EXIT_STANDOUT=$(tput rmso)
# clear styles using ANSI escape
STYLES_OFF=$(tput sgr0)
FGBG_NoColor=$(tput sgr0)
}
## make an alias variable in ~/.bashrc to launch keyboard-layout.sh
function make_bashrc_alias {
aliasVar=$1
autocompleteString=$2
bashFile=$3
bashFilePath=$(pwd)/$bashFile
echo -e "$FG_CYAN \
\n## ################################################################## \
\n## Making an alias variable in ~/.bashrc to launch cache-todo.bash \
\n## the alias is \"ctodo\" \
\n## ################################################################## \
\n $STYLES_OFF"
## make bash executable
chmod +x $bashFilePath
origianlBashrc=~/.bashrc
backupBashrc=$origianlBashrc.backup_$(date +%d%b%Y%H%S)
tempBashrc=$origianlBashrc.temp
## make a safety backup of ~/.bashrc
cp $origianlBashrc $backupBashrc
cp $origianlBashrc $tempBashrc
## Make a temporary .bashrc file to edit
## Delete previous line reference to bash file
sed -i "/$bashFile/d" $tempBashrc
sleep 1s
## Delete previous line reference to bash file autocomplete
sed -i "/$autocompleteString/d" $tempBashrc
sleep 1s
aliasautoString="\
\n## Alias for $bashFilePath \
\nalias $aliasVar=\"bash $bashFilePath\" \
\n## $aliasVar alias autocomplete \
\ncomplete -W \"$autocompleteString\" $aliasVar \
\n"
## Append ~/.bashrc with alias and it's auto complete argv
echo -e "$aliasautoString" >> $tempBashrc
## Make the temp file the ~/.bashrc file
sudo mv $tempBashrc $origianlBashrc
sleep 1
## apply the new ~/.bashrc
source $origianlBashrc
echo -e "$FG_YELLOWDone.\n $STYLES_OFF"
}
## RUN
aliasVar=ctodo
autocompleteString="-a -h -d -l"
bashFile=ctodo.bash
make_bashrc_alias $aliasVar "$autocompleteString" $bashFile
@mezcel
Copy link
Author

mezcel commented Oct 19, 2020

init

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment