~lucasemmoreira/hitter

097b44d161ee9b392fcc54fb9c0e6e6565b4e3cf — Lucas Moreira 2 years ago
initial commit
A  => LICENSE +13 -0
@@ 1,13 @@
Copyright (c) 2020 Lucas Esperancini Moreira e Moreira <me@lucasemmoreira.xyz>

Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

A  => project.janet +12 -0
@@ 1,12 @@
(declare-project
  :name "hitter" 
  :description "a simple tui to show what urls are up" # some example metadata.

  # Optional urls to git repositories that contain required artifacts.
  :dependencies ["https://git.sr.ht/~lucasemmoreira/janet-ncurses"])

(declare-source
  # :source is an array or tuple that can contain
  # source files and directories that will be installed.
  # Often will just be a single file or single directory.
  :source ["src/main.janet"])

A  => src/config.janet +15 -0
@@ 1,15 @@
(defn read-from-file [file-path]
  (let [f (file/open file-path :r)
        content  (file/read f :all)]
    (file/close f)
    content))

(defn separate-name-url [coll]
  @[(map first coll) (map (fn [coll-in] (get coll-in 1)) coll)])

(defn read-config [filename]
  (->> filename
       read-from-file
       (string/split "\n")
       (map (fn [str] (string/split " " str)))
       separate-name-url))

A  => src/curl.janet +5 -0
@@ 1,5 @@
(defn command [url & args]
  (let [cmd @("curl")
        cmd (array/concat cmd args)
        cmd (array/concat cmd url)]
    (os/execute cmd :p )))

A  => src/hit.janet +7 -0
@@ 1,7 @@
(import ./curl :as curl)

(defn status-url [url]
  (curl/command url "-s" "-o /dev/null" "--head" "-w \"%{http_code}\"" "-X" "POST"))

(defn status-urls [urls]
  (map (fn [url] (status-url url)) urls))

A  => src/main.janet +8 -0
@@ 1,8 @@
(import ./tui :as ui)
(import ./config :as config)
(import ./hit :as hit)

(let [[names urls] (config/read-config "../test/urls")
      status (hit/status-urls urls)]
  (printf "%P" status)
  (ui/run-ui [names status]))

A  => src/tui.janet +39 -0
@@ 1,39 @@
(import janet-ncurses :as nc)

(def choices (array "Option 1" "Option 2" "Option 3"))

(def choices2 (array "Description 1" "Description 2" "Description 3"))

(defn run-ui [[names urls]]
  (printf "%P" urls)
  (nc/initscr)

  (nc/cbreak)

  (nc/noecho)

  (nc/keypad (nc/stdscr) true)
  
  (def items (nc/items names urls))

  (def menu (nc/new_menu items))

  (def menu-win (nc/newwin 10 40 4 4))

  (nc/keypad menu-win true)
  (nc/refresh)
  (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)

  (while true
    (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)))))

  (nc/endwin))

A  => test/config_test.janet +4 -0
@@ 1,4 @@
(import ../src/config :as config)

(printf "%P" (config/read-config "./urls"))


A  => test/curl_test.janet +4 -0
@@ 1,4 @@
(import ../src/curl :as curl)

(printf (string (curl/command "http://lucasemmoreira.xyz" "-s" "-o /dev/null" "--head" "-w \"%{http_code}\"" "-X" "POST")))


A  => test/urls +2 -0
@@ 1,2 @@
teste google.com
test2 lukesmith.xyz
\ No newline at end of file