Take notes on the command line: Difference between revisions

From Rosetta Code
Content added Content deleted
Line 555: Line 555:
=={{header|REBOL}}==
=={{header|REBOL}}==
<lang REBOL>REBOL [
<lang REBOL>REBOL [
Title: "Notes"
Title: "Notes"
Author: oofoe
Author: oofoe
Date: 2010-10-04
Date: 2010-10-04
URL: http://rosettacode.org/wiki/Take_notes_on_the_command_line
URL: http://rosettacode.org/wiki/Take_notes_on_the_command_line
]
]


Line 564: Line 564:


either any [none? args: system/script/args empty? args] [
either any [none? args: system/script/args empty? args] [
if exists? notes [print read notes]
if exists? notes [print read notes]
] [
] [
write/binary/append notes rejoin [now lf tab args lf]
write/binary/append notes rejoin [now lf tab args lf]
]</lang>
]</lang>



Revision as of 01:55, 5 October 2010

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 as a Batch file. 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 spaces, prepended with a tab, and appended with a trailing newline, are written to NOTES.TXT. If NOTES.TXT doesn't already exist in the current directory then a new NOTES.TXT file should be created.

AutoHotkey

<lang autohotkey>Notes := "Notes.txt"

If 0 = 0 ; no arguments {

   If FileExist(Notes) {
       FileRead, Content, %Notes%
       MsgBox, %Content%
   } Else
       MsgBox, %Notes% does not exist
   Goto, EOF

}

date and time, colon, newline (CRLF), tab

Date := A_DD "/" A_MM "/" A_YYYY Time := A_Hour ":" A_Min ":" A_Sec "." A_MSec FileAppend, %Date% %Time%:`r`n%A_Tab%, %Notes%

command line parameters, trailing newline (CRLF)

Loop, %0%

   FileAppend, % %A_Index% " ", %Notes%

FileAppend, `r`n, %Notes%

EOF:</lang>

Batch File

Works with: Windows NT version 4

<lang dos>@echo off if %1@==@ (

   if exist notes.txt more notes.txt
   goto :eof

) echo %date% %time%:>>notes.txt echo %*>>notes.txt</lang>

C

<lang c>#include <stdio.h>

  1. include <time.h>
  1. define note_file "NOTES.TXT"

