├── scripts ├── deploy ├── all ├── run ├── move ├── build ├── bump └── core.sh ├── src ├── floki │ ├── movement │ │ ├── subs.cljs │ │ ├── events.cljs │ │ └── logic.cljs │ ├── global │ │ ├── logic.cljs │ │ ├── subs.cljs │ │ ├── view.cljs │ │ ├── events.cljs │ │ └── keys.cljs │ ├── preview │ │ ├── events.cljs │ │ ├── subs.cljs │ │ ├── view.cljs │ │ └── logic.cljs │ ├── tree │ │ ├── subs.cljs │ │ ├── view.cljs │ │ └── logic.cljs │ ├── debug │ │ └── view.cljs │ └── core.cljs └── common │ ├── stdin.cljs │ └── print.cljs ├── .gitignore ├── package.json ├── .circleci └── config.yml ├── README.md ├── project.clj └── LICENSE /scripts/deploy: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | npm publish 5 | -------------------------------------------------------------------------------- /src/floki/movement/subs.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.movement.subs 2 | (:require [re-frame.core :as rf])) 3 | 4 | (rf/reg-sub 5 | :movement/pos 6 | (fn [db _] 7 | (select-keys db [:pos/x :pos/y]))) 8 | 9 | -------------------------------------------------------------------------------- /src/floki/global/logic.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.global.logic) 2 | 3 | (defn custom-compare 4 | [& args] 5 | (try 6 | (apply compare args) 7 | (catch js/Error _ 8 | (apply compare (mapv str args))))) 9 | -------------------------------------------------------------------------------- /scripts/all: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd)" 4 | source "$DIR/scripts/core.sh" 5 | 6 | "$DIR/scripts/build" 7 | "$DIR/scripts/move" 8 | "$DIR/scripts/publish" 9 | -------------------------------------------------------------------------------- /scripts/run: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd)" 5 | source "$DIR/scripts/core.sh" 6 | 7 | input="/dev/stdin" 8 | node "$DIR/target/main.js" < "$input" 9 | -------------------------------------------------------------------------------- /src/floki/global/subs.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.global.subs 2 | (:require [re-frame.core :as rf] 3 | [floki.tree.subs] 4 | [floki.preview.subs] 5 | [floki.movement.subs])) 6 | 7 | (rf/reg-sub 8 | :db 9 | (fn [db _] 10 | db)) 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | /.idea 13 | *.iml 14 | temp 15 | figwheel_server.log 16 | package-lock.json 17 | /demos 18 | yarn.lock 19 | /bin 20 | package.bkp.json 21 | -------------------------------------------------------------------------------- /scripts/move: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd)" 5 | source "$DIR/scripts/core.sh" 6 | 7 | from="$DIR/target/main.js" 8 | to="$DIR/bin/main.js" 9 | 10 | rm "$to" || true 11 | mkdir "$DIR/bin" || true 12 | mv "$from" "$to" 13 | chmod a+x "$to" 14 | -------------------------------------------------------------------------------- /src/floki/preview/events.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.preview.events 2 | (:require [re-frame.core :as rf] 3 | [clipboardy :as clip] 4 | [floki.preview.logic :as l])) 5 | 6 | (rf/reg-event-db 7 | :preview/copy-path 8 | (fn [{:tree/keys [format path] :as db}] 9 | (-> (l/path-viewmodel format path) 10 | (clip/writeSync)) 11 | db)) 12 | 13 | -------------------------------------------------------------------------------- /scripts/build: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd)" 5 | source "$DIR/scripts/core.sh" 6 | 7 | pushd "$DIR" &> /dev/null 8 | 9 | rm -rf bin || true 10 | cp package.json package.bkp.json 11 | lein do clean, cljsbuild once 12 | rm package.json 13 | mv package.bkp.json package.json 14 | 15 | popd &> /dev/null 16 | 17 | "$DIR/scripts/move" 18 | -------------------------------------------------------------------------------- /src/floki/global/view.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.global.view 2 | (:require [floki.tree.view :as v.tree] 3 | [floki.preview.view :as v.preview] 4 | [floki.debug.view :as v.debug])) 5 | 6 | (defn root [_] 7 | [:box#base {:left 0 8 | :right 0 9 | :width "100%" 10 | :height "100%"} 11 | [v.tree/left-pane] 12 | [v.tree/right-pane] 13 | [v.preview/preview-box] 14 | [v.preview/path-box] 15 | ;[v.debug/debug-box {:height 10}] 16 | ]) 17 | -------------------------------------------------------------------------------- /src/floki/preview/subs.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.preview.subs 2 | (:require [re-frame.core :as rf] 3 | [quark.navigation.core :as nav] 4 | [floki.preview.logic :as l])) 5 | 6 | (rf/reg-sub 7 | :preview/preview-viewmodel 8 | :<- [:tree/format] 9 | :<- [:tree/input] 10 | :<- [:tree/path] 11 | (fn [[format input path]] 12 | (l/preview-viewmodel format input path) )) 13 | 14 | (rf/reg-sub 15 | :preview/path-viewmodel 16 | :<- [:tree/format] 17 | :<- [:tree/path] 18 | (fn [[format path]] 19 | (l/path-viewmodel format path))) 20 | -------------------------------------------------------------------------------- /scripts/bump: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null 2>&1 && pwd)" 5 | source "$DIR/scripts/core.sh" 6 | 7 | bump () { 8 | 9 | local readonly file="$1" 10 | local readonly search="$2" 11 | local readonly replace="$3" 12 | 13 | sed -i.tmp -E "s/${search}/${replace}/g" "${DIR}/${file}" 14 | rm "${DIR}/${file}.tmp" 15 | 16 | } 17 | 18 | version="$1" 19 | 20 | bump package.json '("version":[[:space:]]*").+(")' "\1${version}\2" 21 | bump project.clj '(\(defproject.*?\")[0-9\.]+(.*)' "\1${version}\2" 22 | -------------------------------------------------------------------------------- /src/common/stdin.cljs: -------------------------------------------------------------------------------- 1 | (ns common.stdin 2 | (:require [process :as process] 3 | [clojure.string :as str])) 4 | 5 | (def stdinput (atom "")) 6 | (def stdin (.-stdin process)) 7 | 8 | (defn handler 9 | [callback] 10 | (.setEncoding stdin "utf8") 11 | (.on stdin "data" 12 | (fn [data] 13 | ; (print (str "DATA!!!!!!!" data)) 14 | (if (= (str data) "q") 15 | (process/exit)) 16 | (swap! stdinput #(str % data)))) 17 | (.on stdin "end" 18 | (fn [] 19 | ; (print "END EVENT!!!!") 20 | (swap! stdinput #(str/trim %)) 21 | (callback @stdinput)))) 22 | -------------------------------------------------------------------------------- /src/floki/global/events.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.global.events 2 | (:require [re-frame.core :as rf] 3 | [quark.navigation.core :as nav] 4 | [floki.preview.events] 5 | [floki.movement.events] 6 | [floki.global.logic :as l.global])) 7 | 8 | (rf/reg-event-db 9 | :init 10 | (fn [_ _] 11 | {:tree/input {} 12 | :tree/paths [] 13 | :pos/x -1 14 | :pos/y 0 15 | :tree/path []})) 16 | 17 | (rf/reg-event-db 18 | :input/set 19 | (fn [db [_ {:keys [format data]}]] 20 | (assoc db 21 | :tree/input data 22 | :tree/format format 23 | :tree/paths (sort l.global/custom-compare (nav/paths data))))) 24 | -------------------------------------------------------------------------------- /src/floki/global/keys.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.global.keys 2 | (:require [re-frame.core :as rf])) 3 | 4 | (def bindings 5 | {["escape" "q" "C-c"] #(.exit js/process 0) 6 | ["g"] #(rf/dispatch [:movement/first]) 7 | ["S-g"] #(rf/dispatch [:movement/last]) 8 | ["h" "left" "C-a"] #(rf/dispatch [:movement/left]) 9 | ["j" "down" "C-n"] #(rf/dispatch [:movement/down]) 10 | ["k" "up" "C-p"] #(rf/dispatch [:movement/up]) 11 | ["l" "right" "C-e"] #(rf/dispatch [:movement/right]) 12 | ["y"] #(rf/dispatch [:preview/copy-path])}) 13 | 14 | (defn setup 15 | [screen] 16 | (doseq [[hotkeys f] bindings] 17 | (.key screen (clj->js hotkeys) f))) 18 | 19 | -------------------------------------------------------------------------------- /src/floki/tree/subs.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.tree.subs 2 | (:require [re-frame.core :as rf] 3 | [floki.tree.logic :as l.tree] 4 | [floki.tree.logic :as l])) 5 | 6 | (rf/reg-sub 7 | :tree/input 8 | (fn [db _] 9 | (:tree/input db))) 10 | 11 | (rf/reg-sub 12 | :tree/format 13 | (fn [db _] 14 | (:tree/format db))) 15 | 16 | (rf/reg-sub 17 | :tree/path 18 | (fn [db _] 19 | (:tree/path db))) 20 | 21 | (rf/reg-sub 22 | :tree/paths 23 | (fn [db _] 24 | (:tree/paths db))) 25 | 26 | (rf/reg-sub 27 | :tree/descs 28 | :<- [:tree/paths] 29 | :<- [:tree/path] 30 | (fn [[paths path]] 31 | (l.tree/descs paths path))) 32 | 33 | (rf/reg-sub 34 | :tree/viewmodel 35 | :<- [:tree/descs] 36 | :<- [:movement/pos] 37 | (fn [[descs pos] [_ index]] 38 | (l/pane-viewmodel descs pos index))) 39 | -------------------------------------------------------------------------------- /src/floki/debug/view.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.debug.view 2 | (:require [re-frame.core :as rf] 3 | [reagent.core :as r])) 4 | 5 | (defonce logger 6 | (r/atom [])) 7 | 8 | (defn log-box [n] 9 | [:text#log 10 | {:bottom 0 11 | :right 0 12 | :width "50%" 13 | :height n 14 | :style {:fg :yellow :bg :grey} 15 | :scrollable true 16 | :content (->> (take-last n @logger) 17 | (clojure.string/join "\n"))}]) 18 | 19 | (defn debug-box [{:keys [height]}] 20 | [:text#debug {:bottom 0 21 | :left 0 22 | :width "100%" 23 | :style {:border {:fg :yellow}} 24 | :border {:type :line} 25 | :label "Debug info"} 26 | [:text {:width "40%" 27 | :content (str @(rf/subscribe [:db]))}] 28 | [log-box (dec height)]]) 29 | -------------------------------------------------------------------------------- /src/floki/preview/view.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.preview.view 2 | (:require [re-frame.core :as rf] 3 | [floki.preview.logic :as l])) 4 | 5 | (defn preview 6 | [] 7 | [:text 8 | {:left 0 9 | :top 0 10 | :width "100%" 11 | :content @(rf/subscribe [:preview/preview-viewmodel])}]) 12 | 13 | (defn preview-box 14 | [] 15 | [:box {:bottom 3 16 | :right 0 17 | :width "76%" 18 | :label "Preview" 19 | :border {:type :line}} 20 | [preview]]) 21 | 22 | (defn path-box 23 | [] 24 | [:text#debug {:bottom 0 25 | :left 0 26 | :width "100%" 27 | :style {:border {:fg :yellow}} 28 | :border {:type :line} 29 | :shrink true 30 | :label "Path"} 31 | [:text {:width "40%" 32 | :content @(rf/subscribe [:preview/path-viewmodel]) }]]) 33 | -------------------------------------------------------------------------------- /scripts/core.sh: -------------------------------------------------------------------------------- 1 | function platform::command_exists() { 2 | type "$1" &>/dev/null 3 | } 4 | 5 | function platform::is_osx() { 6 | [[ $(uname -s) == "Darwin" ]] 7 | } 8 | 9 | if platform::is_osx && platform::command_exists ggrep; then 10 | function sed() { gsed "$@"; } 11 | function awk() { gawk "$@"; } 12 | function find() { gfind "$@"; } 13 | function grep() { ggrep "$@"; } 14 | function head() { ghead "$@"; } 15 | function mktemp() { gmktemp "$@"; } 16 | function date() { gdate "$@"; } 17 | function shred() { gshred "$@"; } 18 | function cut() { gcut "$@"; } 19 | function tr() { gtr "$@"; } 20 | function od() { god "$@"; } 21 | function cp() { gcp "$@"; } 22 | function cat() { gcat "$@"; } 23 | function sort() { gsort "$@"; } 24 | function kill() { gkill "$@"; } 25 | function xargs() { gxargs "$@"; } 26 | export -f sed awk find head mktemp date shred cut tr od cp cat sort kill xargs 27 | fi 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "floki", 3 | "description": "A JSON/EDN browser for the terminal", 4 | "version": "0.7.0", 5 | "author": "Denis Isidoro", 6 | "repository": "https://github.com/denisidoro/floki.git", 7 | "license": "Apache 2.0", 8 | "engines": { 9 | "node": ">=0.9.0" 10 | }, 11 | "scripts": { 12 | "build": "./scripts/build", 13 | "bump": "./scripts/bump" 14 | }, 15 | "main": "bin/main.js", 16 | "preferGlobal": true, 17 | "bin": { 18 | "floki": "bin/main.js" 19 | }, 20 | "keywords": [ 21 | "cli", 22 | "json", 23 | "edn", 24 | "browser", 25 | "clojure" 26 | ], 27 | "dependencies": { 28 | "blessed": "^0.1.81", 29 | "clipboardy": "^1.2.3", 30 | "create-react-class": "^15.6.3", 31 | "node-json-color-stringify": "^1.1.0", 32 | "react": "^16.6.3", 33 | "react-blessed": "^0.5.0", 34 | "react-dom": "^16.6.3" 35 | }, 36 | "devDependencies": { 37 | "@cljs-oss/module-deps": "^1.1.1", 38 | "ws": "^6.1.2" 39 | }, 40 | "files": [ 41 | "bin" 42 | ] 43 | } 44 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Clojure CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-clojure/ for more details 4 | 5 | version: 2 6 | 7 | job_defaults: &defaults 8 | docker: 9 | - image: circleci/clojure:lein-2.8.3-node-browsers-legacy 10 | working_directory: ~/repo 11 | environment: 12 | LEIN_ROOT: "true" 13 | JVM_OPTS: "-Xmx3600m" 14 | ENVIRONMENT: "test" 15 | 16 | jobs: 17 | deps: 18 | <<: *defaults 19 | steps: 20 | - checkout 21 | - restore_cache: 22 | keys: 23 | - deps-{{ checksum "project.clj" }} 24 | - run: lein deps 25 | - save_cache: 26 | paths: 27 | - ~/.m2 28 | key: deps-{{ checksum "project.clj" }} 29 | 30 | build: 31 | <<: *defaults 32 | steps: 33 | - checkout 34 | - restore_cache: 35 | keys: 36 | - deps-{{ checksum "project.clj" }} 37 | - run: lein cljsbuild once prod 38 | 39 | workflows: 40 | version: 2 41 | build: 42 | jobs: 43 | - deps 44 | - build: 45 | requires: 46 | - deps 47 | -------------------------------------------------------------------------------- /src/floki/tree/view.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.tree.view 2 | (:require [re-frame.core :as rf] 3 | [floki.tree.logic :as l] 4 | [reagent.core :as r])) 5 | 6 | (defn list-native-pane 7 | [] 8 | (let [ref* (atom nil) 9 | update #(l/pane-update (some-> % r/props) ref*)] 10 | (r/create-class 11 | {:component-did-update 12 | update 13 | 14 | :reagent-render 15 | (fn [] 16 | (let [com (r/current-component) 17 | {:keys [items style]} (r/props com)] 18 | [:list 19 | {:ref (fn [ref] (reset! ref* ref)) 20 | :items items 21 | :style style}]))}))) 22 | 23 | (defn list-pane 24 | [index] 25 | [list-native-pane @(rf/subscribe [:tree/viewmodel index])]) 26 | 27 | (defn left-pane 28 | [] 29 | [:box {:bottom 3 30 | :left 0 31 | :width "13%" 32 | :border {:type :line 33 | :right false 34 | :fg "blue"}} 35 | [list-pane 0]]) 36 | 37 | (defn right-pane 38 | [] 39 | [:box {:bottom 3 40 | :left "10%" 41 | :width "17%" 42 | :border {:type :line 43 | :left false 44 | :fg "blue"}} 45 | [list-pane 1]]) 46 | -------------------------------------------------------------------------------- /src/common/print.cljs: -------------------------------------------------------------------------------- 1 | (ns common.print 2 | (:require [zprint.core :as zprint] 3 | [clojure.string :as str])) 4 | 5 | (def default-options 6 | {:color-map 7 | {:brace :magenta 8 | :bracket :purple 9 | :comment :green 10 | :deref :red 11 | :fn :blue 12 | :hash-brace :red 13 | :hash-paren :green 14 | :keyword :cyan 15 | :nil :yellow 16 | :none :yellow 17 | :number :green 18 | :paren :red 19 | :syntax-quote-paren :red 20 | :quote :red 21 | :string :yellow 22 | :uneval :magenta 23 | :user-fn :blue} 24 | 25 | :map 26 | {:comma? false 27 | :force-nl? true} 28 | 29 | :set 30 | {:wrap? false 31 | :wrap-coll? false 32 | :wrap-after-multi? false 33 | :indent 5} 34 | 35 | :vector 36 | {:wrap-coll? false 37 | :wrap? false 38 | :wrap-after-multi? false 39 | :indent 4}}) 40 | 41 | (defn replace-color 42 | [text from to] 43 | (str/replace text (str \u001b \[ from) (str \u001b \[ to))) 44 | 45 | (defn replace-black-to-white 46 | [text] 47 | (replace-color text 30 97)) 48 | 49 | (defn cstr 50 | [x] 51 | (-> x 52 | (zprint/czprint-str default-options) 53 | replace-black-to-white)) 54 | -------------------------------------------------------------------------------- /src/floki/preview/logic.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.preview.logic 2 | (:require [common.print :as print] 3 | [clojure.string :as str] 4 | [quark.navigation.core :as nav] 5 | [node-json-color-stringify] 6 | [quark.conversion.data :as conversion])) 7 | 8 | (def preview-content 9 | print/cstr) 10 | 11 | (defn ^:private without-first-and-last-chars 12 | [s] 13 | (subs s 1 (-> s count dec))) 14 | 15 | (defn replace-numbers-by-array-pos 16 | [s] 17 | (if (re-find #"^[0-9]+$" s) 18 | (str "[" s "]") 19 | s)) 20 | 21 | (defn path-viewmodel 22 | [format x] 23 | (if (= format :json) 24 | (as-> x it 25 | (str it) 26 | (without-first-and-last-chars it) 27 | (str/replace it ":" "") 28 | (str/replace it "\"" "") 29 | (str/split it " ") 30 | (map replace-numbers-by-array-pos it) 31 | (str/join "." it) 32 | (str "." it) 33 | (str/replace it ".[" "[")) 34 | (-> x 35 | str 36 | without-first-and-last-chars))) 37 | 38 | (defn preview-viewmodel 39 | [format input path] 40 | (let [data (if (seq path) 41 | (nav/navigate input path) 42 | input)] 43 | (if (= format :json) 44 | (-> data 45 | conversion/edn->json 46 | (js/JSON.parse) 47 | (js/JSON.colorStringify nil 2) 48 | (str/replace "[35m" "[33m") 49 | (str/replace "[39m" "[36m") 50 | (str/replace "[90m" "[97m")) 51 | (preview-content data)))) 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Floki 2 | 3 | [![CircleCI](https://circleci.com/gh/denisidoro/floki.svg?style=svg)](https://circleci.com/gh/denisidoro/floki) 4 | [![npm version](https://badge.fury.io/js/floki.svg)](https://badge.fury.io/js/floki) 5 | 6 | A JSON/EDN browser for the terminal 7 | 8 | :warning: Work on this repository will be discontinued in favor of [my navigators](https://github.com/denisidoro/dotfiles/blob/7c8b9b8b9111ef8b6f9cb22b721570f7ced22b18/docs/navigators.md). Thank you for all the support! :warning: 9 | 10 | Demo 13 | 14 | ### Installation 15 | 16 | ```sh 17 | npm install -g floki 18 | ``` 19 | 20 | ### Usage 21 | 22 | ```sh 23 | echo '{"a": 42}' | floki 24 | ``` 25 | 26 | - directions: `h`, `j`, `k`, `l` or the arrow keys 27 | - first/last item: `g`/`G`, respectively 28 | - copy the path: `y` 29 | - quit: `q` 30 | 31 | ### Roadmap 32 | 33 | Please refer to the [issues page](https://github.com/denisidoro/floki/issues). 34 | 35 | ### Inspiration and template for building your own terminal UI 36 | 37 | I've used [polymeris/hello-react-blessed](https://gist.github.com/polymeris/5e117676b79a505fe777df17f181ca2e) as a starting point. 38 | 39 | If you want to build a tool similar to this one, use [eccentric-j/cljs-tui-template](https://github.com/eccentric-j/cljs-tui-template). 40 | 41 | ### Etymology 42 | 43 | browser > navigator > viking > [Flóki](https://en.wikipedia.org/wiki/Hrafna-Fl%C3%B3ki_Vilger%C3%B0arson) 44 | -------------------------------------------------------------------------------- /src/floki/movement/events.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.movement.events 2 | (:require [re-frame.core :as rf] 3 | [floki.movement.logic :as l.movement])) 4 | 5 | (rf/reg-event-db 6 | :movement/down 7 | (fn [db _] 8 | (if (l.movement/vertical-allowed? db 1) 9 | (-> db 10 | (update :pos/y inc) 11 | (l.movement/update-list 0)) 12 | db))) 13 | 14 | (rf/reg-event-db 15 | :movement/up 16 | (fn [db _] 17 | (if (l.movement/vertical-allowed? db -1) 18 | (-> db 19 | (update :pos/y dec) 20 | (l.movement/update-list 0)) 21 | db))) 22 | 23 | (rf/reg-event-db 24 | :movement/right 25 | (fn [db _] 26 | (if (l.movement/horizontal-allowed? db 1) 27 | (-> db 28 | (assoc :pos/y 0) 29 | (update :pos/x inc) 30 | (l.movement/update-list 1)) 31 | db))) 32 | 33 | (rf/reg-event-db 34 | :movement/first 35 | (fn [db _] 36 | (if (l.movement/jump-allowed? db) 37 | (-> db 38 | (assoc :pos/y 0) 39 | (l.movement/update-list 0)) 40 | db))) 41 | 42 | (rf/reg-event-db 43 | :movement/last 44 | (fn [db _] 45 | (if (l.movement/jump-allowed? db) 46 | (-> db 47 | (assoc :pos/y (l.movement/last-index db)) 48 | (l.movement/update-list 0)) 49 | db))) 50 | 51 | (rf/reg-event-db 52 | :movement/left 53 | (fn [db _] 54 | (cond 55 | (<= (:pos/x db) 0) 56 | (assoc db 57 | :pos/x -1 58 | :tree/path []) 59 | 60 | (l.movement/horizontal-allowed? db -1) 61 | (-> db 62 | l.movement/with-previous-pos-y 63 | (update :pos/x dec) 64 | (l.movement/update-list -1)) 65 | 66 | :else 67 | db))) 68 | -------------------------------------------------------------------------------- /src/floki/movement/logic.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.movement.logic 2 | (:require [floki.tree.logic :as l.tree])) 3 | 4 | (defn update-list 5 | [{:keys [tree/paths tree/path pos/x pos/y] :as db} 6 | element-increase] 7 | (let [data (l.tree/descs paths path) 8 | get-fn #(get % x) 9 | ks (-> data get-fn :keys vec) 10 | item (get ks y) 11 | crop #(case element-increase 12 | -1 (drop-last 2 %) 13 | 0 (drop-last %) 14 | 1 %) 15 | new-path (-> path crop vec (conj item))] 16 | (if item 17 | (assoc db :tree/path new-path) 18 | (update db :pos/x dec element-increase)))) 19 | 20 | (defn vertical-allowed? 21 | [{:keys [tree/paths tree/path pos/x pos/y]} 22 | increase] 23 | (let [data (l.tree/descs paths path) 24 | total-items (-> data (get x) :keys count) 25 | res (+ y increase)] 26 | (and (not (neg? res)) 27 | (< res total-items)))) 28 | 29 | (defn horizontal-allowed? 30 | [{:keys [tree/paths tree/path pos/x]} 31 | increase] 32 | (let [data (l.tree/descs paths path) 33 | total-items (-> data (get (+ increase x)) :keys count)] 34 | (pos? total-items))) 35 | 36 | (defn jump-allowed? 37 | [{:keys [pos/x]}] 38 | (not (neg? x))) 39 | 40 | (defn last-index 41 | [{:keys [tree/paths tree/path]}] 42 | (let [descs (l.tree/descs paths path)] 43 | (->> descs 44 | (filter :index) 45 | last 46 | :keys 47 | count 48 | dec))) 49 | 50 | (defn with-previous-pos-y 51 | [{:keys [tree/paths tree/path] :as db}] 52 | (let [descs (l.tree/descs paths path) 53 | previous-index (->> descs 54 | (keep :index) 55 | drop-last 56 | last)] 57 | (assoc db :pos/y previous-index))) 58 | -------------------------------------------------------------------------------- /src/floki/tree/logic.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.tree.logic 2 | (:require [quark.collection.seq :as coll.seq] 3 | [quark.collection.map :as coll.map] 4 | [clojure.string :as str])) 5 | 6 | (defn ^:private calc-index 7 | [path path-seq] 8 | (let [n (-> path-seq first count) 9 | p (take n path)] 10 | (coll.seq/first-index #(= % p) path-seq))) 11 | 12 | (defn ^:private calc-path-seqs 13 | [paths path] 14 | (loop [n 0 15 | acc []] 16 | (if (> n (count path)) 17 | acc 18 | (let [p (take n path) 19 | ps (filter #(and (-> % count dec (= n)) 20 | (->> % (take n) (= p))) paths)] 21 | (recur (inc n) (conj acc ps)))))) 22 | 23 | (defn merge-path-with-index 24 | [path-seq index] 25 | (coll.map/assoc-if {:keys (mapv last path-seq)} :index index)) 26 | 27 | (defn descs 28 | [paths path] 29 | (let [path-seqs (vec (calc-path-seqs paths path)) 30 | indexes (mapv (partial calc-index path) path-seqs)] 31 | (mapv merge-path-with-index path-seqs indexes))) 32 | 33 | (defn bg-color 34 | [pos index] 35 | (if (= -1 (:pos/x pos)) 36 | (case index 0 "green" nil) 37 | (case index 0 "blue" "green"))) 38 | 39 | (defn get-fn 40 | [coll index] 41 | (try 42 | (let [x (->> coll (keep :index) count dec) 43 | coll' (->> coll (into [{:keys [:root] :index 0}]) (drop x) vec)] 44 | (get coll' index)) 45 | (catch js/Error e 46 | (do (print e) 47 | {})))) 48 | 49 | (defn pane-update 50 | [{:keys [selected-index]} ref*] 51 | (when selected-index 52 | (some-> @ref* (.select selected-index)))) 53 | 54 | (defn ^:private without-colon 55 | [x] 56 | (-> x 57 | str 58 | (str/replace ":" ""))) 59 | 60 | (defn as-item 61 | [x] 62 | (without-colon x)) 63 | 64 | (defn pane-viewmodel 65 | [descs pos index] 66 | (let [desc (get-fn descs index) 67 | items (->> desc :keys (mapv as-item)) 68 | selected-index (:index desc) 69 | color (bg-color pos index) 70 | style {:selected {:bg color}}] 71 | {:items items 72 | :selected-index selected-index 73 | :style style})) 74 | -------------------------------------------------------------------------------- /src/floki/core.cljs: -------------------------------------------------------------------------------- 1 | (ns floki.core 2 | (:require [cljs.nodejs :as nodejs] 3 | [cognitect.transit :as transit] 4 | [common.stdin :as stdin] 5 | [reagent.core :as r] 6 | [quark.conversion.data :as conversion] 7 | [re-frame.core :as rf] 8 | [blessed :as blessed] 9 | [fs :as fs] 10 | [tty :as tty] 11 | [common.stdin :as stdin] 12 | ["react-blessed" :as react-blessed] 13 | [floki.global.keys :as keys] 14 | [floki.global.subs] 15 | [floki.global.events] 16 | [floki.global.view :as view] 17 | [floki.debug.view :as v.debug])) 18 | 19 | (defn log-fn [& args] 20 | (swap! v.debug/logger conj (clojure.string/join " " args))) 21 | 22 | (defn error-input 23 | [& exceptions] 24 | {:format :unknown 25 | :data {:error "Unable to parse JSON/EDN" 26 | :exceptions exceptions}}) 27 | 28 | (defn json->edn 29 | [json] 30 | (when-not (= json "undefined") 31 | (js->clj (.parse js/JSON json)))) 32 | 33 | (defn convert 34 | [x] 35 | (let [res (try 36 | {:format :json 37 | :data (json->edn x)} 38 | (catch js/Error e0 39 | (try 40 | {:format :edn 41 | :data (conversion/edn-str->edn x)} 42 | (catch js/Error e1 43 | (try 44 | (let [tr (transit/reader :json)] 45 | {:format :edn 46 | :data (transit/read tr x)}) 47 | (catch js/Error e2 48 | (try 49 | {:format :json 50 | :data (json->edn (str x "}"))} 51 | (catch js/Error e3 52 | (error-input e0 e1 e2 e3 x)))))))))] 53 | 54 | (if (seq res) 55 | res 56 | (error-input "Empty document")))) 57 | 58 | 59 | (defn get-filename 60 | [] 61 | (->> (.-argv js/process) 62 | (drop 2) 63 | last)) 64 | 65 | (defn read-file-handler 66 | [err buf] 67 | (rf/dispatch [:input/set (-> buf str convert)])) 68 | 69 | (defn stdin-handler 70 | [buf] 71 | (rf/dispatch [:input/set (-> buf convert)])) 72 | 73 | (defonce filename (get-filename)) 74 | 75 | (defonce callback 76 | (if filename 77 | (fs/readFile filename read-file-handler) 78 | (stdin/handler stdin-handler))) 79 | 80 | (defonce tty-fd 81 | (fs/openSync "/dev/tty" "r+")) 82 | 83 | (defonce program 84 | (blessed/program #js {:input (tty/ReadStream tty-fd) 85 | :output (tty/WriteStream tty-fd)})) 86 | 87 | (defonce screen 88 | (doto 89 | (blessed/screen #js {:program program 90 | :autoPadding true 91 | :smartCSR true 92 | :title "Floki"}) 93 | keys/setup)) 94 | 95 | (defonce render 96 | (react-blessed/createBlessedRenderer blessed)) 97 | 98 | 99 | (defn load [] 100 | (-> (r/reactify-component view/root) 101 | (r/create-element #js {}) 102 | (render screen))) 103 | 104 | (defn -main [] 105 | (rf/dispatch-sync [:init]) 106 | (load)) 107 | 108 | (set! (.-log js/console) log-fn) 109 | 110 | (re-frame.loggers/set-loggers! {:log log-fn 111 | :warn log-fn}) 112 | (set! *main-cli-fn* -main) 113 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject floki "0.7.0-SNAPSHOT" 2 | :description "An EDN/JSON browser for the terminal" 3 | :url "https://github.com/denisidoro/floki" 4 | :min-lein-version "2.7.1" 5 | :license {:name "The Apache License, Version 2.0" 6 | :url "http://www.apache.org/licenses/LICENSE-2.0" 7 | :distribution :repo} 8 | :dependencies [[org.clojure/clojure "1.10.0"] 9 | [org.clojure/clojurescript "1.10.439"] 10 | [com.cognitect/transit-cljs "0.8.256"] 11 | [reagent "0.8.1" :exclusions [[cljsjs/react] 12 | [cljsjs/react-dom] 13 | [cljsjs/create-react-class]]] 14 | [re-frame "0.10.6"] 15 | [denisidoro/quark "0.6.0" :exclusion [[cheshire]]] 16 | [zprint "0.4.13"]] 17 | :plugins [[lein-cljsbuild "1.1.7" :exclusions [[org.clojure/clojure]]] 18 | [lein-figwheel "0.5.17"]] 19 | :source-paths ["src"] 20 | :clean-targets ["target" "node_modules" "package.json" "package-lock.json"] 21 | :cljsbuild {:builds [{:id "dev" 22 | :source-paths ["src"] 23 | :figwheel {:on-jsload "floki.core/load"} 24 | :compiler {:main floki.core 25 | :asset-path "target/js/compiled/dev" 26 | :output-to "target/js/compiled/floki.js" 27 | :output-dir "target/js/compiled/dev" 28 | :target :nodejs 29 | :optimizations :none 30 | :source-map-timestamp true 31 | :npm-deps {:blessed "0.1.81" 32 | :react-blessed "0.5.0" 33 | :react "16.6.3" 34 | :react-dom "16.6.3" 35 | :create-react-class "15.6.3" 36 | :clipboardy "1.2.3" 37 | :node-json-color-stringify "1.1.0" 38 | :ws "6.1.2"} 39 | :install-deps true}} 40 | {:id "prod" 41 | :source-paths ["src"] 42 | :compiler {:output-to "target/main.js" 43 | :output-dir "target/js/compiled/prod" 44 | :target :nodejs 45 | :optimizations :simple 46 | :npm-deps {:blessed "0.1.81" 47 | :react-blessed "0.5.0" 48 | :react "16.6.3" 49 | :react-dom "16.6.3" 50 | :node-json-color-stringify "1.1.0" 51 | :clipboardy "1.2.3" 52 | :create-react-class "15.6.3"} 53 | :install-deps true}}]} 54 | :profiles {:dev {:dependencies [[figwheel-sidecar "0.5.13"] 55 | [com.cemerick/piggieback "0.2.2"]] 56 | :repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}}}) 57 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. --------------------------------------------------------------------------------