]> code.bitgloo.com Git - clyne/lemmold.git/commitdiff
clean up views; set instance and community
authorClyne Sullivan <clyne@bitgloo.com>
Sat, 15 Jul 2023 11:38:58 +0000 (07:38 -0400)
committerClyne Sullivan <clyne@bitgloo.com>
Sat, 15 Jul 2023 11:38:58 +0000 (07:38 -0400)
.gitignore
src/lemmold/core.clj

index 3387254799b45f0f8ace0d5930fc0305a2c80d97..1c311fe87def84130a7da4fd2c9a8cbd6dc64a7c 100644 (file)
@@ -13,4 +13,4 @@ pom.xml.asc
 .lein-failures
 .nrepl-port
 .cpcache/
-
+.*.sw*
index 7e211b9ccc664e5d4f74601ea966ba4de26485e7..c08d30f77dc43bcfa0f7751d1352d2c03238b03c 100644 (file)
@@ -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")
 
       (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:")
   )
 
 (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 []
   (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))