int main(int argc, char**argv) { FILE *note; time_t timer; int i; char ch;

if(argc==1) { //Read NOTES.TXT and print it note = fopen(note_file, "r"); if(note!=NULL) { while((ch=getc(note))!=EOF) putchar(ch); } } else { //Write date & time to NOTES.TXT, and then arguments note = fopen(note_file, "a"); if(note!=NULL) { timer = time(NULL); fprintf(note,"%s",asctime(localtime(&timer))); putc('\t', note); for(i=1;i<argc;i++) fprintf(note, "%s ", argv[i]); putc('\n', note); } }

if(note!=NULL) fclose(note); return 0; } </lang>

C++

<lang cpp>#include <fstream>

  1. include <iostream>
  2. include <ctime>

using namespace std;

  1. define note_file "NOTES.TXT"

int main(int argc, char **argv) { if(argc>1) { ofstream Notes(note_file, ios::app); time_t timer = time(NULL); if(Notes.is_open()) { Notes << asctime(localtime(&timer)) << '\t'; for(int i=1;i<argc;i++) Notes << argv[i] << ' '; Notes << endl; Notes.close(); } } else { ifstream Notes(note_file, ios::in); string line; if(Notes.is_open()) { while(!Notes.eof()) { getline(Notes, line); cout << line << endl; } Notes.close(); } } }</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>

Forth

The following is SwiftForth-specific, and runs in the Forth environment rather than as a standalone terminal application. Win32Forth only needs simple replacements of DATE and TIME . A Unix Forth would come closer to the 'commandline' requirement, as Windows Forths for whatever reason very eagerly discard the Windows console. Because this runs in the Forth environment, the otherwise acceptable application words are stuffed in a wordlist. Standard Forth doesn't have the notion of create-it-if-it-doesn't-exist, so OPEN just tries again with CREATE-FILE if OPEN-FILE fails.

<lang>vocabulary note-words get-current also note-words definitions

\ -- notes.txt variable file

open s" notes.txt" r/w open-file if
            s" notes.txt" r/w create-file throw then file ! ;
appending file @ file-size throw file @ reposition-file throw ;
write file @ write-file throw ;
close file @ close-file throw ;

\ -- SwiftForth console workaround 9 constant TAB

type ( c-addr u -- )
 bounds ?do
   i c@ dup TAB = if drop 8 spaces else emit then
 loop ;

\ -- dump notes.txt create buf 4096 allot

dump ( -- )
 cr begin buf 4096 file @ read-file throw dup while
   buf swap type
 repeat drop ;

\ -- time and date

time @time (time) ;
date @date (date) ;

\ -- add note

cr s\" \n" write ;
tab s\" \t" write ;
space s" " write ;
add-note ( c-addr u -- ) appending
 date write space time write cr
 tab ( note ) write cr ;

set-current

\ -- note

note ( "note" -- )
 open 0 parse dup if add-note
 else 2drop dump then close ;

previous</lang> The following is exactly the same program, but written in 4tH. 4tH has an I/O system which is different from ANS-Forth. The notes file is specified on the commandline. <lang>\ -- notes.txt include lib/argopen.4th include lib/ansfacil.4th

\ -- dump notes.txt 4096 buffer: buf

dump ( -- )
 input 1 arg-open
 begin buf dup 4096 accept dup while type repeat
 drop drop close ;

\ -- time and date

:00 <# # # [char] : hold #> type ;
-00 <# # # [char] - hold #> type ;
.time 0 .r :00 :00 ;
.date 0 .r -00 -00 ;

\ -- add note

add-note ( c-addr u -- )
 output append [+] 1 arg-open -rot
 time&date .date space .time cr
 9 emit type cr close ;

\ -- note

note ( "note" -- )
 argn 2 < abort" Usage: notes filename"
 refill drop 0 parse dup if add-note else 2drop dump then ;

note</lang>

Haskell

<lang haskell>import System.Environment (getArgs) import System.Time (getClockTime)

main :: IO () main = do

 args <- getArgs
 if null args
   then catch (readFile "notes.txt" >>= putStr)
              (\_ -> return ())
   else
     do ct <- getClockTime
        appendFile "notes.txt" $ show ct ++ "\n\t" ++ unwords args ++ "\n"</lang>

Sample output after a few runs:

Thu Apr 22 19:01:41 PDT 2010
	Test line 1.
Thu Apr 22 19:01:45 PDT 2010
	Test line 2.

HicEst

<lang HicEst>SYSTEM(RUN) ! start this script in RUN-mode

CHARACTER notes="Notes.txt", txt*1000

! Remove file name from the global variable $CMD_LINE: EDIT(Text=$CMD_LINE, Mark1, Right=".hic ", Right=4, Mark2, Delete)

IF($CMD_LINE == ' ') THEN

 READ(FIle=notes, LENgth=Lnotes)
 IF( Lnotes ) THEN
   WINDOW(WINdowhandle=hdl, TItle=notes)
   WRITE(Messagebox="?Y") "Finished ?"
 ENDIF

ELSE

 WRITE(Text=txt, Format="UWWW CCYY-MM-DD HH:mm:SS, A") 0, $CRLF//$TAB//TRIM($CMD_LINE)//$CRLF
 OPEN(FIle=notes, APPend)
 WRITE(FIle=notes, CLoSe=1) txt

ENDIF

ALARM(999) ! quit HicEst immediately</lang>

Thu 2010-09-16 18:42:15
	This is line 1

Thu 2010-09-16 18:42:23
	This is line 2

J

Solution:
<lang j>require 'files strings'

notes=: monad define

if. #y do.
  now=. LF ,~ 6!:0 'hh:mm:ss DD/MM/YYYY'
  'notes.txt' fappend~ now, LF ,~ TAB, ' ' joinstring y
elseif. -. _1 -: txt=. fread 'notes.txt' do.
  smoutput txt
end.

)

notes 2}.ARGV exit 0</lang>

Create a Shortcut called Notes that calls the script above using the Target:
"c:\program files\j602\bin\jconsole.exe" path\to\script\notes.ijs
and Start in: .\

Example Use:
In a Windows CMD session...

C:\Folder>Notes.lnk "Test line 1"

C:\Folder>Notes.lnk "Test" "line" "2"

C:\Folder>Notes.lnk Test line 3

C:\Folder>Notes.lnk
2010 5 21 11 31 30.389
        Test line 1
2010 5 21 11 31 50.669
        Test line 2
2010 5 21 11 32 14.895
        Test line 3

Java

