Take notes on the command line: Difference between revisions
Content added Content deleted
(Added Wren) |
Not a robot (talk | contribs) (Add CLU) |
||
Line 728: | Line 728: | ||
</pre> |
</pre> |
||
=={{header|CLU}}== |
|||
<lang clu>% This program uses the "get_argv" function that is supplied iwth |
|||
% PCLU in "useful.lib". |
|||
NOTEFILE = "notes.txt" |
|||
% Format the date and time as MM/DD/YYYY HH:MM:SS [AM|PM] |
|||
format_date = proc (d: date) returns (string) |
|||
ds: stream := stream$create_output() |
|||
stream$putzero(ds, int$unparse(d.month), 2) |
|||
stream$putc(ds, '/') |
|||
stream$putzero(ds, int$unparse(d.day), 2) |
|||
stream$putc(ds, '/') |
|||
stream$putzero(ds, int$unparse(d.year), 4) |
|||
stream$putc(ds, ' ') |
|||
hour: int := d.hour // 12 |
|||
if hour=0 then hour:=12 end |
|||
ampm: string := "AM" |
|||
if d.hour>=12 then ampm := "PM" end |
|||
stream$putzero(ds, int$unparse(hour), 2) |
|||
stream$putc(ds, ':') |
|||
stream$putzero(ds, int$unparse(d.minute), 2) |
|||
stream$putc(ds, ':') |
|||
stream$putzero(ds, int$unparse(d.second), 2) |
|||
stream$putc(ds, ' ') |
|||
stream$puts(ds, ampm) |
|||
return(stream$get_contents(ds)) |
|||
end format_date |
|||
% Add a note to the file |
|||
add_note = proc (note: sequence[string]) |
|||
fn: file_name := file_name$parse(NOTEFILE) |
|||
out: stream := stream$open(fn, "append") |
|||
stream$putl(out, format_date(now())) |
|||
c: char := '\t' |
|||
for part: string in sequence[string]$elements(note) do |
|||
stream$putc(out, c) |
|||
stream$puts(out, part) |
|||
c := ' ' |
|||
end |
|||
stream$putc(out, '\n') |
|||
stream$close(out) |
|||
end add_note |
|||
% Show the notes file, if it exists |
|||
show_notes = proc () |
|||
po: stream := stream$primary_output() |
|||
fn: file_name := file_name$parse(NOTEFILE) |
|||
begin |
|||
inp: stream := stream$open(fn, "read") |
|||
while true do |
|||
stream$putl(po, stream$getl(inp)) |
|||
except when end_of_file: break end |
|||
end |
|||
stream$close(inp) |
|||
end except when not_possible(s: string): end |
|||
end show_notes |
|||
% Add a note if one is given, show the notes otherwise |
|||
start_up = proc () |
|||
note: sequence[string] := get_argv() |
|||
if sequence[string]$empty(note) |
|||
then show_notes() |
|||
else add_note(note) |
|||
end |
|||
end start_up</lang> |
|||
{{out}} |
|||
<pre>$ ls |
|||
notes |
|||
$ ./notes |
|||
$ ./notes This is a test |
|||
$ ./notes This is another test |
|||
$ ./notes Note that this is a test |
|||
$ ./notes |
|||
12/15/2021 11:18:50 AM |
|||
This is a test |
|||
12/15/2021 11:18:52 AM |
|||
This is another test |
|||
12/15/2021 11:19:00 AM |
|||
Note that this is a test |
|||
$ ls |
|||
notes notes.txt</pre> |
|||
=={{header|COBOL}}== |
=={{header|COBOL}}== |