Add image viewer example [skip ci]
This commit is contained in:
parent
c0a674b034
commit
66e0349660
4 changed files with 82 additions and 4 deletions
Binary file not shown.
|
Before Width: | Height: | Size: 34 KiB |
|
|
@ -249,8 +249,6 @@ is a variation on the
|
||||||
[http-server](https://github.com/borkdude/babashka/#tiny-http-server)
|
[http-server](https://github.com/borkdude/babashka/#tiny-http-server)
|
||||||
example. If you get prompted with a login, use `admin`/`admin`.
|
example. If you get prompted with a login, use `admin`/`admin`.
|
||||||
|
|
||||||
<img src="../assets/notes-example.png" width="400px">
|
|
||||||
|
|
||||||
## which
|
## which
|
||||||
|
|
||||||
The `which` command re-implemented in Clojure. See
|
The `which` command re-implemented in Clojure. See
|
||||||
|
|
@ -317,10 +315,22 @@ See [examples/vim.clj](examples/vim.clj).
|
||||||
|
|
||||||
This script uses [djblue/portal](https://github.com/djblue/portal/) for inspecting EDN, JSON, XML or YAML files.
|
This script uses [djblue/portal](https://github.com/djblue/portal/) for inspecting EDN, JSON, XML or YAML files.
|
||||||
|
|
||||||
Examples usage:
|
Example usage:
|
||||||
|
|
||||||
``` shell
|
``` shell
|
||||||
$ examples/portal.clj ~/git/clojure/pom.xml
|
$ examples/portal.clj ~/git/clojure/pom.xml
|
||||||
```
|
```
|
||||||
|
|
||||||
See [portal.clj](portal.clj).
|
See [portal.clj](portal.clj).
|
||||||
|
|
||||||
|
### Image viewer
|
||||||
|
|
||||||
|
Opens browser window and lets user navigate through images of all sub-directories.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
|
||||||
|
``` shell
|
||||||
|
$ examples/image_viewer.clj
|
||||||
|
```
|
||||||
|
|
||||||
|
See [image_viewer.clj](image_viewer.clj).
|
||||||
|
|
|
||||||
68
examples/image_viewer.clj
Executable file
68
examples/image_viewer.clj
Executable file
|
|
@ -0,0 +1,68 @@
|
||||||
|
#!/usr/bin/env bb
|
||||||
|
|
||||||
|
(ns image-viewer
|
||||||
|
(:require [clojure.java.browse :as browse]
|
||||||
|
[clojure.java.io :as io]
|
||||||
|
[clojure.string :as str]
|
||||||
|
[org.httpkit.server :as server])
|
||||||
|
(:import [java.net URLDecoder URLEncoder]))
|
||||||
|
|
||||||
|
(def images
|
||||||
|
(filter #(and (.isFile %)
|
||||||
|
(let [name (.getName %)
|
||||||
|
ext (some-> (str/split name #"\.")
|
||||||
|
last
|
||||||
|
str/lower-case)]
|
||||||
|
(contains? #{"jpg" "jpeg" "png" "gif" "svg"} ext)))
|
||||||
|
(file-seq (io/file "."))))
|
||||||
|
|
||||||
|
(def image-count (count images))
|
||||||
|
|
||||||
|
(defn page [n]
|
||||||
|
(let [prev (max 0 (dec n))
|
||||||
|
next (min (inc n) (dec image-count))
|
||||||
|
file-path (.getCanonicalPath (nth images n))
|
||||||
|
encoded-file-path (URLEncoder/encode file-path)]
|
||||||
|
{:body (format "
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset=\"utf-8\"/>
|
||||||
|
<script>
|
||||||
|
window.onkeydown=function(e) {
|
||||||
|
switch (e.key) {
|
||||||
|
case \"ArrowLeft\":
|
||||||
|
window.location.href=\"/%s\"; break;
|
||||||
|
case \"ArrowRight\":
|
||||||
|
window.location.href=\"/%s\"; break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
Navigation: use left/right arrow keys
|
||||||
|
<p>%s</p>
|
||||||
|
<div>
|
||||||
|
<img style=\"max-height: 90vh; margin: auto; display: block;\" src=\"assets/%s\"/>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>" prev next file-path encoded-file-path)}))
|
||||||
|
|
||||||
|
(server/run-server
|
||||||
|
(fn [{:keys [:uri]}]
|
||||||
|
(if (str/starts-with? uri "/assets")
|
||||||
|
;; serve the file
|
||||||
|
(let [f (io/file (-> (str/replace uri "assets" "")
|
||||||
|
(URLDecoder/decode)))]
|
||||||
|
{:body f})
|
||||||
|
;; serve html
|
||||||
|
(let [n (-> (str/replace uri "/" "")
|
||||||
|
(Integer/parseInt))]
|
||||||
|
(page n)))))
|
||||||
|
|
||||||
|
(browse/browse-url "http://localhost:8090/0")
|
||||||
|
|
||||||
|
@(promise)
|
||||||
2
process
2
process
|
|
@ -1 +1 @@
|
||||||
Subproject commit 98056d87e34a9bf56fcc11ca23ae19c69783455f
|
Subproject commit 249ddcfd1a5825f33adacc9cfde72a0c3823bdc9
|
||||||
Loading…
Reference in a new issue