Take notes on the command line

From Rosetta Code
Task
Take notes on the command line
You are encouraged to solve this task according to the task description, using any language you may know.
Take notes on the command line is part of Short Circuit's Console Program Basics selection.

Take notes on command line

NOTES.CMD is a commandline tool written in DOS Batch language. The task is to implement the same in your language.

Invoking NOTES without commandline arguments displays the current contents of the local NOTES.TXT if it exists. If NOTES has arguments, the current date and time are appended to the local NOTES.TXT followed by a newline. Then all the arguments, joined with a space, and prepended with a tab, are written to NOTES.TXT

DOS Batch File

<lang dos>@echo off if %1@==@ ( if exist notes.txt more notes.txt goto done ) echo %date% %time%:>>notes.txt echo %*>>notes.txt

done</lang>

Factor

<lang factor>#! /usr/bin/env factor USING: kernel calendar calendar.format io io.encodings.utf8 io.files sequences command-line namespaces ;

command-line get [

   "notes.txt" utf8 file-contents print

] [

   " " join "\t" prepend
   "notes.txt" utf8 [
       now timestamp>ymdhms print
       print flush
   ] with-file-appender

] if-empty</lang>

Python

<lang python>import sys, datetime

  1. sys.argv[1:] = 'go for it'.split()

if len(sys.argv) == 1:

   try:
       f = open('notes.txt', 'r')
       sys.stdout.write(f.read())
       f.close()
   except IOError:
       pass

else:

   f = open('notes.txt', 'a')
   f.write(datetime.datetime.now().isoformat() + '\n')
   f.write("\t%s\n" % ' '.join(sys.argv[1:]))
   f.close()</lang>

Sample notes.txt file

After assorted runs:

2010-04-01T17:06:20.312000
	go for it
2010-04-01T17:08:20.718000
	go for it

Ruby

<lang ruby>notes = 'NOTES.TXT' if ARGV.empty?

 File.copy_stream(notes, $stdout) rescue nil

else

 File.open(notes, 'a') {|file| file.puts "%s\n\t%s" % [Time.now, ARGV.join(' ')]}

end</lang>

Tcl

<lang tcl># Make it easier to change the name of the notes file set notefile notes.txt

if {$argc} {

   # Write a message to the file
   set msg [clock format [clock seconds]]\n\t[join $argv " "]
   set f [open $notefile a]
   puts $f $msg
   close $f

} else {

   # Print the contents of the file
   catch {

set f [open $notefile] fcopy $f stdout close $f

   }

}</lang> Sample output after a few runs:

Thu Apr 01 19:28:07 BST 2010
	test 1 two three
Thu Apr 01 19:28:49 BST 2010
	TODO: Submit notes.tcl to Rosetta Code