FIXED: oops - the new suptopic feature confused the commandline-mode of

note! quickly corrected! so subtopics also available from command-
                line.
FIXED:          a small bug fiyed, it was impossible to use -D or -I from command-
                line, if $ALWAYS_INT was turned on, now it is.
FIXED:          fixed problem with local/global variable $time, which confused
                the script under certain circumstances, now $time is no more global,
                it will be read in (using &getdate) locally by &new and &edit.
CHANGED:        The Topic separator is no longer hardcoded, one can customize
                it using the $TopicSep variable, the default is now /, the backslash
                will no mor work!
CHANGED:        use perl buildin localtime() function instead of
                GNU date, which is possibly not installed on every target
                system (i.e. win32), therefore better portability!
CHANGED:        use now the strict module
ADDED:          Support for subtopics added (and sub-sub-..-topics).
CHANGED:        Removed the "T" command, it is now obsolete.
CHANGED:        behavior of list command changed, now shows topics as well as
                notes under the current topic(if there are some).
CHANGED:        The ".." command takes you now one level higher in your topic-
                structure.
ADDED:          A new config option $PreferredEditor, which you can use to
                specify your own choice of editor.
FIXED:          A bug at line 769 causing single note where smaller than note-
                listings
This commit is contained in:
TLINDEN
2012-02-10 20:11:22 +01:00
parent 6c5db55b5e
commit 142ff12b12
7 changed files with 636 additions and 320 deletions

View File