<lang java>import java.io.*; import java.nio.channels.*; import java.util.Date;

public class TakeNotes {

   public static void main(String[] args) throws IOException {
       if (args.length > 0) {
           PrintStream ps = new PrintStream(new FileOutputStream("notes.txt", true));
           ps.println(new Date());
           ps.print("\t" + args[0]);
           for (int i = 1; i < args.length; i++)
               ps.print(" " + args[i]);
           ps.println();
           ps.close();
       } else {
           FileChannel fc = new FileInputStream("notes.txt").getChannel();
           fc.transferTo(0, fc.size(), Channels.newChannel(System.out));
           fc.close();
       }
   }

}</lang>

JavaScript

Works with: JScript

<lang javascript>var notes = 'NOTES.TXT';

var args = WScript.Arguments; var fso = new ActiveXObject("Scripting.FileSystemObject"); var ForReading = 1, ForWriting = 2, ForAppending = 8;

if (args.length == 0) {

   if (fso.FileExists(notes)) {
       var f = fso.OpenTextFile(notes, ForReading);
       WScript.Echo(f.ReadAll());
       f.Close();
   }

} else {

   var f = fso.OpenTextFile(notes, ForAppending, true);
   var d = new Date();
   f.WriteLine(d.toLocaleString());
   f.Write("\t");
   // note that WScript.Arguments is not an array, it is a "collection"
   // it does not have a join() method.
   for (var i = 0; i < args.length; i++) {
       f.Write(args(i) + " ");
   }
   f.WriteLine();
   f.Close();

}</lang>

> del NOTES.TXT
> cscript /nologo notes.js
> cscript /nologo notes.js this   is   the   first note
> cscript /nologo notes.js
April 1, 2010 5:18:38 PM
        this is the first note

Lua

<lang lua>filename = "NOTES.TXT"

if #arg == 0 then

   fp = io.open( filename, "r" )
   if fp ~= nil then
       print( fp:read( "*all*" ) )
       fp:close()
   end

else

   fp = io.open( filename, "a+" )
   fp:write( os.date( "%x %X\n" ) )
   fp:write( "\t" )
   for i = 1, #arg do

fp:write( arg[i], " " )

   end
   fp:write( "\n" )
   fp:close()

end</lang>

OCaml

<lang ocaml>#! /usr/bin/env ocaml

  1. load "unix.cma"

let notes_file = "notes.txt"

let take_notes() =

 let gmt = Unix.gmtime (Unix.time()) in
 let date =
   Printf.sprintf "%d-%02d-%02d %02d:%02d:%02d"
     (1900 + gmt.Unix.tm_year) (1 + gmt.Unix.tm_mon) gmt.Unix.tm_mday
     gmt.Unix.tm_hour gmt.Unix.tm_min gmt.Unix.tm_sec
 in
 let oc = open_out_gen [Open_append; Open_creat; Open_text] 0o644 notes_file in
 output_string oc (date ^ "\t");
 output_string oc (String.concat " " (List.tl(Array.to_list Sys.argv)));
 output_string oc "\n";

let dump_notes() =

 if not(Sys.file_exists notes_file)
 then (prerr_endline "no local notes found"; exit 1);
 let ic = open_in notes_file in
 try while true do
   print_endline (input_line ic)
 done with End_of_file -> ()

let () =

 if Array.length Sys.argv = 1
 then dump_notes()
 else take_notes()</lang>


Oz

<lang oz>functor import

  Application
  Open
  OS
  System

