~lucasemmoreira/hitter

6e101a0db0511db814463bad434150e95f711bd5 — Lucas Moreira 2 years ago 88c08d6
working version withou menu
3 files changed, 24 insertions(+), 22 deletions(-)

M src/hit.janet
M src/main.janet
M src/tui.janet
M src/hit.janet => src/hit.janet +3 -2
@@ 1,8 1,9 @@
(import http)

(defn status-url [url]
  (when url 
    (get (http/get url) :status)))
  (try (when url 
	 (get (http/get url) :status))
       ([err] "connection failed")))

(defn status-urls [urls]
  (filter (fn [ell] (do (not (nil? ell))))

M src/main.janet => src/main.janet +2 -3
@@ 3,6 3,5 @@
(import ./hit :as hit)

(defn main [& args]
  (let [[names urls] (config/read-config "./urls")
	status (hit/status-urls urls)]
    (ui/run-ui [names status])))
  (let [[names urls] (config/read-config "./urls")]
    (ui/run-ui [names urls])))

M src/tui.janet => src/tui.janet +19 -17
@@ 1,6 1,14 @@
(import janet-ncurses :as nc)
(import ./hit :as hit)

(defn run-ui [[names statuses]]
(defn build-win [win names status]
  (nc/wclear win)
  (for i 0 (length names)
    (nc/mvwprintw win (inc i) 1 (string/format "%s %s" (get names i) (get status i))))
  (nc/box win 0 0)
  (nc/wrefresh win))

(defn run-ui [[names urls]]
  (nc/initscr)

  (nc/cbreak)


@@ 9,27 17,21 @@

  (nc/keypad (nc/stdscr) true)
  
  (def statuses (map (fn [status] (string status)) statuses))
  (def items (nc/items names statuses))

  (def menu (nc/new_menu items))

  (def menu-win (nc/newwin 10 40 4 4))
  (def url-status (fiber/new (fn []
			       (while true
				 (yield (map (fn [status] (string status))
					     (hit/status-urls urls)))))))

  (nc/keypad menu-win true)
  (nc/refresh)
  (var status (map (fn [url] "checking...") urls))
  (var menu-win (nc/newwin 10 40 4 4))
  (nc/box menu-win 0 0)
  (nc/set_menu_sub menu (nc/derwin menu-win 6 38 1 1))
  (nc/post_menu menu)

  (nc/set_menu_win menu menu-win)

  (nc/wrefresh menu-win)
  (build-win menu-win names status)

  (while true
    (var status (resume url-status))
    (build-win menu-win names status)
    (let [c (nc/wgetch menu-win)]
      (case c 
	(nc/KEY_DOWN) (nc/menu_driver menu (nc/REQ_DOWN_ITEM))
	(nc/KEY_UP) (nc/menu_driver menu (nc/REQ_UP_ITEM)))))
	"q" (nc/endwin))))

  (nc/endwin))