@@ -1,4 +1,20 @@
#!/usr/bin/perl
# $Author: tom $ $Id: note.mysql,v 1.5 2000/02/25 20:59:30 tom Exp tom $ $Revision: 1.5 $
#
# $Log: note.mysql,v $
# Revision 1.5 2000/02/25 20:59:30 tom
# corrected small timestamp problem in &edit and &new
#
# Revision 1.4 2000/02/25 13:24:11 tom
# fixed a small bug, that caused to use the last line for a note title instead the 2nd.
#
# Revision 1.3 2000/02/25 11:28:53 tom
# all changes from bin version applied to sql version
#
# Revision 1.2 2000/02/25 10:30:06 tom
# *** empty log message ***
#
#
# this is the small console program "note" (MYSQL version)
# It works similar to some well known GUI note programs,
# but instead of using X11 it uses the UN*X console.
@@ -17,6 +33,38 @@
# note is GPL software.
use Mysql;
use strict;
use Data::Dumper;
sub usage;
sub find_editor;
sub output;
sub C;
sub uen;
sub ude;
sub num_bereich;
sub getdate;
sub new;
sub edit;
sub del;
sub display;
sub list;
sub help;
sub import;
my (
$maxlen, $timelen, $TOPIC, $TYPE, $mode, $NOTEDB,
$version, $number, $CurTopic, $CurDepth, $PATH, $CONF,
$sizeof, $MAX_TIME, $PreferredEditor, %TP, $TopicSep,
$ListType, $searchstring, $dump_file, $ALWAYS_INT,
$BORDERC, $BORDER_COLOR, $_BORDERC, $NOTEC, $NOTE_COLOR,
$NUMC, $NUM_COLOR, $_NUMC, $_NOTEC, $TIMEC, $TIME_COLOR,
$_TIMEC, $TOPICC, $TOPIC_COLOR, $_TOPICC, $SetTitle, $COLOR,
$typedef, $MAX_NOTE, $MAX_TIME, @NumBlock, $ALWAYS_EDIT, $HOME,
$db, $dbname, $dbhost, $DEFAULTDBNAME, $dbuser, $USER, $dbpasswd,
$table, $fnum, $fnote, $fdate, $date
);
##################################
# define some default values.
@@ -25,8 +73,7 @@ use Mysql;
$maxlen = 20;
$timelen = 22;
$date = `date +%e\".\"%m\".\"%Y\" \"%T`;
chomp $date;
$date = &getdate;
$USER = getlogin || getpwuid($<);
chomp $USER;
@@ -60,27 +107,18 @@ $TOPIC_COLOR = "BLACK";
# Turns Topic Support on
$TOPIC = 1;
# Default topic separator: \
$TopicSep = '/';
$version = "0.6 (mysql database)";
if($TOPIC)
{
$CurDepth = 1; # the current depth inside the topic "directory" structure...
}
###################################
sub usage;
sub find_editor;
sub output;
sub C;
sub uen;
sub ude;
sub num_bereich;
sub new;
sub edit;
sub del;
sub display;
sub list;
sub help;
sub import;
$version = "0.5 (mysql database)";
# process command line args
if($ARGV[0] eq "")
{
@@ -110,7 +148,9 @@ else
elsif($ARGV[0] eq "-l" || $ARGV[0] eq "--list")
{
$mode = "list";
$CurTopic = $ARGV[1];
my @ArgTopics = split /$TopicSep/,$ARGV[1];
$CurDepth += $#ArgTopics + 1 if $ARGV[1];
$CurTopic = $ArgTopics[$#ArgTopics]; # use the last element everytime...
$ARGV[0] = "";
}
elsif($ARGV[0] eq "-L" || $ARGV[0] eq "--longlist")
@@ -205,7 +245,7 @@ if(-e $CONF)
# Always interactive?
if($ALWAYS_INT eq "YES")
if($ALWAYS_INT eq "YES" && $mode ne "dump" && $mode ne "import")
{
$mode = "interactive";
}
@@ -223,8 +263,6 @@ $_TIMEC = "</$TIME_COLOR>";
$TOPICC = "<$TOPIC_COLOR>";
$_TOPICC = "</$TOPIC_COLOR>";
$time = `date +%d\".\"%m\".\"%Y\" \"%T`;
chomp $time;
if($ListType ne "LONG" && $mode ne "interactive")
@@ -233,6 +271,7 @@ if($ListType ne "LONG" && $mode ne "interactive")
}
# ok, if still here, we got it all, now let's connect to the database
$db = Mysql->connect($dbhost,$dbname,$dbuser,$dbpasswd)
or die "ERROR: $Mysql::dberrstr\n";
@@ -290,6 +329,7 @@ exit(0);
############################### DISPLAY ##################################
sub display
{
my($N,$address,$buffer,$n,$t,$match,$note,$time,$num,@row,$res);
# display a certain note
print "\n";
&num_bereich; # get @NumBlock from $numer
@@ -312,6 +352,7 @@ sub display
############################### SEARCH ##################################
sub search
{
my($n,$t,$match,$note,$time,$num,$buffer,@row,$res, $sqlstatement);
$maxlen += $timelen;
if($searchstring eq "")
{
@@ -343,26 +384,16 @@ sub search
############################### LIST ##################################
sub list
{
my(@topic,@RealTopic, $i,$buffer,$t,$n,$num,$note,$time,@CurItem,$top,$in, $res, @row);
if($mode ne "interactive")
{
if($TOPIC && $CurTopic eq "")
{
print "List of all available topics:\n\n";
}
elsif($TOPIC && $CurTopic ne "")
{
print "List of all notes under topic \"$CurTopic\":\n\n";
}
else
{
print "List of all existing notes:\n\n";
}
print "List of all existing notes:\n\n";
}
# list all available notes (number and firstline)
$res = $db->query("SELECT $fnum,$fnote,$fdate FROM $table ORDER BY $fnum")
or die "ERROR: $Mysql::dberrstr\n";
if($TOPIC && $CurTopic eq "")
if($TOPIC)
{
undef %TP;
}
@@ -373,57 +404,87 @@ sub list
$num = $row[0]; $n = $row[1]; $t = $row[2];
if($TOPIC)
{
@topic = split(/\\/,$n); # this allows us to have multiple topics (subtopics!)
if($CurTopic eq "")
# this allows us to have multiple topics (subtopics!)
my ($firstline,$dummy) = split /\n/, $n, 2;
if($firstline =~ /^($TopicSep)/)
{
# looks like: "\topic\"
# collect:
if($topic[1] eq "")
{
$topic[1] = "default";
}
if(exists $TP{$topic[1]})
{
$TP{$topic[1]}++;
}
else
{
$TP{$topic[1]} = 1;
}
@topic = split(/$TopicSep/,$firstline);
}
else
{
if($topic[1] eq $CurTopic)
@topic = ();
}
# looks like: "\topic\"
# collect a list of topics under the current topic
if($topic[$CurDepth-1] eq $CurTopic && $topic[$CurDepth] ne "")
{
if(exists $TP{$topic[$CurDepth]})
{
# cut the topic from the note-text
if($n =~ /^\\$CurTopic\\\n*/)
{
$n = $';
}
output($num, $n, $t);
$TP{$topic[$CurDepth]}++;
}
elsif($topic[1] eq "" && $CurTopic eq "default")
else
{
output($num, $n, $t);
# only if the next item *is* a topic!
$TP{$topic[$CurDepth]} = 1 if(($CurDepth) <= $#topic);
}
}
}
elsif($topic[$CurDepth-1] eq $CurTopic || ($topic[$CurDepth] eq "" && $CurDepth ==1))
{
# cut the topic off the note-text
if($n =~ /^($TopicSep)/)
{
$CurItem[$i]->{'note'} = $dummy;
}
else
{
$CurItem[$i]->{'note'} = $n;
}
# save for later output() call
$CurItem[$i]->{'num'} = $num;
$CurItem[$i]->{'time'} = $t;
$i++;
# use this note for building the $PATH!
if($RealTopic[0] eq "")
{
@RealTopic = @topic;
}
}
}
else
{
output($num, $n, $t);
}
}
if($TOPIC && $CurTopic eq "")
{
# we are at top level, print a list of topics...
foreach $top (sort(keys %TP))
{
print C " => " . $BORDERC. "[" . $_BORDERC . $TOPICC
. $top . $_TOPICC . $BORDERC . "]"
.$_BORDERC ." ($TP{$top} notes)\n";
}
}
if($TOPIC)
{
if($CurTopic ne "")
{
undef $PATH;
foreach (@RealTopic)
{
$PATH .= $_ . $TopicSep;
last if($_ eq $CurTopic);
}
}
else
{
$PATH = $TopicSep;
}
# we are at top level, print a list of topics...
foreach $top (sort(keys %TP))
{
output("-", " => ". $top . "$TopicSep ($TP{$top} notes)",
" Sub Topic ");
}
#print Dumper(@CurItem);
for($in=0;$in<$i;$in++)
{
output( $CurItem[$in]->{'num'},
$CurItem[$in]->{'note'},
$CurItem[$in]->{'time'} );
}
}
print "\n";
}
@@ -431,6 +492,8 @@ sub list
############################### NEW ##################################
sub new
{
my($TEMP,$editor, $time, $note, $WARN, $c, $line, $num, $te, $me, $buff,$buffer, @topic,$n,$t,$res, @row,$sqlstatement);
$time = &getdate;
if($ALWAYS_EDIT eq "YES")
{
$TEMP = "/tmp/note.$$";
@@ -458,7 +521,7 @@ sub new
$c = 0;
while(<E>)
{
$_ =~ s/'/\\'/g;
$_ =~ s/'/`/g;
$note = $note . $_;
}
chomp $note;
@@ -469,8 +532,8 @@ sub new
else
{
$note = "";
local $line = "";
#local $num = 0;
$line = "";
#$num = 0;
# create a new note
print "enter the text of the note, end with .\n";
do
@@ -493,10 +556,10 @@ sub new
$number++;
if($TOPIC && $CurTopic ne "")
{
@topic = split(/\\/,$note);
@topic = split(/$TopicSep/,$note);
if($topic[1] eq "")
{
$note = "\\$CurTopic\\\n$note";
$note = $PATH . "\n$note";
}
}
@@ -504,7 +567,6 @@ sub new
$note =~ s/\\/\\\\/g;
$sqlstatement = "INSERT INTO $table VALUES ($number,'$note','$date')";
$db->query($sqlstatement)
or die "ERROR: $Mysql::dberrstr\n";
@@ -516,21 +578,22 @@ sub new
############################### DELETE ##################################
sub del
{
my($i,@count, $setnum, $buff, %Merk, $num, $note, $pos, $droped, $buffer, $sqlstatement,$res, @row, $ERR);
# delete a note
&num_bereich; # get @NumBlock from $number
foreach $N (@NumBlock)
foreach $_ (@NumBlock)
{
$sqlstatement = "DELETE FROM $table WHERE $fnum = $N";
$sqlstatement = "DELETE FROM $table WHERE $fnum = $_";
$res = $db->query($sqlstatement) or $ERR = $Mysql::dberrstr;
# do not exit if an error occurs - good for int mode!
if($ERR)
{
print "no note with number $N found!\n";
print "no note with number $_ found!\n";
}
else
{
print "note number $N has been deleted.\n";
print "note number $_ has been deleted.\n";
}
}
# recount the notenumbers:
@@ -555,7 +618,9 @@ sub del
############################### EDIT ##################################
sub edit
{
my($time, $editor, $TEMP, $address, $n, $buff, $c, $note, $t, $buffer, $num, $sqlstatement, $res, @row, $match);
# edit a note
$time = &getdate;
$sqlstatement = "SELECT $fnote FROM $table WHERE $fnum = $number";
$res = $db->query($sqlstatement) or die "ERROR: $Mysql::dberrstr\n";
while(@row = $res->fetchrow)
@@ -590,7 +655,7 @@ sub edit
$c = 0;
while(<NOTE>)
{
$_ =~ s/'/\\'/g;
$_ =~ s/'/`/g;
$note = $note . $_;
}
chomp $note;
@@ -603,7 +668,8 @@ sub edit
# we got it, now save to db
$sqlstatement = "UPDATE $table SET "
. "$fnote = '$note' "
. "$fnote = '$note', "
. "$fdate = '$time' "
. "WHERE $fnum = $number";
$db->query($sqlstatement) or die "ERROR: $Mysql::dberrstr\n";
@@ -613,6 +679,7 @@ sub edit
sub dump
{
my($buffer,$num, $note, $time,$n, $t, $res, @row);
# $dump_file
open (DUMP, ">$dump_file") or die "could not open $dump_file\n";
select DUMP;
@@ -633,10 +700,11 @@ sub dump
sub import
{
my($buff, $num,$te, $me, $start, $complete, $dummi, $n, $t, $buffer, $note, $time, $date);
# open $dump_file and import it into the notedb
open (DUMP, "<$dump_file") or die "could not open $dump_file\n";
local $complete=0;
local $start = 0;
$complete=0;
$start = 0;
while(<DUMP>)
{
chomp $_;
@@ -684,47 +752,51 @@ sub import
sub interactive
{
my($maxlen_save, $B, $BB, $menu, $char, @LastTopic);
$maxlen_save = $maxlen;
# create menu:
local $B = "<blackI>";
local $BB = "</blackI>";
local $menu = "[" . $B . "L" . $BB . " List "
$B = "<blackI>";
$BB = "</blackI>";
$menu = "[" . $B . "L" . $BB . " List "
. $B . "N" . $BB . " New "
. $B . "D" . $BB . " Delete "
. $B . "S" . $BB . " Search "
. $B . "E" . $BB . " Edit ";
if($TOPIC)
{
$menu .= $B . "T" . $BB . " Topic ";
}
$menu .= $B . "?" . $BB . " Help "
. $B . "E" . $BB . " Edit "
. $B . "?" . $BB . " Help "
. $B . "Q" . $BB . " Quit] "; # $CurTopic will be empty if $TOPIC is off!
# per default let's list all the stuff:
# Initially do a list command!
$maxlen += $timelen;
print "\n";
&list;
undef $SetTitle;
for(;;)
{
$time = `date +%d\".\"%m\".\"%Y\" \"%T`;
chomp $time;
$ListType = "";
$maxlen = $maxlen_save;
print C $menu . $TOPICC . $CurTopic . $_TOPICC . ">";
if($CurDepth > 2)
{
print C $menu . $TOPICC . "../" . $CurTopic . $_TOPICC . ">";
}
else
{
print C $menu . $TOPICC . $CurTopic . $_TOPICC . ">";
}
# endless until user press "Q" or "q"!
$char = <STDIN>;
chomp $char;
if($char =~ /^\d+/)
{
# display notes
$maxlen += $timelen;
$number = $char;
&display;
undef $SetTitle;
}
elsif($char =~ /^n$/i)
{
# create a new one
$time = `date +%d\".\"%m\".\"%Y\" \"%T`;
chomp $time;
&new;
}
elsif($char =~ /^l$/ || $char =~ /^$/)
@@ -766,8 +838,6 @@ sub interactive
elsif($char =~ /^e\s+(\d+\-*\,*\d*)/i)
{
# edit one!
$time = `date +%d\".\"%m\".\"%Y\" \"%T`;
chomp $time;
$number = $1;
&edit;
}
@@ -803,19 +873,10 @@ sub interactive
print "\n\ngood bye\n";
exit(0);
}
elsif($char =~ /^t$/i && $TOPIC)
{
$SaveTopic = $CurTopic;
$CurTopic = "";
#$maxlen += $timelen;
print "\n";
&list;
$CurTopic = $SaveTopic;
undef $SetTitle;
}
elsif($char =~ /^\.\.$/)
{
$CurTopic = "default";
$CurDepth-- if ($CurDepth > 1);
$CurTopic = $LastTopic[$CurDepth];
$maxlen += $timelen;
print "\n";
&list;
@@ -826,8 +887,10 @@ sub interactive
# unknown
if(exists $TP{$char})
{
$LastTopic[$CurDepth] = $CurTopic;
$CurTopic = $char;
$maxlen += $timelen;
$CurDepth++;
print "\n";
&list;
undef $SetTitle;
@@ -877,11 +940,13 @@ Options:
exit 1;
}
sub find_editor {
return $ENV{"VISUAL"} || $ENV{"EDITOR"} || "vim" || "vi" || "pico";
return $PreferredEditor || $ENV{"VISUAL"} || $ENV{"EDITOR"} || "vim" || "vi" || "pico";
}
#/
sub output
{
my($SSS, $LINE, $num, $note, $time, $TYPE, $L, $LONGSPC, $R, $PathLen, $SP, $title, $CUTSPACE,
$len, $diff, $Space);
# 0 = Num, 1 = Note, 2 = Time
if($ListType ne "LONG")
{
@@ -891,27 +956,28 @@ sub output
{
$SSS = "-" x ($maxlen + 31);
}
local $LINE = "$BORDERC $SSS $_BORDERC\n";
local $num = $_[0];
local $note = $_[1];
local $time = $_[2];
local $TYPE = $_[3];
local $L = $BORDERC . "[" . $_BORDERC;
local $LONGSPC = " " x (22 + 3);
local $R = $BORDERC . "]" . $_BORDERC;
$LINE = "$BORDERC $SSS $_BORDERC\n";
$num = $_[0];
$note = $_[1];
$time = $_[2];
$TYPE = $_[3];
$L = $BORDERC . "[" . $_BORDERC;
$LONGSPC = " " x (22 + 3);
$R = $BORDERC . "]" . $_BORDERC;
$PathLen = length($PATH); # will be ZERO, if not in TOPIC mode!
if($TYPE ne "SINGLE")
{
if(!$SetTitle)
{
local $SP = "";
$SP = "";
# print only if it is the first line!
if($ListType ne "LONG")
{
$SP = " " x ($maxlen-2 - $timelen);
$SP = " " x ($maxlen-2 - $timelen - $PathLen);
}
else
{
$SP = " " x ($maxlen-2);
$SP = " " x ($maxlen-2 - $PathLen);
}
print C $LINE;
@@ -924,21 +990,35 @@ sub output
{
print $LONGSPC;
}
print C $NOTEC . "note$_NOTEC$SP$R\n";
if($TOPIC)
{
print C $TOPICC . "$PATH $_TOPICC$SP$R\n";
}
else
{
print C $NOTEC . "note$_NOTEC$SP$R\n";
}
print C $LINE;
$SetTitle = 1;
}
local $title = "";
$title = "";
$CUTSPACE = " " x $maxlen;
$note =~ s/\n/$CUTSPACE/g;
local $len = length($note);
$len = length($note);
if($len < $maxlen-3)
{
local $diff = $maxlen - $len;
local $Space = " " x $diff;
$title = $BORDERC . $NOTEC . "\"" . $note . "\"" . $_NOTEC . $Space . "$_BORDERC";
$diff = $maxlen - $len;
$Space = " " x $diff;
if($num eq "-")
{
$title = $BORDERC . $TOPICC . "\"" . $note . "\"" . $_TOPICC . $Space . "$_BORDERC";
}
else
{
$title = $BORDERC . $NOTEC . "\"" . $note . "\"" . $_NOTEC . $Space . "$_BORDERC";
}
}
else
{
@@ -959,8 +1039,8 @@ sub output
else
{
chomp $note;
local $Space = " " x ($maxlen - 16);
local $SP = " " x ($maxlen + 13);
$Space = " " x ($maxlen - 16);
$SP = " " x ($maxlen + 13);
#print C $LINE;
#print C "$L $NUMC#$_NUMC " . $TIMEC . "creation date$_TIMEC$SP$R\n";
print C $LINE;
@@ -976,8 +1056,9 @@ sub output
sub C
{
my(%Color, $default, $S, $Col, $NC, $T);
# \033[1m%30s\033[0m
local %Color = ( 'black' => '0;30',
%Color = ( 'black' => '0;30',
'red' => '0;31',
'green' => '0;32',
'yellow' => '0;33',
@@ -1036,22 +1117,25 @@ sub C
sub uen
{
local $T = pack("u", $_[0]);
my($T);
$T = pack("u", $_[0]);
chomp $T;
return $T;
}
sub ude
{
local $T = unpack("u", $_[0]);
my($T);
$T = unpack("u", $_[0]);
return $T;
}
sub num_bereich
{
my($m,@LR,@Sorted_LR,$i);
# $number is the one we want to delete!
# But does it contain kommas?
local $m = 0;
$m = 0;
if($number =~ /\,/)
{
# accept -d 3,4,7
@@ -1060,8 +1144,8 @@ sub num_bereich
elsif($number =~ /^\d+\-\d+$/)
{
# accept -d 3-9
local @LR = split(/\-/,$number);
local @Sorted_LR = ();
@LR = split(/\-/,$number);
@Sorted_LR = ();
if($LR[0] > $LR[1])
{
@@ -1091,6 +1175,19 @@ sub num_bereich
}
sub getdate
{
my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year += 1900;
$mon += 1;
$mon =~ s/^(\d)$/0$1/;
$hour =~ s/^(\d)$/0$1/;
$min =~ s/^(\d)$/0$1/;
$sec =~ s/^(\d)$/0$1/;
$mday =~ s/^(\d)$/0$1/;
return "$mday.$mon.$year $hour:$min:$sec";
}
sub help
{

View File

@@ -4,3 +4,5 @@ CREATE TABLE note (
date text,
PRIMARY KEY (number)
);
# sample grant statement:
#GRANT ALL PRIVILEGES ON tom_note TO tom@localhost IDENTIFIED BY 'password';