Skip to content

Commit 0a75ae7

Browse files
committed
a readline app with replic
we get: - a prompt with `replic` built-ins - two commands: lyrics and search-song (TAB-complete the name)
1 parent 47789fd commit 0a75ae7

File tree

4 files changed

+49
-5
lines changed

4 files changed

+49
-5
lines changed

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
LISP?=sbcl
2+
3+
build:
4+
$(LISP) --non-interactive \
5+
--load lyrics.asd \
6+
--eval '(ql:quickload :lyrics)' \
7+
--eval '(asdf:make :lyrics)'

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# lyrics
2+
23
Search song lyrics or, the other way around, search songs from lyrics.
34

5+
Available as a CL library and as a terminal application.
6+
7+
48
# Installation
59

610
```bash
@@ -18,6 +22,16 @@ git clone https://github.com/mihaiolteanu/lyrics ~/quicklisp/local-projects/lyri
1822
sudo pacman -S sqlite3
1923
```
2024

25+
Optionally, build the terminal app:
26+
27+
```bash
28+
make build
29+
```
30+
31+
This produces a `lyrics` binary with the two commands `lyrics` and
32+
`search-song` available.
33+
34+
2135
# Usage
2236

2337
```common-lisp
@@ -84,6 +98,10 @@ If the song cannot be found on any of the websites, `lyrics` returns nil. Otherw
8498
`lyrics` returns the song lyrics and saves them in the database from where they
8599
will be fetched on the next call. The `lyrics` function is memoized.
86100

101+
The readline application is built quite automatically with the
102+
[replic](https://github.com/vindarel/replic/) library.
103+
104+
87105
## Authors
88106
Copyright (c) 2019 [Mihai Olteanu](www.mihaiolteanu.me)
89107

lyrics.asd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
:sqlite
1313
:alexandria
1414
:bordeaux-threads
15-
:defmemo)
15+
:defmemo
16+
:replic)
1617
:serial t
18+
19+
:build-operation "program-op"
20+
:build-pathname "lyrics"
21+
:entry-point "lyrics::main"
22+
1723
:components ((:file "package")
1824
(:file "lyrics")))

lyrics.lisp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
;;;; lyrics.lisp
22
(in-package #:lyrics)
33

4+
(defparameter *db* nil)
5+
46
(defun setup-db ()
57
"Create a sqlite table in ~/ if one does not already exist."
6-
(defparameter *db*
8+
(setf *db*
79
(connect (merge-pathnames "cl-lyrics.db"
810
(xdg-config-home))))
911
(execute-non-query *db*
@@ -17,10 +19,9 @@
1719
COLLATE NOCASE,
1820
lyrics TEXT COLLATE NOCASE);"))
1921

20-
(setup-db)
2122

2223
(defstruct website
23-
;; website name, only used for documentation purposes
24+
;; website name, only used for documentation purposes
2425
(name)
2526
;; Template used to construct the actual url. It contains the artist-name and
2627
;; song-name string, respectively, which need to be replaced with the user
@@ -160,8 +161,10 @@ name. If the lyrics are not in the db, try and extract them from one of the
160161
supported lyrics websites. If found, save the lyrics the db and return them. If
161162
not found, return nil."
162163
(declare (string artist song))
164+
(unless *db*
165+
(setup-db))
163166
(if-let ((lyrics (lyrics-from-db artist song)))
164-
lyrics ;already in db
167+
lyrics ;already in db
165168
(dolist (website
166169
;; Try to minimize the chance of getting banned; try a different
167170
;; order of sites on every request.
@@ -196,3 +199,13 @@ the thread that is started for the request."
196199
;; at a future date; take your time; better to be safe than being banned
197200
;; for making too many requests in a short time.
198201
(sleep (random-elt '(1 2 3)))))))
202+
203+
(defun main ()
204+
(setup-db)
205+
206+
(setf replic:*prompt* "lyrics> ")
207+
(replic.completion:functions-to-commands :replic.base)
208+
(replic.completion:functions-to-commands :lyrics)
209+
(replic:autoprint-results-from :lyrics)
210+
211+
(replic:repl))

0 commit comments

Comments
 (0)