todo-prolog/todo.pl
2024-07-08 12:09:15 +02:00

100 lines
2.1 KiB
Prolog

:- dynamic task/2.
:- dynamic requires/2.
:- dynamic idx/2.
:- discontiguous load/0.
all_tasks(L):- findall(X, task(X, _), L).
all_tasks(L, STATE):- findall(X, task(X, STATE), L).
print_task_list([],_).
print_task_list([H|T],N) :-
M is N+1,
retractall(idx(N, _)),
asserta(idx(N, H)),
write(N),
write(". "),
task(H,ISDONE),
write(ISDONE),
write(': '),
write(H),nl,
print_task_list(T, M).
list_all :- all_tasks(L), print_task_list(L,1).
list_all :- all_tasks(L), print_task_list(L,1).
list :- all_tasks(L, todo), print_task_list(L,1).
list_done :- all_tasks(L, done), print_task_list(L,1).
print_list([]).
print_list([H|T]) :- write(H),nl, print_list(T).
new(X) :-
asserta(task(X, todo)),
write("added task "),
write(X).
check(X) :- idx(X, Task), check(Task).
check(X) :-
task(X, todo),
\+ (requires(Y, X), task(Y,todo)),
asserta(task(X, done)),
retractall(task(X, todo)),
write("marked "),
write(X),
write(" as done"),
!.
check(X) :-
task(X, todo),
(requires(Y, X), task(Y,todo)),
write("you need to do "),
write(Y),
write(" before doing "),
write(X),
!.
check(X) :-
\+ task(X, todo),
write("no such task as "),
write(X).
save_task(Task, Stream) :-
task(Task, State),
write_canonical(Stream, task(Task, State)),
write(Stream, '.').
save_task_list([Task], Stream) :-
save_task(Task, Stream).
save_task_list([H|T], Stream) :-
save_task(H, Stream),
write(Stream, '\n'),
save_task_list(T, Stream).
save :-
all_tasks(L),
open('data.pl', write, Stream),
save_task_list(L, Stream),
close(Stream).
load_task(Stream) :-
read(Stream, Task),
Task \= end_of_file,
% write("reading: "),
% write(Task),
% nl,
asserta(Task),
load_task(Stream).
load_task(Stream) :-
read(Stream, Task),
Task = end_of_file,
% write("reading last: "),
% write(Task),
close(Stream).
load :-
open('data.pl', read, Stream),
load_task(Stream).
clear :- retractall(task(_, _)).
load.