From 71e6bff0388cec0f684f37d338baa42588a95c10 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 11:15:36 -0800 Subject: [PATCH 01/48] Move scripts to separate folder --- juliandate | 19 +++++++++++++++++++ stardate | 6 ++++++ stardate.pl | 14 ++++++++++++++ 3 files changed, 39 insertions(+) create mode 100755 juliandate create mode 100755 stardate create mode 100755 stardate.pl diff --git a/juliandate b/juliandate new file mode 100755 index 0000000..6e23a9e --- /dev/null +++ b/juliandate @@ -0,0 +1,19 @@ +#!/usr/bin/env python +""" Calculate the Julian Date """ +# http://www.cs.utsa.edu/~cs1063/projects/Spring2011/Project1/jdn-explanation.html + +import time + +t = time.gmtime() + +a = (14 - t.tm_mon) / 12 +y = t.tm_year + 4800 - a +m = t.tm_mon + 12 * a - 3 + +jdn = t.tm_mday + (153 * m + 2) / 5 + 365 * y +\ + y / 4 - y / 100 + y / 400 - 32045 + +jd = jdn + (t.tm_hour - 12) / 24.0 + t.tm_min / 1440.0 + t.tm_sec / 86400 + +print "%05.1f" % jd + diff --git a/stardate b/stardate new file mode 100755 index 0000000..1bb4b6c --- /dev/null +++ b/stardate @@ -0,0 +1,6 @@ +#!/usr/bin/env python + +import time + +print "%05.1f" % (time.time() / 31557.6) + diff --git a/stardate.pl b/stardate.pl new file mode 100755 index 0000000..4a992a9 --- /dev/null +++ b/stardate.pl @@ -0,0 +1,14 @@ +#!/usr/bin/perl + +my $trek_year; +my $sd; + +($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = + gmtime(time); + +$trek_year = ($year - 70) * 1000; + +$sd = ((($yday)*1000/365.25 + $trek_year)); + +printf "%.1f\n", $sd; + From e376d0eca1cd181b3613f35acb6115428f33d599 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 11:15:51 -0800 Subject: [PATCH 02/48] Add Julian date script based on Unix time --- jdate | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100755 jdate diff --git a/jdate b/jdate new file mode 100755 index 0000000..1988a07 --- /dev/null +++ b/jdate @@ -0,0 +1,14 @@ +#!/usr/bin/env python +""" Calculate the Julian Date """ + +import time + +t = time.time() + +""" Technically, we should be adding 2440587.5, + however, since we are trying to stick to the stardate + concept, we add only 40587.5""" +jd = t / 86400.0 + 40587.5 + +print "%05.1f" % jd + From cb88dd975e2230b941de5d2bf3d883ba7ce7b976 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 12:00:38 -0800 Subject: [PATCH 03/48] Add stardate script based on Julian date --- sdate | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100755 sdate diff --git a/sdate b/sdate new file mode 100755 index 0000000..7426811 --- /dev/null +++ b/sdate @@ -0,0 +1,15 @@ +#!/usr/bin/env python +""" Calculate the Julian Date """ + +import time + +t = time.time() + +""" Technically, we should be adding 2440587.5, + however, since we are trying to stick to the stardate + concept, we add only 440587.5""" +jd = t / 86400.0 + 440587.5 + +# Use the idea that 10 Julian days is equal to 1 stardate +print "%05.1f" % (jd / 10) + From 27c7871dfd4e44f0517391bac517044cf9ffbc83 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 13:54:57 -0800 Subject: [PATCH 04/48] Add script to set Xterm/Tmux pane title --- settitle | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100755 settitle diff --git a/settitle b/settitle new file mode 100755 index 0000000..8cbaa10 --- /dev/null +++ b/settitle @@ -0,0 +1,10 @@ +#!/bin/bash +# Set the Xterm/Tmux pane title + +if [[ -z $TMUX ]] +then + echo -e "\033]0;$*\007" +else + echo -e "\033k$*\033\\" +fi + From 1f76e1311235028b4c58ccae9db95ac2ea43cdfc Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 16:51:46 -0800 Subject: [PATCH 05/48] Tweak sdate script to print correct Julian date The existing script was rounding the computed Julian date which was resulting in the script printing the next day for 12 hours in the day, i.e., a date of 456314.5 was being printed as 45631.5 (due to the division by 10 and rounding up by the print statement). Now, the script computes the number of days since the epoch (JD 2000000) and drops the fractional portion, so it will print the correct Julian date stardate, i.e., a date of 456314.5 will be printed correctly as 45631.4. --- sdate | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/sdate b/sdate index 7426811..303d0ef 100755 --- a/sdate +++ b/sdate @@ -2,14 +2,15 @@ """ Calculate the Julian Date """ import time +import math t = time.time() """ Technically, we should be adding 2440587.5, however, since we are trying to stick to the stardate concept, we add only 440587.5""" -jd = t / 86400.0 + 440587.5 +jd = math.floor(t / 86400.0 + 440587.5) # Use the idea that 10 Julian days is equal to 1 stardate -print "%05.1f" % (jd / 10) +print "%05.1f" % (jd / 10.0) From 6560b0050d7f309f6cd44a2316b482f7652b5a2a Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 17:36:07 -0800 Subject: [PATCH 06/48] Delete obsolete scripts --- jdate | 14 -------------- juliandate | 19 ------------------- stardate | 6 ------ stardate.pl | 14 -------------- 4 files changed, 53 deletions(-) delete mode 100755 jdate delete mode 100755 juliandate delete mode 100755 stardate delete mode 100755 stardate.pl diff --git a/jdate b/jdate deleted file mode 100755 index 1988a07..0000000 --- a/jdate +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env python -""" Calculate the Julian Date """ - -import time - -t = time.time() - -""" Technically, we should be adding 2440587.5, - however, since we are trying to stick to the stardate - concept, we add only 40587.5""" -jd = t / 86400.0 + 40587.5 - -print "%05.1f" % jd - diff --git a/juliandate b/juliandate deleted file mode 100755 index 6e23a9e..0000000 --- a/juliandate +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env python -""" Calculate the Julian Date """ -# http://www.cs.utsa.edu/~cs1063/projects/Spring2011/Project1/jdn-explanation.html - -import time - -t = time.gmtime() - -a = (14 - t.tm_mon) / 12 -y = t.tm_year + 4800 - a -m = t.tm_mon + 12 * a - 3 - -jdn = t.tm_mday + (153 * m + 2) / 5 + 365 * y +\ - y / 4 - y / 100 + y / 400 - 32045 - -jd = jdn + (t.tm_hour - 12) / 24.0 + t.tm_min / 1440.0 + t.tm_sec / 86400 - -print "%05.1f" % jd - diff --git a/stardate b/stardate deleted file mode 100755 index 1bb4b6c..0000000 --- a/stardate +++ /dev/null @@ -1,6 +0,0 @@ -#!/usr/bin/env python - -import time - -print "%05.1f" % (time.time() / 31557.6) - diff --git a/stardate.pl b/stardate.pl deleted file mode 100755 index 4a992a9..0000000 --- a/stardate.pl +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/perl - -my $trek_year; -my $sd; - -($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = - gmtime(time); - -$trek_year = ($year - 70) * 1000; - -$sd = ((($yday)*1000/365.25 + $trek_year)); - -printf "%.1f\n", $sd; - From 26e51adc9ff4138a7af13f8e16c840fdb13b9253 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 21 Jan 2013 22:09:50 -0800 Subject: [PATCH 07/48] Add script to test dircolors file This Perl script reads a valid dircolors configuration file and prints the colorscheme used by 'ls --color' for each file type (and extension). Error checking is non-existent. The script assumes that your dircolors file is valid input for the dircolors program. --- dircolortest | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 dircolortest diff --git a/dircolortest b/dircolortest new file mode 100755 index 0000000..f5a2db1 --- /dev/null +++ b/dircolortest @@ -0,0 +1,55 @@ +#!/usr/bin/perl +# A utility script to test out dircolors settings without reloading +# Usage: dircolortest + +if ($#ARGV < 0) { + die "Usage: $0 \n"; +} + +if ($#ARGV > 0) { + warn "Ignoring additional command line arguments\n"; +} + +# Open the file and get the handle +open DCFILE, $ARGV[0] or + die "Cannot open dircolors file $ARGV[0]! $!\n"; + +$line_counter = 0; + +while ($line = ) { + chomp $line; + + # Strip off any comments + $line =~ s/#.*$//; + + # Strip off leading and trailing whitespace + $line =~ s/^\s*//; + $line =~ s/\s*$//; + + if ($line ne '') { + ($type, $format) = split /\s+/, $line; + + # Ignore the TERM lines, we don't care about them here + next if ($type eq 'TERM'); + + # Just a little enhancement, if the type begins with a . + if ($type =~ m/^\./) { + $type = "*$type"; + } + + print "\033[${format}m$type\033[m"; + + $line_counter = $line_counter + 1; + + if ($line_counter == 8) { + print "\n"; + $line_counter = 0; + } else { + print "\t"; + } + } +} + +print "\n" if ($line_counter != 0); + +close DCFILE; From 92c27287b8964b5587ed3dacd455686e17e6e1f3 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 22 Jan 2013 11:22:22 -0800 Subject: [PATCH 08/48] Ignore COLOR, OPTIONS and EIGHTBIT keywords --- dircolortest | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/dircolortest b/dircolortest index f5a2db1..d79d412 100755 --- a/dircolortest +++ b/dircolortest @@ -29,8 +29,9 @@ while ($line = ) { if ($line ne '') { ($type, $format) = split /\s+/, $line; - # Ignore the TERM lines, we don't care about them here - next if ($type eq 'TERM'); + # Ignore the following lines, we don't care about them here + next if (($type eq 'TERM') || ($type eq 'COLOR') || + ($type eq 'OPTIONS') || ($type eq 'EIGHTBIT')); # Just a little enhancement, if the type begins with a . if ($type =~ m/^\./) { From 91ca8e118f9cf633705f484e5174cf432db18255 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 22 Jan 2013 16:58:43 -0800 Subject: [PATCH 09/48] Speed up stardate counter (1 SD = 1 JD) --- sdate | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdate b/sdate index 303d0ef..0ee3c69 100755 --- a/sdate +++ b/sdate @@ -8,9 +8,9 @@ t = time.time() """ Technically, we should be adding 2440587.5, however, since we are trying to stick to the stardate - concept, we add only 440587.5""" -jd = math.floor(t / 86400.0 + 440587.5) + concept, we add only 40587.5""" +jd = (t / 86400.0 + 40587.5) # Use the idea that 10 Julian days is equal to 1 stardate -print "%05.1f" % (jd / 10.0) +print "%05.1f" % jd From a5321078e4e2638c6d4c17b3d897f6d70289f21a Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 23 Jan 2013 20:58:18 -0800 Subject: [PATCH 10/48] Add note script --- note | 209 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 209 insertions(+) create mode 100755 note diff --git a/note b/note new file mode 100755 index 0000000..060e866 --- /dev/null +++ b/note @@ -0,0 +1,209 @@ +#!/bin/sh +# Note taking script + +NOTES_DIR="$HOME/.notes" + +# Initialize with the default editor +[ -z "$EDITOR" ] && EDITOR="/usr/bin/vim" + +notes_init() { + # Check if the notes folder exists, if not, create it + if [ ! -d $NOTES_DIR ] + then + echo -n "Creating notes folder..." + mkdir -p $NOTES_DIR + cd $NOTES_DIR + echo "done." + + git init . + else + [ ! -z $1 ] && echo "Notes has already been initialized." + fi +} + +make_title() { + echo "$*" | sed 's/[^A-Za-z0-9_-]\+/-/g' +} + +title_to_name() { + note_file=$NOTES_DIR/$(make_title $note_arg) +} + +check_arg() { + if [ -z "$note_arg" ] + then + echo "Must specify a note title!" + exit + fi +} + +note_exists() { + [[ -f "$note_file" ]] +} + +check_md5() { + if [[ "$note_old_md5" == "$note_new_md5" ]] + then + echo "No changes to '$note_arg'" + + if [[ ! -z "$1" ]] + then + rm -f $1 + fi + + exit + fi +} + +check_dir() { + if [[ ! -d "$NOTES_DIR" ]] + then + echo "Notes not initialized. Run init first!" + exit + fi +} + +help_cmd() { + echo ' +Note taker for the Command Line +=============================== + +This is a bash script that lets you take notes which are maintained in a +Git repository. You can run this script by typing the following command +at the command line: + +Usage: note + +Commands +-------- + + Command Usage + new Follow this with the note name to create a new note + edit Follow this with the note name to edit an existing + note + delete Follow this with the note name to delete a note + list Prints the list of available notes + hist Displays the note history + init Run this the very first time to set up the folders + help Prints this help message +' +} + +parse_args() { + note_cmd=$1 + shift + note_arg="$*" + + case "$note_cmd" in + "help") + help_cmd + ;; + + "new") + check_dir + check_arg + title_to_name + + if note_exists + then + echo "Note '$note_arg' already exists! Use edit instead." + exit + fi + + echo "$note_arg" > $note_file + note_old_md5=$(md5sum $note_file) + + $EDITOR $note_file + + note_new_title=$(head -1 $note_file) + note_new_md5=$(md5sum $note_file) + note_new_file=$(make_title $note_new_title) + check_md5 $note_file + + cd $NOTES_DIR + gitfile=$(basename $note_new_file) + + git add $gitfile + git commit -m "Create note '$note_new_title'" + echo "Created note '$note_new_title'" + + ;; + + "edit") + check_dir + check_arg + title_to_name + if ! note_exists + then + echo "Cannot find note '$note_arg'!" + exit + fi + + note_old_title=$(head -1 $note_file) + note_old_md5=$(md5sum $note_file) + + $EDITOR $note_file + + note_new_title=$(head -1 $note_file) + note_new_md5=$(md5sum $note_file) + + check_md5 + + cd $NOTES_DIR + gitfile=$(basename $note_file) + commit_msg="Update note '$note_new_title'" + if [[ "$note_old_title" != "$note_new_title" ]] + then + note_new_file=$(make_title $note_new_title) + + git mv $gitfile $note_new_file + git add $note_new_file + commit_msg="$commit_msg. Rename from '$note_old_title'" + else + git add $gitfile + fi + + git commit -m "$commit_msg" + echo "Updated note '$note_new_title'" + ;; + + "del" | "delete") + check_dir + check_arg + title_to_name + if ! note_exists + then + echo "Cannot find note '$note_arg'!" + exit + fi + + cd $NOTES_DIR + gitfile=$(basename $note_file) + git rm $gitfile + git commit -m "Delete note '$note_arg'" + echo "Deleted note '$note_arg'" + ;; + + "hist"|"history") + check_dir + cd $NOTES_DIR + git log --pretty=format:'%ai - %s' -- $note_arg + ;; + + "init") + notes_init warn + ;; + + "list") + check_dir + cd $NOTES_DIR + ls -1 $note_arg + ;; + + *) + echo "Unrecognized command '$note_cmd'. Use help." + ;; + esac +} + +parse_args $* From e2f3a1a7bd03a8de46bbd599ee0ffcee4ff2d2e6 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 23 Jan 2013 22:20:06 -0800 Subject: [PATCH 11/48] Fix note issues Fix the following issues in note 1. Always use a combo of lowercase letters, hyphen, underscore and numbers for the file name. This is taken from the title and uses sed to translate the uppercase letters to lowercase. Unfortunately, sed's translation mode doesn't support ranges, so we must use the full alphabet. 2. Add support for displaying version information. 3. Reformat the help display so it all fits in a single line. 4. Add checking to ensure that at least one argument is passed to note so that the parse_args routine doesn't barf an error with shift. 5. Remove argument checking in notes_init. It's called only by the init function, so there's no need to add a flag to control warnings. --- note | 51 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/note b/note index 060e866..8600851 100755 --- a/note +++ b/note @@ -1,6 +1,7 @@ -#!/bin/sh -# Note taking script +#!/bin/bash +# Notes manager for the command line +NOTES_VER="0.1a" NOTES_DIR="$HOME/.notes" # Initialize with the default editor @@ -17,12 +18,13 @@ notes_init() { git init . else - [ ! -z $1 ] && echo "Notes has already been initialized." + echo "Notes has already been initialized." fi } make_title() { - echo "$*" | sed 's/[^A-Za-z0-9_-]\+/-/g' + echo "$*" | sed 's/[^A-Za-z0-9_-]\+/-/g' | \ + sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' } title_to_name() { @@ -65,8 +67,8 @@ check_dir() { help_cmd() { echo ' -Note taker for the Command Line -=============================== +Notes Manager for the Command Line +================================== This is a bash script that lets you take notes which are maintained in a Git repository. You can run this script by typing the following command @@ -77,18 +79,23 @@ Usage: note Commands -------- - Command Usage - new Follow this with the note name to create a new note - edit Follow this with the note name to edit an existing - note - delete Follow this with the note name to delete a note - list Prints the list of available notes - hist Displays the note history - init Run this the very first time to set up the folders - help Prints this help message + Command Usage + ------- ----- + new Follow this with the note name to create a new note + edit Follow this with the note name to edit an existing note + delete Follow this with the note name to delete a note + list Prints the list of available notes + history Displays the note history + init Run this the very first time to set up the folders + help Prints this help message + version Prints the version information ' } +version_info() { + echo "Notes Manager version $NOTES_VER" +} + parse_args() { note_cmd=$1 shift @@ -191,7 +198,7 @@ parse_args() { ;; "init") - notes_init warn + notes_init ;; "list") @@ -200,10 +207,20 @@ parse_args() { ls -1 $note_arg ;; + "ver" | "version") + version_info + ;; + *) echo "Unrecognized command '$note_cmd'. Use help." ;; esac } -parse_args $* +if [[ $# -lt 1 ]] +then + echo "Usage: note " + echo "Type 'note help' for detailed help" +else + parse_args $* +fi From 17e9f45505e994055c9f3a58fb5f9ba89b125459 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 24 Jan 2013 19:27:39 -0800 Subject: [PATCH 12/48] Fix title renaming for a new note --- note | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/note b/note index 8600851..fadc428 100755 --- a/note +++ b/note @@ -127,6 +127,12 @@ parse_args() { note_new_file=$(make_title $note_new_title) check_md5 $note_file + # Check for a title rename + if [[ "$note_file" != "$note_new_file" ]] + then + mv -f $note_file $note_new_file + fi + cd $NOTES_DIR gitfile=$(basename $note_new_file) From 0ecbe0605dd38facba41a07431a4f5afbd8f985c Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 30 Jan 2013 18:04:21 -0800 Subject: [PATCH 13/48] Fix note script for title renaming Also add a debug print statement for when NOTES_DEBUG is set. --- note | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/note b/note index fadc428..1dc97f4 100755 --- a/note +++ b/note @@ -7,6 +7,12 @@ NOTES_DIR="$HOME/.notes" # Initialize with the default editor [ -z "$EDITOR" ] && EDITOR="/usr/bin/vim" +dprint() { + if [ ! -z $NOTES_DEBUG ]; then + echo $* + fi +} + notes_init() { # Check if the notes folder exists, if not, create it if [ ! -d $NOTES_DIR ] @@ -23,7 +29,7 @@ notes_init() { } make_title() { - echo "$*" | sed 's/[^A-Za-z0-9_-]\+/-/g' | \ + echo "$*" | sed 's/[^A-Za-z0-9_]\+/-/g' | \ sed 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' } @@ -124,9 +130,12 @@ parse_args() { note_new_title=$(head -1 $note_file) note_new_md5=$(md5sum $note_file) - note_new_file=$(make_title $note_new_title) + note_new_file=$NOTES_DIR/$(make_title $note_new_title) check_md5 $note_file + dprint "Original filename = " $note_file + dprint "New filename = " $note_new_file + # Check for a title rename if [[ "$note_file" != "$note_new_file" ]] then From 8d0349f999617763cff0ad6d8b9b40be7135378d Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 30 Jan 2013 19:25:32 -0800 Subject: [PATCH 14/48] Add search functionality to note Also tweak new and edit commands to delete/restore note if the file is empty. --- note | 248 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 156 insertions(+), 92 deletions(-) diff --git a/note b/note index 1dc97f4..b3132b8 100755 --- a/note +++ b/note @@ -63,6 +63,22 @@ check_md5() { fi } +check_empty() { + if [[ ! -s "$note_file" ]] + then + if [[ "$1" == "restore" ]] + then + gitfile=$(basename "$note_file") + git checkout $gitfile + elif [[ "$1" == "delete" ]] + then + rm -f $note_file + fi + + exit + fi +} + check_dir() { if [[ ! -d "$NOTES_DIR" ]] then @@ -92,6 +108,8 @@ Commands delete Follow this with the note name to delete a note list Prints the list of available notes history Displays the note history + find Finds all notes with the specified keyword in the filename + search Finds all notes with the specified keyword in the text init Run this the very first time to set up the folders help Prints this help message version Prints the version information @@ -102,6 +120,134 @@ version_info() { echo "Notes Manager version $NOTES_VER" } +note_new() { + check_dir + check_arg + title_to_name + + if note_exists + then + echo "Note '$note_arg' already exists! Use edit instead." + exit + fi + + echo "$note_arg" > $note_file + note_old_md5=$(md5sum $note_file) + + $EDITOR $note_file + + note_new_title=$(head -1 $note_file) + note_new_md5=$(md5sum $note_file) + note_new_file=$NOTES_DIR/$(make_title $note_new_title) + check_md5 $note_file + check_empty delete + + dprint "Original filename = " $note_file + dprint "New filename = " $note_new_file + + # Check for a title rename + if [[ "$note_file" != "$note_new_file" ]] + then + mv -f $note_file $note_new_file + fi + + cd $NOTES_DIR + gitfile=$(basename $note_new_file) + + git add $gitfile + git commit -m "Create note '$note_new_title'" + echo "Created note '$note_new_title'" +} + +note_edit() { + check_dir + check_arg + title_to_name + if ! note_exists + then + echo "Cannot find note '$note_arg'!" + exit + fi + + note_old_title=$(head -1 $note_file) + note_old_md5=$(md5sum $note_file) + + $EDITOR $note_file + + note_new_title=$(head -1 $note_file) + note_new_md5=$(md5sum $note_file) + + check_md5 + check_empty restore + + cd $NOTES_DIR + gitfile=$(basename $note_file) + commit_msg="Update note '$note_new_title'" + if [[ "$note_old_title" != "$note_new_title" ]] + then + note_new_file=$(make_title $note_new_title) + + git mv $gitfile $note_new_file + git add $note_new_file + commit_msg="$commit_msg. Rename from '$note_old_title'" + else + git add $gitfile + fi + + git commit -m "$commit_msg" + echo "Updated note '$note_new_title'" +} + +note_delete() { + check_dir + check_arg + title_to_name + if ! note_exists + then + echo "Cannot find note '$note_arg'!" + exit + fi + + cd $NOTES_DIR + gitfile=$(basename $note_file) + git rm $gitfile + git commit -m "Delete note '$note_arg'" + echo "Deleted note '$note_arg'" +} + +note_history() { + check_dir + cd $NOTES_DIR + git log --pretty=format:'%ai - %s' -- $note_arg +} + +note_list() { + check_dir + cd $NOTES_DIR + if [[ -z "$1" ]] + then + ls -1 + else + ls -1 | grep -i $1 + fi +} + +note_search() { + check_dir + + if [[ -z "$1" ]]; then + echo "Must specify a pattern to search titles!" + exit + fi + + cd $NOTES_DIR + for file in * + do + grep --color=always -il "\<$1\>" $file + grep --color=always -inhT -m2 -C1 "\<$1\>" $file + done +} + parse_args() { note_cmd=$1 shift @@ -113,115 +259,33 @@ parse_args() { ;; "new") - check_dir - check_arg - title_to_name - - if note_exists - then - echo "Note '$note_arg' already exists! Use edit instead." - exit - fi - - echo "$note_arg" > $note_file - note_old_md5=$(md5sum $note_file) - - $EDITOR $note_file - - note_new_title=$(head -1 $note_file) - note_new_md5=$(md5sum $note_file) - note_new_file=$NOTES_DIR/$(make_title $note_new_title) - check_md5 $note_file - - dprint "Original filename = " $note_file - dprint "New filename = " $note_new_file - - # Check for a title rename - if [[ "$note_file" != "$note_new_file" ]] - then - mv -f $note_file $note_new_file - fi - - cd $NOTES_DIR - gitfile=$(basename $note_new_file) - - git add $gitfile - git commit -m "Create note '$note_new_title'" - echo "Created note '$note_new_title'" - + note_new ;; "edit") - check_dir - check_arg - title_to_name - if ! note_exists - then - echo "Cannot find note '$note_arg'!" - exit - fi - - note_old_title=$(head -1 $note_file) - note_old_md5=$(md5sum $note_file) - - $EDITOR $note_file - - note_new_title=$(head -1 $note_file) - note_new_md5=$(md5sum $note_file) - - check_md5 - - cd $NOTES_DIR - gitfile=$(basename $note_file) - commit_msg="Update note '$note_new_title'" - if [[ "$note_old_title" != "$note_new_title" ]] - then - note_new_file=$(make_title $note_new_title) - - git mv $gitfile $note_new_file - git add $note_new_file - commit_msg="$commit_msg. Rename from '$note_old_title'" - else - git add $gitfile - fi - - git commit -m "$commit_msg" - echo "Updated note '$note_new_title'" + note_edit ;; "del" | "delete") - check_dir - check_arg - title_to_name - if ! note_exists - then - echo "Cannot find note '$note_arg'!" - exit - fi - - cd $NOTES_DIR - gitfile=$(basename $note_file) - git rm $gitfile - git commit -m "Delete note '$note_arg'" - echo "Deleted note '$note_arg'" + note_delete ;; "hist"|"history") - check_dir - cd $NOTES_DIR - git log --pretty=format:'%ai - %s' -- $note_arg + note_history ;; "init") notes_init ;; - "list") - check_dir - cd $NOTES_DIR - ls -1 $note_arg + "list" | "find") + note_list $1 ;; + "search") + note_search $1 + ;; + "ver" | "version") version_info ;; From 2fda4cbad215bedb2fe26c7f0117d500562f9c04 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 31 Jan 2013 13:33:33 -0800 Subject: [PATCH 15/48] Fix check_empty and add show command to note check_empty for restore was trying to run a git checkout in the current working directory, instead of in the ~/.notes directory. Fix this issue note_show uses less to display the contents of the note file. --- note | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/note b/note index b3132b8..a5e4952 100755 --- a/note +++ b/note @@ -68,11 +68,14 @@ check_empty() { then if [[ "$1" == "restore" ]] then + cd "$NOTES_DIR" gitfile=$(basename "$note_file") git checkout $gitfile + echo "Restored empty note $note_arg" elif [[ "$1" == "delete" ]] then rm -f $note_file + echo "Deleted empty note $note_arg" fi exit @@ -198,6 +201,19 @@ note_edit() { echo "Updated note '$note_new_title'" } +note_show() { + check_dir + check_arg + title_to_name + if ! note_exists + then + echo "Cannot find note '$note_arg'!" + exit + fi + + less -R $note_file +} + note_delete() { check_dir check_arg @@ -266,6 +282,10 @@ parse_args() { note_edit ;; + "show") + note_show + ;; + "del" | "delete") note_delete ;; From ec6b62badc9e1447a74966048bfa21275f4abffc Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 31 Jan 2013 14:52:41 -0800 Subject: [PATCH 16/48] Add beep script This script evaluates the commands specified on the command line and sends a beep to the terminal after the command completes execution. In tmux, this results in the window status bar displaying the beep signal. --- beep | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 beep diff --git a/beep b/beep new file mode 100755 index 0000000..167a243 --- /dev/null +++ b/beep @@ -0,0 +1,8 @@ +#!/bin/sh + +if [[ $# -gt 0 ]] +then + eval "$*" + echo -e "\007" +fi + From 2675146dea7bc1f26bc2639e94726f686cd24ade Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 31 Jan 2013 14:59:23 -0800 Subject: [PATCH 17/48] Fix note to use openssl MD5 instead of md5sum --- note | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/note b/note index a5e4952..3f1e59e 100755 --- a/note +++ b/note @@ -71,11 +71,11 @@ check_empty() { cd "$NOTES_DIR" gitfile=$(basename "$note_file") git checkout $gitfile - echo "Restored empty note $note_arg" + echo "Restored empty note '$note_arg'" elif [[ "$1" == "delete" ]] then rm -f $note_file - echo "Deleted empty note $note_arg" + echo "Deleted empty note '$note_arg'" fi exit @@ -90,6 +90,10 @@ check_dir() { fi } +md5_sum() { + openssl dgst -md5 $* +} + help_cmd() { echo ' Notes Manager for the Command Line @@ -135,12 +139,12 @@ note_new() { fi echo "$note_arg" > $note_file - note_old_md5=$(md5sum $note_file) + note_old_md5=$(md5_sum $note_file) $EDITOR $note_file note_new_title=$(head -1 $note_file) - note_new_md5=$(md5sum $note_file) + note_new_md5=$(md5_sum $note_file) note_new_file=$NOTES_DIR/$(make_title $note_new_title) check_md5 $note_file check_empty delete @@ -173,12 +177,12 @@ note_edit() { fi note_old_title=$(head -1 $note_file) - note_old_md5=$(md5sum $note_file) + note_old_md5=$(md5_sum $note_file) $EDITOR $note_file note_new_title=$(head -1 $note_file) - note_new_md5=$(md5sum $note_file) + note_new_md5=$(md5_sum $note_file) check_md5 check_empty restore From bfb42d3732a920d668979a4fd125bb4528f18252 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 31 Jan 2013 15:27:21 -0800 Subject: [PATCH 18/48] Rename commands and add short forms --- note | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/note b/note index 3f1e59e..1946a73 100755 --- a/note +++ b/note @@ -107,16 +107,17 @@ Usage: note Commands -------- +You can use the first letter of each command as a shortcut key. Command Usage ------- ----- new Follow this with the note name to create a new note edit Follow this with the note name to edit an existing note + show Follow this with the note name to display an existing note delete Follow this with the note name to delete a note - list Prints the list of available notes - history Displays the note history - find Finds all notes with the specified keyword in the filename - search Finds all notes with the specified keyword in the text + find Prints the list of available notes + log Displays the note history + grep Finds all notes with the specified keyword in the text init Run this the very first time to set up the folders help Prints this help message version Prints the version information @@ -274,43 +275,43 @@ parse_args() { note_arg="$*" case "$note_cmd" in - "help") + "h" | "help") help_cmd ;; - "new") + "n" | "new") note_new ;; - "edit") + "e" | "edit") note_edit ;; - "show") + "s" | "show") note_show ;; - "del" | "delete") + "d" | "delete") note_delete ;; - "hist"|"history") + "l" | "log") note_history ;; - "init") + "i" | "init") notes_init ;; - "list" | "find") + "f" | "find") note_list $1 ;; - "search") + "g" | "grep") note_search $1 ;; - "ver" | "version") + "v" | "version") version_info ;; From aebd58f73db867624f6fba60ccf70fcf9abd1e2a Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 6 Feb 2013 21:59:02 -0800 Subject: [PATCH 19/48] Add git-branch-parent script from git-scripts repo --- git/git-branch-parent | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 git/git-branch-parent diff --git a/git/git-branch-parent b/git/git-branch-parent new file mode 100755 index 0000000..f83767f --- /dev/null +++ b/git/git-branch-parent @@ -0,0 +1,28 @@ +#!/bin/bash +# +# Support script to find the commit from which the given branch was spawned. +# Takes one or two arguments, the first argument is mandatory and specifies +# the child branch while the second argument is optional and specifies the +# parent branch. If omitted, the parent branch defaults to 'master' + +CHILD=$1 +PARENT=$2 + +USAGE="Usage: $0 " + +if [ "$CHILD" == "" ] +then + echo $USAGE + exit +fi + +if [ "$PARENT" == "" ] +then + PARENT="master" +fi + +PCOMMIT=`diff -u <(git rev-list --first-parent $CHILD) \ + <(git rev-list --first-parent $PARENT) | sed -ne 's/^ //p' | head -1` + +git show --pretty="%H %an | %s" $PCOMMIT | head -1 + From 35eb2b8da82339acad2f1a216cc48f5270478e76 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 11 Feb 2013 15:39:15 -0800 Subject: [PATCH 20/48] Allow user to pass in grep pattern This also disables the search by word, so to search only by word boundaries, surround the pattern by \< and \> --- note | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/note b/note index 1946a73..c398315 100755 --- a/note +++ b/note @@ -264,8 +264,8 @@ note_search() { cd $NOTES_DIR for file in * do - grep --color=always -il "\<$1\>" $file - grep --color=always -inhT -m2 -C1 "\<$1\>" $file + grep --color=always -il "$1" $file + grep --color=always -inhT -m2 -C1 "$1" $file done } From 8cf3f6d25b31df9255d616a077c8d66bcaa06a3d Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 14 Feb 2013 10:57:42 -0800 Subject: [PATCH 21/48] Add smartwd script - WIP --- smartwd | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100755 smartwd diff --git a/smartwd b/smartwd new file mode 100755 index 0000000..dad6c30 --- /dev/null +++ b/smartwd @@ -0,0 +1,45 @@ +#!/usr/bin/env ruby +# -*- ruby -*- +# Script to smartly chop off portions of the current working directory for +# use in the shell prompt + +# Constants used to decide when to start chopping +DIRLIM = 4 +PWDLEN = 14 + +def smartwd + username = ENV['USER'] + homedir = ENV['HOME'] + pwd = ENV['PWD'] + + path = pwd.split('/') + #pwd = pwd.gsub(homedir, '~') + + # Ignore the root path + if (path.length > 0) + index = path.index(username) + if index + prefix = path.shift(index + 1) + + # We need to map additional paths in environments where the user + # may have more than one available folder in his/her name. + if prefix.join('/') == homedir + path.unshift('~') + else + # The first entry in the prefix array is the empty string + pre = prefix[1].split('').shift(4).join('') + '~' + path.unshift(pre) + end + else + # Replace the first two entries in the array with / + # (because the first entry is always empty string) + prefix = path.shift(2).join('/') + path.unshift(prefix) + end + end + + puts username, homedir, pwd, prefix.inspect, path.inspect +end + +smartwd + From 3520ba84b6df0627583be46be33e26a3c7fa8ef0 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 14 Feb 2013 14:11:12 -0800 Subject: [PATCH 22/48] Fix smartwd script and update PS1 in bashrc.common --- smartwd | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/smartwd b/smartwd index dad6c30..b28529d 100755 --- a/smartwd +++ b/smartwd @@ -4,7 +4,9 @@ # use in the shell prompt # Constants used to decide when to start chopping -DIRLIM = 4 +DIRLIM = 5 +DIR_L = 3 +DIR_R = 2 PWDLEN = 14 def smartwd @@ -13,7 +15,6 @@ def smartwd pwd = ENV['PWD'] path = pwd.split('/') - #pwd = pwd.gsub(homedir, '~') # Ignore the root path if (path.length > 0) @@ -36,9 +37,18 @@ def smartwd prefix = path.shift(2).join('/') path.unshift(prefix) end + + # Check to make sure that both the DIRLIM and PWDLEN constraints + # are met before splitting + pwd = path.join('/') + if path.length > DIRLIM and pwd.length > PWDLEN + pwd_l = path.shift(DIR_L).join('/') + pwd_r = path.pop(DIR_R).join('/') + pwd = pwd_l + '/.../' + pwd_r + end end - puts username, homedir, pwd, prefix.inspect, path.inspect + print pwd end smartwd From 1ffb0a9abed908bc8be02c8a3162eabd29eecf81 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 19 Feb 2013 14:57:30 -0800 Subject: [PATCH 23/48] Fix beep script to always send ^G --- beep | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/beep b/beep index 167a243..dd6a6ce 100755 --- a/beep +++ b/beep @@ -2,7 +2,7 @@ if [[ $# -gt 0 ]] then - eval "$*" - echo -e "\007" + eval "$@" fi +echo -e "\007" From c64326cd06df0d7a3266b36d297abecff9c4991e Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 19 Feb 2013 15:00:03 -0800 Subject: [PATCH 24/48] Enhance precision in sdate display --- sdate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdate b/sdate index 0ee3c69..9050c44 100755 --- a/sdate +++ b/sdate @@ -12,5 +12,5 @@ t = time.time() jd = (t / 86400.0 + 40587.5) # Use the idea that 10 Julian days is equal to 1 stardate -print "%05.1f" % jd +print "%05.2f" % jd From f962d96980c2e0b11b26a0f1d2dcd8a092192008 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 19 Feb 2013 15:14:12 -0800 Subject: [PATCH 25/48] Fix smartwd script to ensure that it always works With Ruby version 1.8.5, the smartwd script would abort with an error that the shift method would take no arguments. Therefore, I have restricted the use of the script to ruby version 1.8.7 and above. On older versions, it will simply behave the same as \w in PS1 of bash. --- smartwd | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/smartwd b/smartwd index b28529d..2a59afa 100755 --- a/smartwd +++ b/smartwd @@ -3,12 +3,22 @@ # Script to smartly chop off portions of the current working directory for # use in the shell prompt +# Some of the methods don't work with Ruby versions older than 1.8.7 +min_release = "1.8.7 (2008-08-11)" +ruby_release = "#{RUBY_VERSION} (#{RUBY_RELEASE_DATE})" + # Constants used to decide when to start chopping DIRLIM = 5 DIR_L = 3 DIR_R = 2 PWDLEN = 14 +def backslash_w + homedir = ENV['HOME'] + pwd = ENV['PWD'] + print pwd.gsub(homedir, '~') +end + def smartwd username = ENV['USER'] homedir = ENV['HOME'] @@ -51,5 +61,9 @@ def smartwd print pwd end -smartwd +if (ruby_release < min_release) + backslash_w +else + smartwd +end From d1d2ec1ec5d03330be34512bddc30a64546e4b29 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 12 Apr 2013 15:07:39 -0700 Subject: [PATCH 26/48] Reformat smartwd indentation to compy with Ruby coding guidelines --- smartwd | 78 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/smartwd b/smartwd index 2a59afa..4365752 100755 --- a/smartwd +++ b/smartwd @@ -14,56 +14,56 @@ DIR_R = 2 PWDLEN = 14 def backslash_w - homedir = ENV['HOME'] - pwd = ENV['PWD'] - print pwd.gsub(homedir, '~') + homedir = ENV['HOME'] + pwd = ENV['PWD'] + print pwd.gsub(homedir, '~') end def smartwd - username = ENV['USER'] - homedir = ENV['HOME'] - pwd = ENV['PWD'] + username = ENV['USER'] + homedir = ENV['HOME'] + pwd = ENV['PWD'] - path = pwd.split('/') + path = pwd.split('/') - # Ignore the root path - if (path.length > 0) - index = path.index(username) - if index - prefix = path.shift(index + 1) - - # We need to map additional paths in environments where the user - # may have more than one available folder in his/her name. - if prefix.join('/') == homedir - path.unshift('~') - else - # The first entry in the prefix array is the empty string - pre = prefix[1].split('').shift(4).join('') + '~' - path.unshift(pre) - end - else - # Replace the first two entries in the array with / - # (because the first entry is always empty string) - prefix = path.shift(2).join('/') - path.unshift(prefix) - end + # Ignore the root path + if (path.length > 0) + index = path.index(username) + if index + prefix = path.shift(index + 1) - # Check to make sure that both the DIRLIM and PWDLEN constraints - # are met before splitting - pwd = path.join('/') - if path.length > DIRLIM and pwd.length > PWDLEN - pwd_l = path.shift(DIR_L).join('/') - pwd_r = path.pop(DIR_R).join('/') - pwd = pwd_l + '/.../' + pwd_r - end + # We need to map additional paths in environments where the user + # may have more than one available folder in his/her name. + if prefix.join('/') == homedir + path.unshift('~') + else + # The first entry in the prefix array is the empty string + pre = prefix[1].split('').shift(4).join('') + '~' + path.unshift(pre) + end + else + # Replace the first two entries in the array with / + # (because the first entry is always empty string) + prefix = path.shift(2).join('/') + path.unshift(prefix) end - print pwd + # Check to make sure that both the DIRLIM and PWDLEN constraints + # are met before splitting + pwd = path.join('/') + if path.length > DIRLIM and pwd.length > PWDLEN + pwd_l = path.shift(DIR_L).join('/') + pwd_r = path.pop(DIR_R).join('/') + pwd = pwd_l + '/.../' + pwd_r + end + end + + print pwd end if (ruby_release < min_release) - backslash_w + backslash_w else - smartwd + smartwd end From 372a2e13e0a60d26e5d6f0f7706ceb5830b3878d Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 1 May 2013 22:43:36 -0700 Subject: [PATCH 27/48] Fix stardate script to truncate instead of round --- sdate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdate b/sdate index 9050c44..daddd5b 100755 --- a/sdate +++ b/sdate @@ -12,5 +12,5 @@ t = time.time() jd = (t / 86400.0 + 40587.5) # Use the idea that 10 Julian days is equal to 1 stardate -print "%05.2f" % jd +print ("%05.3f" % jd)[:-1] From cbb52a3db2de5861e0d37c7495d94fbe4c22800e Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 3 May 2013 22:26:43 -0700 Subject: [PATCH 28/48] Improve precision on stardate script There is a possibility that for every 0.1 stardates (8640 seconds), the final 0.05 stardate would get rounded up (432 seconds). This kind of inaccuracy is unacceptable by Starfleet standards, so I've reduced the inaccuracy to 5e-10 stardates or 43.2 microseconds. --- sdate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdate b/sdate index daddd5b..a33cf6d 100755 --- a/sdate +++ b/sdate @@ -12,5 +12,5 @@ t = time.time() jd = (t / 86400.0 + 40587.5) # Use the idea that 10 Julian days is equal to 1 stardate -print ("%05.3f" % jd)[:-1] +print ("%05.9f" % jd)[:-7] From 342b6a97753f67a6edba01ab8cd39defcd6570f2 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 16 May 2013 13:36:35 -0700 Subject: [PATCH 29/48] Rename sdate script to 'stardate' --- sdate => stardate | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sdate => stardate (100%) diff --git a/sdate b/stardate similarity index 100% rename from sdate rename to stardate From 7f0e20cfeff0a1f6c42f5ed8201b491337bdc149 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 7 Aug 2013 15:47:54 -0700 Subject: [PATCH 30/48] Fix comment in stardate script --- stardate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stardate b/stardate index a33cf6d..c1a7c37 100755 --- a/stardate +++ b/stardate @@ -11,6 +11,6 @@ t = time.time() concept, we add only 40587.5""" jd = (t / 86400.0 + 40587.5) -# Use the idea that 10 Julian days is equal to 1 stardate +# Use the idea that 1 Julian day is equal to 1 stardate print ("%05.9f" % jd)[:-7] From fec04c7037c10e8d19eac0d8d154d6afd198956b Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 9 Aug 2013 19:34:27 -0700 Subject: [PATCH 31/48] Add applygitconfig script Simple Perl script to apply a git config from any gitconfig file --- applygitconfig | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100755 applygitconfig diff --git a/applygitconfig b/applygitconfig new file mode 100755 index 0000000..dab208e --- /dev/null +++ b/applygitconfig @@ -0,0 +1,27 @@ +#!/usr/bin/env perl +# Script to apply git configuration to global gitconfig + +my $section = 'unknown'; +my $variable; +my $value; +my $command; + +while(<>) { + chomp; + if (m/^\[(\w+)\]$/) { + $section = $1; + #print "Section: $section\n"; + } elsif (m/^\[(\w+) +"(\w+)"\]/) { + $section = "$1.$2"; + #print "Section: $section\n"; + } elsif (m/(\w+) += +(.+)$/) { + $variable = $1; + $value = $2; + + $value =~ s/"/\\"/g; + #print "\t$section.$variable = \"$value\"\n"; + $command = "git config --global $section.$variable \"$value\""; + print "$command\n"; + system($command); + } +} From ce03c06b174398287b2e43c90e1905147a9f1807 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 9 Sep 2013 23:58:00 -0700 Subject: [PATCH 32/48] Script to track daily work items Work items are logged against a monthly file, so you can quickly see at a glance what your activities were for the last month --- mlog | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 mlog diff --git a/mlog b/mlog new file mode 100755 index 0000000..0fe79c9 --- /dev/null +++ b/mlog @@ -0,0 +1,39 @@ +#!/bin/bash +# Monthly logging script +# Call mlog and it will update a log file + +# Set the default mlog folder +[[ -z $MLOG_FOLDER ]] && MLOG_FOLDER="$HOME/mlog" + +# Make sure that the folder exists +[[ ! -d $MLOG_FOLDER ]] && mkdir -pv $MLOG_FOLDER + +# Make sure that we have a valid TMPDIR +[[ -z $TMPDIR ]] && TMPDIR='/tmp' + +# Make sure that we have a valid EDITOR +[[ -z $EDITOR ]] && EDITOR=vim + +MLOG_HDR='' + +if [[ -x $HOME/bin/stardate ]] +then + [[ -z $MLOG_RANK ]] && MLOG_RANK='Chief Engineer' + + MLOG_HDR="$MLOG_RANK's log, Stardate $($HOME/bin/stardate)\n" +fi + +MLOG_HDR="$MLOG_HDR[$(date)]" + +MLOG_FILE="$TMPDIR/mlog-tmp-$$" + +$EDITOR $MLOG_FILE + +[[ ! -s $MLOG_FILE ]] && exit 0 + +MLOG_LOG="$MLOG_FOLDER/$(date +%Y-%m)" + +echo -e "$MLOG_HDR\n" >> $MLOG_LOG +cat $MLOG_FILE >> $MLOG_LOG +echo -e "\n----------------------------------------\n" >> $MLOG_LOG + From 2891d5925914657eb10c232f403f96352def68a3 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 10 Sep 2013 14:05:00 -0700 Subject: [PATCH 33/48] Add supplemental log handling to mlog This also takes care of deleting temp log files --- mlog | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/mlog b/mlog index 0fe79c9..2e61fb4 100755 --- a/mlog +++ b/mlog @@ -15,25 +15,35 @@ [[ -z $EDITOR ]] && EDITOR=vim MLOG_HDR='' +MLOG_TS="[$(date)]" +MLOG_LOG="$MLOG_FOLDER/$(date +%Y-%m)" +MLOG_FILE="$TMPDIR/mlog-tmp-$$" if [[ -x $HOME/bin/stardate ]] then [[ -z $MLOG_RANK ]] && MLOG_RANK='Chief Engineer' - MLOG_HDR="$MLOG_RANK's log, Stardate $($HOME/bin/stardate)\n" + MLOG_HDR=`echo "$MLOG_RANK's log, Stardate $($HOME/bin/stardate)" | sed 's/.$//'` + + if [[ -e $MLOG_LOG ]] + then + MLOG_LAST=$(grep Stardate $MLOG_LOG | tail -1) + + if [[ "$MLOG_LAST" == "$MLOG_HDR" ]] + then + MLOG_HDR="$MLOG_RANK's log, supplemental" + fi + fi fi -MLOG_HDR="$MLOG_HDR[$(date)]" - -MLOG_FILE="$TMPDIR/mlog-tmp-$$" - $EDITOR $MLOG_FILE -[[ ! -s $MLOG_FILE ]] && exit 0 +[[ ! -s $MLOG_FILE ]] && rm -f $MLOG_FILE && exit 0 -MLOG_LOG="$MLOG_FOLDER/$(date +%Y-%m)" -echo -e "$MLOG_HDR\n" >> $MLOG_LOG +echo "$MLOG_HDR" >> $MLOG_LOG +echo "$MLOG_TS" >> $MLOG_LOG +echo >> $MLOG_LOG # Empty line cat $MLOG_FILE >> $MLOG_LOG echo -e "\n----------------------------------------\n" >> $MLOG_LOG From 8da558be6bac6e43063cd55f1abb9d2e5705407b Mon Sep 17 00:00:00 2001 From: nirenjan Date: Tue, 17 Sep 2013 15:00:36 -0700 Subject: [PATCH 34/48] Print a full 80-char separator in mlog --- mlog | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mlog b/mlog index 2e61fb4..b1c0a83 100755 --- a/mlog +++ b/mlog @@ -45,5 +45,6 @@ echo "$MLOG_HDR" >> $MLOG_LOG echo "$MLOG_TS" >> $MLOG_LOG echo >> $MLOG_LOG # Empty line cat $MLOG_FILE >> $MLOG_LOG -echo -e "\n----------------------------------------\n" >> $MLOG_LOG +echo -en "\n----------------------------------------" >> $MLOG_LOG +echo -e "----------------------------------------\n" >> $MLOG_LOG From 0a74f2f6ada3bc125fd801a7ae1ff86e7dd346e0 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 30 Sep 2013 20:46:50 -0700 Subject: [PATCH 35/48] Remove the temporary file before exiting mlog --- mlog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mlog b/mlog index b1c0a83..b9e3444 100755 --- a/mlog +++ b/mlog @@ -48,3 +48,5 @@ cat $MLOG_FILE >> $MLOG_LOG echo -en "\n----------------------------------------" >> $MLOG_LOG echo -e "----------------------------------------\n" >> $MLOG_LOG +rm -f $MLOG_FILE + From d0d937cc9c4052b39f315f61c7186f5da794704b Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 16 Oct 2013 21:41:04 -0700 Subject: [PATCH 36/48] Add deploy script to install files from Git repos Deploy files are bash scripts with a couple of extra commands - namely the repo, target and file commands. This allows a user to specify a deployment file with a source repository, destination folder and specific Git version to extract. --- deploy | 191 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100755 deploy diff --git a/deploy b/deploy new file mode 100755 index 0000000..a400114 --- /dev/null +++ b/deploy @@ -0,0 +1,191 @@ +#!/bin/bash +# Script to copy files from a Git repository to a destination +# Reads deployment instructions from a file and acts accordingly +# +# File Format +# =========== +# +# Comments begin with a # character and terminate at the end of the line. +# +# The keyword 'repo' specifies the path to the repository. The repository must +# be accessible locally without having to resort to ssh, https or git protocols. +# Usage: repo +# +# The keyword 'target' specifies the location to which to deploy. If omitted, it +# defaults to the current folder. Folders can be specified relative to the +# current folder, relative to the home directory, or relative to the root, using +# ./ or ../, ~/ and / as prefixes respectively. The parameter must be a valid +# directory. +# Usage: target +# +# The keyword 'file' specifies the file location relative to the root of the +# Git repository, along with the version to install and an optional rename +# argument which allows you to rename the destination file. If the version is +# omitted, it defaults to HEAD. If the rename-to parameter is omitted, the +# filename is the same as the name within the repository. +# Usage: file [version [rename-to]] + +# Default environment variables used +DEPLOY_GIT='' +DEPLOY_TGT=$(pwd) +DEPLOY_SRC='' +DEPLOY_LST='' +DEPLOY_DEBUG=false + +# Usage function will display the usage +usage() +{ + local deploy=$(basename $0) + echo "Usage: $deploy ..." + exit 0 +} + +# Die function will print the error message to stderr and abort the script +die() +{ + local exit_code=$1 + local deploy=$(basename $0) + shift + + for msg in "$@" + do + echo -e "$deploy: $msg" >&2 + done + + if [[ $exit_code != 128 && $exit_code != 0 ]] + then + echo -e "\tError in file $DEPLOY_SRC line ${BASH_LINENO[1]}" >&2 + fi + + exit $exit_code +} + +# Debug function will display all data, but only if debugs are enabled +debug() +{ + $DEPLOY_DEBUG && echo "$@" +} + +# Repo function checks if it is a valid Git repo and sets the DEPLOY_GIT +repo() +{ + if [[ $# != 1 ]] + then + die 4 "Invalid usage of repo command" "Usage: repo " + fi + + if [[ -d "$1" ]] + then + cd "$1" + local gtl=$(git rev-parse --show-toplevel 2>/dev/null) + + if [[ -z $gtl ]] + then + die 3 "Path $1 is not a valid Git repository!" + else + debug "Using repository $gtl" + DEPLOY_GIT="$gtl" + fi + else + die 1 "Path $1 does not exist!" + fi +} + +# Target function checks if it is a valid directory and sets the DEPLOY_TGT +target() +{ + if [[ $# != 1 ]] + then + die 4 "Invalid usage of target command" "Usage: target " + fi + + if [[ -d "$1" ]] + then + debug "Target folder $1" + DEPLOY_TGT="$1" + else + die 1 "Path $1 does not exist!" + fi +} + +# File command selects a file to deploy +file() +{ + if [[ $# == 0 || $# > 3 ]] + then + die 4 "Invalid usage of file command" \ + "Usage: file [ [] ]" + fi + + if [[ -z $DEPLOY_GIT ]] + then + die 2 "Must specify repo before file!" + fi + + if [[ -z $DEPLOY_TGT ]] + then + die 2 "Must specify target before file!" + fi + + local file=$1 + local ver=$2 + local rename=$3 + + debug "file $@" + + # Sanity check on ver + [[ -z $ver ]] && ver=HEAD + + # Sanity check on rename + [[ -z $rename ]] && rename=$(basename $file) + + cd $DEPLOY_GIT + + # Check to make sure that version is a sane version + git rev-parse $ver &>/dev/null + + if [[ $? != 0 ]] + then + die 3 "Version $ver does not exist in the repository $DEPLOY_GIT" + fi + + local vercheck=$(git rev-parse $ver 2>/dev/null) + debug "Using version $vercheck" + + # Check to make sure that the file is valid in the specified version + git show $vercheck:$file &>/dev/null + + if [[ $? != 0 ]] + then + die 3 "File $file does not exist in version $ver of the repo $DEPLOY_GIT" + else + debug "Using file $file" + local cmd="cd $DEPLOY_GIT; git show $vercheck:$file > $DEPLOY_TGT/$rename;" + DEPLOY_LST="$DEPLOY_LST $cmd" + fi + +} + +if [[ $# == 0 ]] +then + usage +else + for i in "$@" + do + if [[ -f $i && -r $i ]] + then + DEPLOY_SRC=$i + source $i + else + die 128 "Deploy file $i does not exist or is not readable!" + fi + done +fi + +if [[ ! -z $DEPLOY_LST ]] +then + eval $DEPLOY_LST +else + die 0 "No files to deploy!" +fi + From 8b8b4850862418660bce8772e7ff90da0428d63f Mon Sep 17 00:00:00 2001 From: nirenjan Date: Thu, 17 Oct 2013 11:59:09 -0700 Subject: [PATCH 37/48] Add commands to set executable permission on file The deploy script was simply redirecting the contents of the files into the target files, however, they were getting created without executable permissions. Since this script is designed to deploy executables, it is required to set the executable permissions. This cannot be done in the deployment configuration file however, because the script parses all the input files first before extracting the relevant contents. Therefore, if we try to run chmod +x within the file, it would fail because the file would not exist yet (or run deploy twice). This commit also cleans up the formatting when building the command line so that each command is set up on it's own line. --- deploy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/deploy b/deploy index a400114..fb16888 100755 --- a/deploy +++ b/deploy @@ -160,7 +160,11 @@ file() die 3 "File $file does not exist in version $ver of the repo $DEPLOY_GIT" else debug "Using file $file" - local cmd="cd $DEPLOY_GIT; git show $vercheck:$file > $DEPLOY_TGT/$rename;" + # Build the commands to extract the file and set the + # executable permissions bit in the deployed file. + local cmd="cd $DEPLOY_GIT" + cmd="$cmd; git show $vercheck:$file > $DEPLOY_TGT/$rename" + cmd="$cmd; chmod +x $DEPLOY_TGT/$rename;" DEPLOY_LST="$DEPLOY_LST $cmd" fi From 5c5354fa91f3c1eb7140223a4cc66af4d9beb804 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 29 Nov 2013 22:25:16 -0800 Subject: [PATCH 38/48] Add mkiso script mkiso converts an image into ISO/Joliet compatible format suitable for burning onto a CD/DVD and having it cross-compatible with Win/OSX/*NIX --- mkiso | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100755 mkiso diff --git a/mkiso b/mkiso new file mode 100755 index 0000000..f61a0fa --- /dev/null +++ b/mkiso @@ -0,0 +1,71 @@ +#!/bin/bash +# Make an ISO/Joliet compatible image from a CDR image on OS X + +APP=$(echo $0 | sed 's#^.*/##') + +usage() +{ + echo " +$APP converts an image into ISO/Joliet compatible format suitable for +burning onto a CD/DVD and having it cross-compatible with Win32/OSX/*NIX + +Usage: $APP [-o ] +" +} + +while getopts :ho: OPTION +do + case $OPTION in + h) + # Help + usage + exit 0 + ;; + + o) + # Output file + OUTFILE=$OPTARG + ;; + + :) + # Missing required argument + echo "$APP: Missing argument for option -$OPTARG" >&2 + exit 1 + ;; + + \?) + # Invalid option + echo "$APP: Invalid option: -$OPTARG" >&2 + exit 1 + ;; + esac +done + +# Shift away the options +shift $(($OPTIND - 1)) + +INFILE=$1 + +if [[ -z $INFILE ]] +then + usage + exit 1 +fi + +if [[ ! -r $INFILE ]] +then + echo "$APP: Unable to read input file $INFILE" >&2 + exit 1 +fi + +if [[ -z $OUTFILE ]] +then + OUTFILE=$(echo $INFILE | sed -E 's#\.[^\.]+$#\.iso#') +fi + +echo "Input file = $INFILE" +echo "Output file = $OUTFILE" + +hdiutil makehybrid -iso -joliet -o "$OUTFILE" "$INFILE" + +exit $? From 330c8e5a7e49f5ab1db762b9ba7aaf1fbbc3b86e Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 29 Nov 2013 22:27:26 -0800 Subject: [PATCH 39/48] Create README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..5318313 --- /dev/null +++ b/README.md @@ -0,0 +1,6 @@ +Scripts +======= + +Handy scripts for ease of use + +* mkiso - Make ISO file from CD/DVD image suitable for burning From b076b2ed23d66126428696d7d6373e81262c410d Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 29 Nov 2013 22:36:27 -0800 Subject: [PATCH 40/48] Add comment identifying source for mkiso --- mkiso | 1 + 1 file changed, 1 insertion(+) diff --git a/mkiso b/mkiso index f61a0fa..ac84873 100755 --- a/mkiso +++ b/mkiso @@ -1,5 +1,6 @@ #!/bin/bash # Make an ISO/Joliet compatible image from a CDR image on OS X +# Developed based on http://forums.macrumors.com/showthread.php?t=220740 APP=$(echo $0 | sed 's#^.*/##') From 73e53323da7ab84651ac47184387918296af4ec7 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sun, 8 Dec 2013 20:23:25 -0800 Subject: [PATCH 41/48] Add backup script to copy folder contents Folder contents can be either tarred and gzipped or saved to a git repository. The default configuration is to use git and tags every time the backup is made, even if there were no commits. --- backup | 202 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100755 backup diff --git a/backup b/backup new file mode 100755 index 0000000..60b7a82 --- /dev/null +++ b/backup @@ -0,0 +1,202 @@ +#!/bin/bash +# Script to backup folders to either a tarball or a Git repository +# Script expects to run at most on a daily basis. Any faster, and you risk +# overwriting a previous backup. + +# Default environment variables used +BACKUP_METHOD=git +BACKUP_TS=$(date +%F) +BACKUP_DEBUG=false +BACKUP_SRC='' +BACKUP_DST='' +BACKUP_DB='' + +APP=$(basename $0) + +# Usage function will display the usage +usage() +{ + local backup=$APP + local exit_code=$1 + + [[ -z $exit_code ]] && exit_code=0 + + echo "Usage: $backup [options] " + echo "Options:" + echo " -s tar Save to a tarball, format is backup-%Y-%m-%d.tar.gz" + echo " This is the default." + echo " -s git Save to a git repository and tag the commit" + echo + echo " -t Specify the path to save the backup to. If omitted," + echo " it defaults to ~/backups/\$(basename )" + echo + echo " -d Specify a MySQL database to backup in addition to" + echo " the files in the source path. This requires you to" + echo " save the MySQL root password in ~/.my.cnf" + echo + echo " -h Display this help message" + + exit $exit_code +} + +# Die function will print the error message to stderr and abort the script +die() +{ + local exit_code=$1 + local backup=$APP + shift + + for msg in "$@" + do + echo -e "$backup: $msg" >&2 + done + + exit $exit_code +} + +# Sanity check +backup_sanity() +{ + if [[ -z $BACKUP_SRC ]] + then + die 1 "Need to specify a source" + fi + + # Run basic sanity checks on env variables + if [[ -z $BACKUP_DST ]] + then + BACKUP_DST=$HOME/backups/$(basename $BACKUP_SRC) + fi + + mkdir -p $BACKUP_DST + if [[ $? != 0 ]] + then + die 2 "Error creating backup folder" + fi +} + +# Retrieve data using rsync +backup_data() +{ + # Don't rsync if we are using tar as the backup method + if [[ "$BACKUP_METHOD" != "tar" ]] + then + rsync -a $BACKUP_SRC $BACKUP_DST + + if [[ $? != 0 ]] + then + die 2 "Error syncing data from source $BACKUP_SRC" + fi + fi +} + +# Backup the database +backup_db() +{ + if [[ ! -z $BACKUP_DB ]] + then + # Dump using mysqldump + mysqldump -u root $BACKUP_DB >$BACKUP_DST/$BACKUP_DB.sql 2>/dev/null + + if [[ $? != 0 ]] + then + die 2 "Error dumping database $BACKUP_DB" + fi + fi +} + +# Save the files to git repo or tarball +backup_save() +{ + if [[ "$BACKUP_METHOD" == "tar" ]] + then + cd $BACKUP_SRC + local daily=$BACKUP_DST/backup-daily-$BACKUP_TS.tar.gz + tar czf $daily . + + # Weekly tarballs every Sunday + if [[ $(date +%w) == "0" ]] + then + local weekly=$BACKUP_DST/backup-weekly-$(date +%Y-%U).tar.gz + ln $daily $weekly + fi + + # Monthly tarballs on the first of every month + if [[ $(date +%-d) == "1" ]] + then + local monthly=$BACKUP_DST/backup-monthly-$(date +%Y-%m).tar.gz + ln $daily $monthly + fi + else + cd $BACKUP_DST + git init -q + git config core.safecrlf false + git add . + git commit -m "Backup of $BACKUP_SRC on $BACKUP_TS" + git tag -am "Daily ($BACKUP_TS) backup of $BACKUP_SRC" daily-$BACKUP_TS + + # Weekly tags every Sunday + if [[ $(date +%w) == "0" ]] + then + local week=$(date +%Y-%U) + git tag -am "Weekly ($week) backup of $BACKUP_SRC" weekly-$week + fi + + # Monthly tags on the first of every month + if [[ $(date +%-d) == "1" ]] + then + local month=$(date +%Y-%m) + git tag -am "Monthly ($month) backup of $BACKUP_SRC" monthly-$month + fi + fi +} + +while getopts :hs:t:d: OPTION +do + case $OPTION in + h) + # Help + usage 0 + ;; + + s) + # Backup method + if [[ "$OPTARG" == "git" || "$OPTARG" == "tar" ]] + then + BACKUP_METHOD=$OPTARG + else + die 1 "Backup method must be one of git or tar" + fi + ;; + t) + # Target folder + BACKUP_DST=$OPTARG + ;; + + d) + # Database + BACKUP_DB=$OPTARG + ;; + + :) + # Missing required argument + die 1 "Missing argument for option -$OPTARG" + ;; + + \?) + # Invalid option + die 1 "Invalid option: -$OPTARG" + ;; + + esac +done + +# Shift away the options +shift $(($OPTIND - 1)) + +BACKUP_SRC=$1 +backup_sanity +backup_data +backup_db +backup_save + From 46d993cdd9005aa9912e94c69a4c54b2ab3ebb1c Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sun, 25 May 2014 21:36:48 -0700 Subject: [PATCH 42/48] Import scripts from dotfiles repository --- mkpasswd | 5 +++++ 1 file changed, 5 insertions(+) create mode 100755 mkpasswd diff --git a/mkpasswd b/mkpasswd new file mode 100755 index 0000000..5039351 --- /dev/null +++ b/mkpasswd @@ -0,0 +1,5 @@ +#!/bin/bash + +len=$1 +[[ "$len" == "" ]] && len=16 +LC_CTYPE=C tr -dc "[:alnum:]" < /dev/urandom | head -c ${len} | xargs From 2241fdc6577e1ae2ac086bdc2e3fa6b753db304b Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sun, 25 May 2014 21:41:40 -0700 Subject: [PATCH 43/48] Update README to reflect new scripts --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 5318313..10f9a50 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,18 @@ Scripts Handy scripts for ease of use +* applygitconfig - Treat the contents of stdin as a gitconfig file and apply + them to the global config (~/.gitconfig) +* backup - Folder backup utility script +* beep - Sound a beep after executing the given command line +* deploy - Deploy a set of files from a git repository to a destination folder +* dircolortest - Test the contents of stdin as a .dir_colors file +* git-branch-parent - Attempt to determine the branch root SHA * mkiso - Make ISO file from CD/DVD image suitable for burning +* mkpasswd - Generate a random alphanumeric password of any length +* mlog - Logging utility script +* note - Note taking utility script +* settitle - Set the terminal/tmux window title +* smartwd - Print the current working directory for the command prompt +* stardate - Print the current Stardate + From 5ff851cb6dab4eaa9a8b7df2c42815a2e8d9b1c9 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Wed, 25 Feb 2015 12:08:14 -0800 Subject: [PATCH 44/48] Fix beep script to use bash instead of sh --- beep | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beep b/beep index dd6a6ce..4739c4d 100755 --- a/beep +++ b/beep @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash if [[ $# -gt 0 ]] then From 1245674adc825ffe9c8cbd20b4189b27ce537fec Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 26 Jun 2015 14:40:35 -0700 Subject: [PATCH 45/48] Add mkcscope script --- mkcscope | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100755 mkcscope diff --git a/mkcscope b/mkcscope new file mode 100755 index 0000000..334f988 --- /dev/null +++ b/mkcscope @@ -0,0 +1,163 @@ +#!/bin/bash +# Script to create cscope.out file for any workspace +# Script expects to see a .mkcscope.conf file in either the current working +# directory or in your $HOME. The format of the file is as follows: +# +# This is essentially a Bash script, using a structured language as follows: +# target foo bar +# desc Description for foo +# files +# +# The above segment shows a sample target 'foo', which depends on a different +# target 'bar'. The desc line is a description for use in the help, while the +# files command specifies a directory relative to the current directory and +# the file patterns to search for and add to the cscope file list. The script +# expects to run from the base folder for the project workspace, and the +# directories in the files command are relative to this base folder. +# +# Example: +# -------- +# target tmux ncurses +# desc tmux Source code +# files tmux *.[ch] *.[cpp] +# +# target ncurses +# desc ncurses source code +# files ncurses *.[ch] + +APP=$(basename $0) +VERSION='1.0.0' +MAKEFILE=/dev/null + +declare -a TARGETS=() + +usage() +{ + echo "$APP v$VERSION" + echo "Usage: $APP [options] " + echo + echo "Options:" + echo "--------" + echo " -h Show this help screen" + echo " -l List targets" + echo " -r Rebuild cscope DB without regenerating files" + echo +} + +files() +{ + local dir=$1 + shift + for patt in "$@" + do + echo -e "\t@find $PWD/$dir -name \"$patt\" -print >> $CSCOPE_FILES" >> $MAKEFILE + done +} + +desc() +{ + echo -e "\t@echo $@" >> $MAKEFILE +} + +target() +{ + local tgt=$1 + shift + + echo "$tgt: $@" >> $MAKEFILE + + TARGETS=(${TARGETS[@]} $tgt) +} + +list_targets() +{ + echo "Supported targets:" + echo "------------------" + for tgt in ${TARGETS[@]} + do + echo " - $tgt" + done +} + +gen_makefile() +{ + MAKEFILE=$(mktemp --tmpdir cscope-$USER-XXXXXXXX.mk) + CSCOPE_FILES=$PWD/cscope.files +} + +gen_cscope() +{ + cscope -bqv +} + +gen_targets() +{ + set -f + + if [[ -r "$PWD/.mkcscope.conf" ]] + then + source "$PWD/.mkcscope.conf" + elif [[ -r "$HOME/.mkcscope.conf" ]] + then + source "$HOME/.mkcscope.conf" + else + echo "Unable to find a configuration file!" >&2 + echo "Expect to find a .mkcscope.conf in either of:" >&2 + echo " $PWD" >&2 + echo " $HOME" >&2 + exit 1 + fi +} + +cleanup() +{ + rm $MAKEFILE +} + +trap cleanup "EXIT" + +gen_makefile +gen_targets + +while getopts "hlr" OPTION +do + case $OPTION in + h) + usage + exit 0 + ;; + + l) + list_targets + exit 0 + ;; + + r) + echo "Rebuilding existing cscope database" + gen_cscope + exit 0 + ;; + + \?) + echo "Invalid option -$OPTARG" + exit 1 + ;; + esac +done + +shift $(($OPTIND - 1)) + +if [[ -n "$1" ]] +then + rm -f $CSCOPE_FILES + make -f $MAKEFILE "$@" + + if [[ ! -s $CSCOPE_FILES ]] + then + echo "$APP: Must specify targets to generate file list!" >&2 + exit 1 + fi + + gen_cscope +fi + From dd6a3182f82049a6c059c4ccb053c72634a1e56a Mon Sep 17 00:00:00 2001 From: nirenjan Date: Fri, 26 Jun 2015 14:59:41 -0700 Subject: [PATCH 46/48] mkcscope: Add option to create a blank config file --- mkcscope | 58 +++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 7 deletions(-) diff --git a/mkcscope b/mkcscope index 334f988..c8c202e 100755 --- a/mkcscope +++ b/mkcscope @@ -26,8 +26,9 @@ # files ncurses *.[ch] APP=$(basename $0) -VERSION='1.0.0' +VERSION='1.0.1' MAKEFILE=/dev/null +CONFFILE='.mkcscope.conf' declare -a TARGETS=() @@ -41,6 +42,7 @@ usage() echo " -h Show this help screen" echo " -l List targets" echo " -r Rebuild cscope DB without regenerating files" + echo " -c Create a blank config file in the current directory" echo } @@ -94,21 +96,57 @@ gen_targets() { set -f - if [[ -r "$PWD/.mkcscope.conf" ]] + if [[ -r "$PWD/$CONFFILE" ]] then - source "$PWD/.mkcscope.conf" - elif [[ -r "$HOME/.mkcscope.conf" ]] + source "$PWD/$CONFFILE" + elif [[ -r "$HOME/$CONFFILE" ]] then - source "$HOME/.mkcscope.conf" + source "$HOME/$CONFFILE" else echo "Unable to find a configuration file!" >&2 - echo "Expect to find a .mkcscope.conf in either of:" >&2 + echo "Expect to find a $CONFFILE in either of:" >&2 echo " $PWD" >&2 echo " $HOME" >&2 exit 1 fi } +gen_config() +{ + (cat <<-EOM +# Configuration for mkcscope +# This is essentially a Bash script, using a structured language as follows: +# target foo bar +# desc Description for foo +# files +# +# The above segment shows a sample target 'foo', which depends on a different +# target 'bar'. The desc line is a description for use in the help, while the +# files command specifies a directory relative to the current directory and +# the file patterns to search for and add to the cscope file list. The script +# expects to run from the base folder for the project workspace, and the +# directories in the files command are relative to this base folder. + +# Sample configuration +# target foo bar +# desc Target 'foo' depends on 'bar' +# # Files are in the folder foo relative to \$PWD +# files foo *.[ch] *.cpp +# +# target bar baz +# desc Target 'bar' depends on 'baz' +# # Multiple files +# files bar/folder1 *.s +# files bar/folder2 *.h +# +# target baz +# desc Target 'baz' does not depend on anything +# files baz/*.c + +EOM + ) > "$PWD/$CONFFILE" +} + cleanup() { rm $MAKEFILE @@ -119,7 +157,7 @@ trap cleanup "EXIT" gen_makefile gen_targets -while getopts "hlr" OPTION +while getopts "hlrc" OPTION do case $OPTION in h) @@ -138,6 +176,12 @@ do exit 0 ;; + c) + echo "Creating blank configuration" + gen_config + exit 0 + ;; + \?) echo "Invalid option -$OPTARG" exit 1 From a1dbe3c5d0235df31d7119309e6ad27bbdb8c018 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sat, 26 Mar 2016 13:17:11 -0700 Subject: [PATCH 47/48] Replace smartwd with python equivalent --- smartwd | 90 +++++++++++++++++++++++++-------------------------------- 1 file changed, 39 insertions(+), 51 deletions(-) diff --git a/smartwd b/smartwd index 4365752..47c148b 100755 --- a/smartwd +++ b/smartwd @@ -1,11 +1,8 @@ -#!/usr/bin/env ruby -# -*- ruby -*- +#!/usr/bin/env python # Script to smartly chop off portions of the current working directory for # use in the shell prompt -# Some of the methods don't work with Ruby versions older than 1.8.7 -min_release = "1.8.7 (2008-08-11)" -ruby_release = "#{RUBY_VERSION} (#{RUBY_RELEASE_DATE})" +import os # Constants used to decide when to start chopping DIRLIM = 5 @@ -13,57 +10,48 @@ DIR_L = 3 DIR_R = 2 PWDLEN = 14 -def backslash_w - homedir = ENV['HOME'] - pwd = ENV['PWD'] - print pwd.gsub(homedir, '~') -end +def smartwd(): + username = os.environ['USER'] + homedir = os.environ['HOME'] + pwd = os.environ['PWD'] -def smartwd - username = ENV['USER'] - homedir = ENV['HOME'] - pwd = ENV['PWD'] + path = pwd.split('/') - path = pwd.split('/') + # Ignore the root path + if len(path) == 1: + return pwd - # Ignore the root path - if (path.length > 0) - index = path.index(username) - if index - prefix = path.shift(index + 1) + try: + username_index = path.index(username) + except ValueError: + username_index = None - # We need to map additional paths in environments where the user - # may have more than one available folder in his/her name. - if prefix.join('/') == homedir - path.unshift('~') - else - # The first entry in the prefix array is the empty string - pre = prefix[1].split('').shift(4).join('') + '~' - path.unshift(pre) - end - else - # Replace the first two entries in the array with / - # (because the first entry is always empty string) - prefix = path.shift(2).join('/') - path.unshift(prefix) - end + if username_index is not None: + prefix = '/'.join(path[:username_index+1]) - # Check to make sure that both the DIRLIM and PWDLEN constraints - # are met before splitting - pwd = path.join('/') - if path.length > DIRLIM and pwd.length > PWDLEN - pwd_l = path.shift(DIR_L).join('/') - pwd_r = path.pop(DIR_R).join('/') - pwd = pwd_l + '/.../' + pwd_r - end - end + if prefix == homedir : + pre_path = '~' + else: + # The first element is always the empty string. + # We want the first 4 characters of the second element + pre_path = path[1][:4] + '~' - print pwd -end + del path[:username_index] + path[0] = pre_path -if (ruby_release < min_release) - backslash_w -else - smartwd -end + pwd = '/'.join(path) + + # If we exceed the dirlimit and the length of the joined pwd, + # replace the pwd with left and right elements, with ellipses + # in between to simulate a long path. + if len(path) > DIRLIM and len(pwd) > PWDLEN: + newpwd = '/'.join(path[:DIR_L] + ['...'] + path[-DIR_R:]) + + if len(newpwd) < len(pwd): + pwd = newpwd + + return pwd + +if __name__ == "__main__": + print smartwd() From 0e3760e65e20817dab6c03efee9ef545c80eed69 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Mon, 3 Oct 2016 13:03:29 -0700 Subject: [PATCH 48/48] Add newsh script --- newsh | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100755 newsh diff --git a/newsh b/newsh new file mode 100755 index 0000000..f9100aa --- /dev/null +++ b/newsh @@ -0,0 +1,140 @@ +#!/bin/bash +# Script to create a new shell script and open it for editing + +APP=$(basename $0) + +####################################################################### +# Set default options +####################################################################### +NEWSH_SCRIPT_TYPE=bash +NEWSH_USE_ABS_PATH=0 +NEWSH_NO_EXEC_FLAG=0 +NEWSH_TRUNC_SCRIPT=0 +NEWSH_SCRIPT_FILE= + +####################################################################### +# Usage information +####################################################################### +usage() +{ + cat <<-EOM +Usage: $APP OPTIONS + +OPTIONS + -t Type of script to create (bash/python/ruby/expect) + -a Use absolute path to binary instead of the env wrapper + -x Don't make the file executable + -f Overwrite the script file if it already exists + -h Display this help message + +$APP will abort if the specified filename already exists +EOM +} + +####################################################################### +# Get path to binary +####################################################################### +get_binary_path() +{ + local binary=$1 + local env_path=$(which env) + local bin_path=$(which $binary) + + if [[ -z "$bin_path" ]] + then + echo "$APP: fatal: Cannot find $binary" >&2 + exit 1 + fi + + case "$NEWSH_USE_ABS_PATH" in + 0) + # Use env as the path specifier + NEWSH_BINARY_PATH="$env_path $binary" + ;; + + 1) + # Use absolute path to the binary as the path specifier + NEWSH_BINARY_PATH="$bin_path" + ;; + + *) + echo "$APP: fatal: Corrupted internal state!" >&2 + echo "$APP: USE_ABS_PATH=$NEWSH_USE_ABS_PATH" >&2 + exit 2 + ;; + esac +} + + +####################################################################### +# Verify command line switches +####################################################################### +while getopts "t:axfh" OPTION +do + case "$OPTION" in + h) + usage + exit 0 + ;; + + t) + NEWSH_SCRIPT_TYPE=$OPTARG + ;; + + a) + NEWSH_USE_ABS_PATH=1 + ;; + + x) + NEWSH_NO_EXEC_FLAG=1 + ;; + + f) + NEWSH_TRUNC_SCRIPT=1 + ;; + + \?) + exit 1 + ;; + + :) + echo "$APP: Option -$OPTARG requires an argument" >&2 + exit 1 + ;; + esac +done + +shift $((OPTIND - 1)) +NEWSH_SCRIPT_FILE=$1 + +if [[ -z "$NEWSH_SCRIPT_FILE" ]] +then + echo "$APP: fatal: Missing script file!" >&2 + exit 1 +fi + +if [[ -e "$NEWSH_SCRIPT_FILE" ]] +then + if [[ "$NEWSH_TRUNC_SCRIPT" == "0" ]] + then + echo "$APP: fatal: Existing script file $NEWSH_SCRIPT_FILE" >&2 + exit 1 + elif [[ "$NEWSH_TRUNC_SCRIPT" == "1" ]] + then + echo "$APP: overwriting existing script file $NEWSH_SCRIPT_FILE" >&2 + fi +fi + +get_binary_path $NEWSH_SCRIPT_TYPE +(cat <<-EOF +#!$NEWSH_BINARY_PATH +# Autogenerated by $APP on $(date +%F) at $(date +%T%z) + +EOF +) > "$NEWSH_SCRIPT_FILE" + +# Check and make file executable +if [[ "$NEWSH_NO_EXEC_FLAG" == "0" ]] +then + chmod +x "$NEWSH_SCRIPT_FILE" +fi