add bookmark support + test - clic - Clic is an command line interactive clien… | |
git clone git://bitreich.org/clic/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
LICENSE | |
--- | |
commit 68af95e45c280ecb7eaa1e846948042fb4f8b49d | |
parent f9ee8314bb65f728a3a6569942f7786dd9410e79 | |
Author: Solene Rapenne <[email protected]> | |
Date: Mon, 6 Nov 2017 22:18:10 +0000 | |
add bookmark support + test | |
Diffstat: | |
M Makefile | 2 +- | |
M clic.lisp | 58 +++++++++++++++++++++++++++++… | |
M test.lisp | 8 ++++++++ | |
3 files changed, 65 insertions(+), 3 deletions(-) | |
--- | |
diff --git a/Makefile b/Makefile | |
@@ -27,7 +27,7 @@ uninstall: | |
@rm -f "${DESTDIR}${BINDIR}/${BIN}" | |
clean: | |
- rm -f "${BIN}" clic.o clic.eclh clic.cxx | |
+ rm -f "${BIN}" clic.o clic.eclh clic.cxx bookmark-test | |
test: clean all | |
@sh run-test.sh ${LISP} | |
diff --git a/clic.lisp b/clic.lisp | |
@@ -6,12 +6,17 @@ | |
(require 'sockets)) | |
(defstruct location host port type uri) | |
+(defparameter *history* '()) | |
+(defparameter *bookmarks* nil) | |
(defparameter *links* (make-hash-table)) | |
(defparameter *colors* (make-hash-table)) | |
(defparameter *allowed-selectors* (list "0" "1" "2" "3" "4" "5" "6" "i" | |
"h" "7" "8" "9" "+" "T" "g" "I")) | |
-(defparameter *history* '()) | |
-(defparameter *offline* nil) | |
+ | |
+;; customizable | |
+(defparameter *offline* nil) ;; keep files visited on disk | |
+(defparameter *bookmark-file* "bookmark.lisp") ;; name/location of the bookmar… | |
+;; end customizable | |
;; ANSI colors | |
(defun add-color(name type hue) | |
@@ -205,11 +210,47 @@ | |
(pop *history*) | |
(visit (pop *history*)))) | |
+(defun load-bookmark() | |
+ "Restore the bookmark from file" | |
+ (when (probe-file *bookmark-file*) | |
+ (with-open-file (x *bookmark-file* :direction :input) | |
+ (setf *bookmarks* (read x))))) | |
+ | |
+(defun save-bookmark() | |
+ "Dump the bookmark to file" | |
+ (with-open-file (x *bookmark-file* | |
+ :direction :output | |
+ :if-does-not-exist :create | |
+ :if-exists :supersede) | |
+ (print *bookmarks* x))) | |
+ | |
+(defun add-bookmark() | |
+ "Add a new bookmark" | |
+ (push (car *history*) *bookmarks*) | |
+ (save-bookmark)) | |
+ | |
+(defun show-bookmarks() | |
+ "display the bookmarks like a page" | |
+ (setf *links* (make-hash-table)) | |
+ (loop for bookmark in *bookmarks* | |
+ counting bookmark into line-number | |
+ while bookmark do | |
+ (progn | |
+ (setf (gethash line-number *links*) bookmark) | |
+ (print-with-color (concatenate 'string | |
+ (location-host bookmark) | |
+ " " | |
+ (location-type bookmark) | |
+ (location-uri bookmark)) | |
+ 'file line-number)))) | |
+ | |
(defun help-shell() | |
"show help for the shell" | |
(format t "number : go to link n~%") | |
(format t "p : go to previous page~%") | |
(format t "h : display history~%") | |
+ (format t "b : display bookmarks and choose a link from it~%") | |
+ (format t "a : add a bookmark~%") | |
(format t "help : show this help~%") | |
(format t "x or q : exit the shell, go back to REPL~%")) | |
@@ -228,6 +269,14 @@ | |
((string= "HELP" user-input) | |
(help-shell)) | |
+ ;; bookmark current link | |
+ ((string= "A" user-input) | |
+ (add-bookmark)) | |
+ | |
+ ;; show bookmarks | |
+ ((string= "B" user-input) | |
+ (show-bookmarks)) | |
+ | |
;; go to previous page | |
((string= "P" user-input) | |
(p)) | |
@@ -275,6 +324,9 @@ | |
;; glue remaining args between them | |
:uri (format nil "~{/~a~}" infos)))))) | |
+ | |
+ | |
+ | |
(defun get-argv() | |
#+sbcl | |
(cadr *posix-argv*) | |
@@ -299,3 +351,5 @@ | |
#+ecl | |
(defconstant +uri-rules+ | |
'(("*DEFAULT*" 1 "" :stop))) | |
+ | |
+(load-bookmark) | |
diff --git a/test.lisp b/test.lisp | |
@@ -6,8 +6,12 @@ | |
(print (parse-url "perso.pw")) | |
(print (parse-url "perso.pw:70")) | |
+ | |
+(setf *bookmark-file* "bookmark-test") | |
+(load-bookmark) | |
(p) | |
(g 2) | |
+(add-bookmark) | |
(getpage "bitreich.org" 70 "/") | |
(g 11) ;; going to radio | |
(g 35) ;; going back | |
@@ -17,6 +21,10 @@ | |
(g 1) | |
(p) | |
(p) | |
+(add-bookmark) | |
+(show-bookmarks) | |
+(g 1) | |
+ | |
(print *history*) | |
(format t "~%") |