define

  fun {TimeStamp}
     N = {OS.localTime}
  in
     (1900+N.year)#"-"#(1+N.mon)#"-"#N.mDay#", "#N.hour#":"#N.min#":"#N.sec
  end
  fun {Join X|Xr Sep}
     {FoldL Xr fun {$ Z X} Z#Sep#X end X}
  end
  
  case {Application.getArgs plain}
  of nil then
     try
        F = {New Open.file init(name:"notes.txt")}
     in
        {System.printInfo {F read(list:$ size:all)}}
        {F close}
     catch _ then skip end      
  [] Args then
     F = {New Open.file init(name:"notes.txt" flags:[write text create append])}
  in
     {F write(vs:{TimeStamp}#"\n")}
     {F write(vs:"\t"#{Join Args " "}#"\n")}
     {F close}
  end
  {Application.exit 0}

end</lang>

Perl

<lang Perl>my $file = 'notes.txt'; if ( @ARGV ) {

 open NOTES, '>>', $file or die "Can't append to file $file: $!";
 print NOTES scalar localtime, "\n\t@ARGV\n";

} else {

 open NOTES, '<', $file or die "Can't read file $file: $!";
 print <NOTES>;

} close NOTES;</lang>

Sample output after a few runs:

Thu Apr 22 19:01:41 2010
	Test line 1.
Thu Apr 22 19:01:45 2010
	Test line 2.

Perl 6

<lang perl6>my $file = 'notes.txt';

multi MAIN() {

   print slurp($file);

}

multi MAIN(*@note) {

   my $fh = open($file, :a);
   $fh.say: DateTime.now, "\n\t", @note;
   $fh.close;

}</lang>

PicoLisp

<lang PicoLisp>#!/usr/bin/picolisp /usr/lib/picolisp/lib.l

(load "@lib/misc.l") (if (argv)

  (out "+notes.txt" (prinl (stamp) "^J^I" (glue " " @)))
  (and (info "notes.txt") (in "notes.txt" (echo))) )

(bye)</lang>

PowerShell

<lang powershell>$notes = "notes.txt" if (($args).length -eq 0) {

   if(Test-Path $notes) {
       Get-Content $notes
   }

} else {

   Get-Date | Add-Content $notes
   "`t" + $args -join " " | Add-Content $notes

}</lang>

PureBasic

<lang PureBasic>#FileName="notes.txt" Define argc=CountProgramParameters() If OpenConsole()

 If argc=0
   If ReadFile(0,#FileName)
     While Eof(0)=0
       PrintN(ReadString(0))                    ; No new notes, so present the old
     Wend
     CloseFile(0)
   EndIf
 Else ; e.g. we have some arguments
   Define d$=FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",date())
   If OpenFile(0,#FileName)
     Define args$=""
     While argc
       args$+" "+ProgramParameter()             ; Read all arguments
       argc-1
     Wend
     FileSeek(0,Lof(0))                         ; Go to the end of this file
     WriteStringN(0,d$+#CRLF$+#TAB$+args$)      ; Append date & note
     CloseFile(0)
   EndIf
 EndIf

EndIf</lang>

Python

<lang python>import sys, datetime, shutil

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

if len(sys.argv) == 1:

   try:
       f = open('notes.txt', 'r')
       shutil.copyfileobj(f, sys.stdout)
       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

REBOL

<lang REBOL>REBOL [

  Title: "Notes"
  Author: oofoe
  Date: 2010-10-04
  URL: http://rosettacode.org/wiki/Take_notes_on_the_command_line

]

notes: %notes.txt

either any [none? args: system/script/args empty? args] [

  if exists? notes [print read notes]

] [

  write/binary/append notes rejoin [now lf tab args lf]

]</lang>

Sample session:

> rebol -w notes.r 
> rebol -w notes.r "Test line 1"
> rebol -w notes.r "Test" "line" "2"
> rebol -w notes.r Test line 3
> rebol -w notes.r
4-Oct-2010/21:45:16-5:00
	Test line 1
4-Oct-2010/21:45:27-5:00
	Test line 2
4-Oct-2010/21:45:36-5:00
	Test line 3

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>

SNOBOL4

Works with: Macro SNOBOL4 in C

<lang snobol>#! /usr/local/bin/snobol4 -b a = 2 ;* skip '-b' parameter notefile = "notes.txt" while args = args host(2,a = a + 1) " " :s(while) ident(args) :f(append) noparms input(.notes,io_findunit(),,notefile) :s(display)f(end) display output = notes :s(display) endfile(notes) :(end) append output(.notes,io_findunit(),"A",notefile) :f(end) notes = date() notes = char(9) args 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

UNIX Shell

Bash version <lang bash># NOTES=$HOME/notes.txt if $# -eq 0  ; then

 -r  $NOTES  && more $NOTES

else

 date >> $NOTES
 echo "  $*" >> $NOTES

fi</lang>

"Spoiled Bash kid" version <lang bash>N=~/notes.txt;$# -gt 0 && { date ; echo " $*"; exit 0; } >> $N || -r $N && cat $N</lang>


Portable version <lang shell>NOTES=$HOME/notes.txt if test "x$*" = "x" then

       if test -r  $NOTES
       then
               more $NOTES
       fi

else

       date >> $NOTES
       echo "  $*" >> $NOTES

fi</lang>