:- consult(pce_file_search_path). :- consult(library(pce)). :- consult(library(process)). :- pce_global(@dialog, new(dialog, 'Save')). main :- pce_main_loop(main). main(_Argv) :- init. init :- shell("git add .", Status), added(Status). added(1) :- send(@display, report, error, 'Files could not be added to index. Ensure Git is in PATH.'). added(0) :- %send(@pce, load_defaults, 'Defaults'), new(D, @dialog), % File browser. new(C, chain), status(C), send(new(B, browser), right, D), send(B, members(C)), % Input fields. send(D, append, text('Commit message (optional):', left, bold)), send(D, append, new(E, editor)), send(D, append, button(save, message(@prolog, save, E, C))), send(D, append, button(cancel, and(message(@prolog, reset), message(D, destroy)))), send(D, open). reset :- shell("git reset"). status(C) :- setup_call_cleanup( process_create(path(git), ['status', '--porcelain'], [stdout(pipe(Out))]), read_to_chain(Out, C), close(Out)). read_to_chain(S, C) :- read_line_to_codes(S, Cs), ( Cs == end_of_file -> true ; atom_codes(A, Cs), send(C, append, A), read_to_chain(S, C) ). save(Editor, Chain) :- get(Editor?text_buffer, contents, S), send(S, strip), get(S, value, Msg), ( Msg == '' -> chain_atom(Chain, A), commit(A) ; commit(Msg) ). chain_atom(C, A) :- chain_atom_x(C, '', A). chain_atom_x(C, A0, A) :- ( get(C, delete_head, A1) -> atom_concat(A0, A1, A2), atom_concat(A2, ' ', A3), chain_atom_x(C, A3, A) ; A = A0 ). commit(Msg) :- write('creating process'), nl, process_create(path(git), ['commit', '-m', Msg], [process(PID)]), process_wait(PID, S), commited(S). commited(S) :- dif(S, exit(0)), dif(S, exit(128)), !, send(@display, report, error, 'Files could not be saved.'). commited(_) :- send(@pce, destroy).