todo-prolog/todo.pl
2024-07-08 07:34:39 +02:00

77 lines
1.6 KiB
Prolog

:- dynamic task/2.
:- dynamic requires/2.
:- dynamic idx/2.
:- discontiguous load/0.
task_input(X, Y) :- task(X, Y).
task_input(X, Y) :- idx(X, Task), task(Task, Y).
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).
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) :- task(X, todo), asserta(task(X, done)), retractall(task(X, todo)),
write("marked task "), write(X), write(" as done").
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,
close(Stream).
load_task(Stream) :-
read(Stream, Task),
write("reading: "),
write(Task),
nl,
asserta(Task),
load_task(Stream).
load :-
open('data.pl', read, Stream),
load_task(Stream).
clear :- retractall(task(_, _)).
consult(data).