aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorClyne Sullivan <clyne@bitgloo.com>2023-07-15 07:38:58 -0400
committerClyne Sullivan <clyne@bitgloo.com>2023-07-15 07:38:58 -0400
commitb9c39fdc582116086befde372684a1901115728f (patch)
tree3e6bad75f955540e44b17b9b9171e31d1a770980
parent5523960bef3c76aba8d52a25fb0c352e4374f61d (diff)
clean up views; set instance and community
-rw-r--r--.gitignore2
-rw-r--r--src/lemmold/core.clj82
2 files changed, 52 insertions, 32 deletions
diff --git a/.gitignore b/.gitignore
index 3387254..1c311fe 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,4 +13,4 @@ pom.xml.asc
.lein-failures
.nrepl-port
.cpcache/
-
+.*.sw*
diff --git a/src/lemmold/core.clj b/src/lemmold/core.clj
index 7e211b9..c08d30f 100644
--- a/src/lemmold/core.clj
+++ b/src/lemmold/core.clj
@@ -2,7 +2,6 @@
(:require [clj-http.client :as client])
(:require [clojure.data.json :as json]))
-(def INSTANCE "lemmy.ml")
(def POST-LIST "/post/list")
(def COMMENTS "/comment/list")
@@ -15,24 +14,27 @@
(get :body)
(json/read-str)))
-(defn post-list [page]
- (-> (make-api-url INSTANCE POST-LIST)
- (api-call {:sort "Hot" :page (str page)})
- (get "posts")))
+(defn post-list [instance community page]
+ (let [query-params (cond-> {:sort "Hot" :page (str page)}
+ (not (empty? community))
+ (assoc :community_name community))]
+ (-> (make-api-url instance POST-LIST)
+ (api-call query-params)
+ (get "posts"))))
-(defn comments-list [post]
- (-> (make-api-url INSTANCE COMMENTS)
+(defn comments-list [instance post]
+ (-> (make-api-url instance COMMENTS)
(api-call {:limit "50" :post_id (str (get-in post ["post" "id"]))})
(get "comments")))
-(defn show-post-item [post]
+(defn show-post-item [index post]
(let [items [["creator" "name"]
["community" "name"]
["post" "name"]
["post" "published"]
["counts" "comments"]]
- data (mapv (partial get-in post) items)]
- (apply (partial printf "%s on %s\n%s\nat %s (%d comments)\n\n") data)))
+ data (cons (inc index) (mapv (partial get-in post) items))]
+ (apply (partial printf "%d. %s on %s\n %s\n at %s (%d comments)\n\n") data)))
(defn show-comment-item [commnt]
(println (get-in commnt ["creator" "name"]) "says:")
@@ -41,11 +43,11 @@
)
(defn show-posts [posts]
- (doseq [post (take 5 posts)]
- (show-post-item post)))
+ (doseq [index (range 0 5)]
+ (show-post-item index (nth posts index))))
(defn show-comments [comments]
- (doseq [commnt (take 5 comments)]
+ (doseq [commnt (take 1 comments)]
(show-comment-item commnt)))
(defn show-posts-prompt []
@@ -58,32 +60,50 @@
(flush)
(first (read-line)))
-(defn view-post [post]
- (loop [offset 0 comments (comments-list post)]
- (show-comments (if (pos? offset) (drop (* 5 offset) comments) comments))
+(defn show-community-prompt []
+ (print "Enter community name [all]: ")
+ (flush)
+ (read-line))
+
+(defn show-instance-prompt []
+ (print "Enter instance name [lemmy.ml]: ")
+ (flush)
+ (let [inst (read-line)] (if (empty? inst) "lemmy.ml" inst)))
+
+(defn view-post [instance post]
+ (loop [offset 0 comments (comments-list instance post)]
+ (println)
+ (show-comments (if (pos? offset) (drop (* 1 offset) comments) comments))
(case (show-comments-prompt)
\B (println)
\N (recur (inc offset) comments)
\P (recur (dec offset) comments)
(do (println "Unknown command.") (recur offset comments)))))
-(defn view-page []
- (loop [top true page 1 posts (post-list page)]
+(defn view-page [instance init-community]
+ (loop [top true community init-community page 1 posts (post-list instance community page)]
+ (println)
(show-posts (if top posts (drop 5 posts)))
(case (show-posts-prompt)
- \N (if top (recur false page posts)
- (recur true (inc page) (post-list (inc page))))
- \P (if top (recur false (dec page) (post-list (dec page)))
- (recur true page posts))
- \Q (do)
- \1 (do (view-post (nth posts (cond-> 5 top (- 5)))) (recur top page posts))
- \2 (do (view-post (nth posts (cond-> 6 top (- 5)))) (recur top page posts))
- \3 (do (view-post (nth posts (cond-> 7 top (- 5)))) (recur top page posts))
- \4 (do (view-post (nth posts (cond-> 8 top (- 5)))) (recur top page posts))
- \5 (do (view-post (nth posts (cond-> 9 top (- 5)))) (recur top page posts))
- (do (println "Unknown command.") (recur top page posts)))))
+ \N (if top (recur false community page posts)
+ (recur true community (inc page) (post-list instance community (inc page))))
+ \P (if top (recur false community (dec page) (post-list instance community (dec page)))
+ (recur true community page posts))
+ \Q (println)
+ \1 (do (view-post instance (nth posts (cond-> 5 top (- 5)))) (recur top community page posts))
+ \2 (do (view-post instance (nth posts (cond-> 6 top (- 5)))) (recur top community page posts))
+ \3 (do (view-post instance (nth posts (cond-> 7 top (- 5)))) (recur top community page posts))
+ \4 (do (view-post instance (nth posts (cond-> 8 top (- 5)))) (recur top community page posts))
+ \5 (do (view-post instance (nth posts (cond-> 9 top (- 5)))) (recur top community page posts))
+ \C (let [comm (show-community-prompt)] (recur true comm 1 (post-list instance comm page)))
+ (do (println "Unknown command.") (recur top community page posts)))))
(defn -main [& args]
- (view-page)
- (println "Goodbye."))
+ (println "Welcome to lemmold, your old-school Lemmy browser!")
+ (println)
+ (let [instance (show-instance-prompt)
+ community (show-community-prompt)]
+ (view-page instance community))
+ (println "Goodbye.")
+ (println))