Skip to content

Commit

Permalink
a readline app with replic
Browse files Browse the repository at this point in the history
we get:
- a prompt with `replic` built-ins
- two commands: lyrics and search-song (TAB-complete the name)
  • Loading branch information
vindarel committed Jul 26, 2019
1 parent 47789fd commit 0a75ae7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
LISP?=sbcl

build:
$(LISP) --non-interactive \
--load lyrics.asd \
--eval '(ql:quickload :lyrics)' \
--eval '(asdf:make :lyrics)'
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# lyrics

Search song lyrics or, the other way around, search songs from lyrics.

Available as a CL library and as a terminal application.


# Installation

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

Optionally, build the terminal app:

```bash
make build
```

This produces a `lyrics` binary with the two commands `lyrics` and
`search-song` available.


# Usage

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

The readline application is built quite automatically with the
[replic](https://github.com/vindarel/replic/) library.


## Authors
Copyright (c) 2019 [Mihai Olteanu](www.mihaiolteanu.me)

Expand Down
8 changes: 7 additions & 1 deletion lyrics.asd
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
:sqlite
:alexandria
:bordeaux-threads
:defmemo)
:defmemo
:replic)
:serial t

:build-operation "program-op"
:build-pathname "lyrics"
:entry-point "lyrics::main"

:components ((:file "package")
(:file "lyrics")))
21 changes: 17 additions & 4 deletions lyrics.lisp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
;;;; lyrics.lisp
(in-package #:lyrics)

(defparameter *db* nil)

(defun setup-db ()
"Create a sqlite table in ~/ if one does not already exist."
(defparameter *db*
(setf *db*
(connect (merge-pathnames "cl-lyrics.db"
(xdg-config-home))))
(execute-non-query *db*
Expand All @@ -17,10 +19,9 @@
COLLATE NOCASE,
lyrics TEXT COLLATE NOCASE);"))

(setup-db)

(defstruct website
;; website name, only used for documentation purposes
;; website name, only used for documentation purposes
(name)
;; Template used to construct the actual url. It contains the artist-name and
;; song-name string, respectively, which need to be replaced with the user
Expand Down Expand Up @@ -160,8 +161,10 @@ name. If the lyrics are not in the db, try and extract them from one of the
supported lyrics websites. If found, save the lyrics the db and return them. If
not found, return nil."
(declare (string artist song))
(unless *db*
(setup-db))
(if-let ((lyrics (lyrics-from-db artist song)))
lyrics ;already in db
lyrics ;already in db
(dolist (website
;; Try to minimize the chance of getting banned; try a different
;; order of sites on every request.
Expand Down Expand Up @@ -196,3 +199,13 @@ the thread that is started for the request."
;; at a future date; take your time; better to be safe than being banned
;; for making too many requests in a short time.
(sleep (random-elt '(1 2 3)))))))

(defun main ()
(setup-db)

(setf replic:*prompt* "lyrics> ")
(replic.completion:functions-to-commands :replic.base)
(replic.completion:functions-to-commands :lyrics)
(replic:autoprint-results-from :lyrics)

(replic:repl))

0 comments on commit 0a75ae7

Please sign in to comment.