Created
May 24, 2019 07:32
-
-
Save pauldub/73ee3501382ed85dc9b49a398e501da8 to your computer and use it in GitHub Desktop.
janet zsh-like prompt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add ~/.janet to module/paths | |
(let [base-dir (string (os/getenv "HOME") "/.janet")] | |
(array/insert module/paths 0 | |
(tuple (string base-dir "/:all:/:name:.:native:") :native)) | |
(array/insert module/paths 0 | |
(tuple (string base-dir "/:all:.:native:") :native)) | |
(array/insert module/paths 0 | |
(tuple (string base-dir "/:all:/init.janet") :source)) | |
(array/insert module/paths 0 | |
(tuple (string base-dir "/:all:.janet") :source)) | |
(array/insert module/paths 0 | |
(tuple (string base-dir "/:all:") :source))) | |
# Go inspired implementation of string trimming. | |
(defn- make-cutset-func [cutset] | |
(fn [c] | |
(not= nil (peg/match ~(some (set ,cutset)) c)))) | |
(defn- string/index-func [s f truth] | |
(var res -1) | |
(var continue? true) | |
(var i 0) | |
(while (and continue? (< i (length s))) | |
(let [c (get s i)] | |
(if (= (f (string/from-bytes c)) truth) | |
(do (set continue? false) | |
(set res i))) | |
(++ i))) | |
res) | |
(defn- string/last-index-func [s f truth] | |
(var res -1) | |
(var continue? true) | |
(var i (dec (length s))) | |
(while (and continue? (> i 0)) | |
(let [c (get s i)] | |
(if (= (f (string/from-bytes c)) truth) | |
(do (set continue? false) | |
(set res (inc i)))) | |
(-- i))) | |
res) | |
(defn- string/trim-right-func [s f] | |
(let [i (string/last-index-func s f false)] | |
(if (= -1 i) | |
s | |
(string/slice s 0 i)))) | |
(defn- string/trim-left-func [s f] | |
(let [i (string/index-func s f false)] | |
(if (= -1 i) | |
"" | |
(string/slice s i)))) | |
(defn string/trim-func [s f] | |
"TrimFunc returns a copy of the string s with all leading and trailing Unicode code points c satisfying f(c) removed." | |
(string/trim-right-func | |
(string/trim-left-func s f) f)) | |
(defn string/trim [s cutset] | |
"Trim returns a copy of string s with all leading and trailing Unicode code points contained in cutset removed." | |
(if (or (= s "") (= cutset "")) | |
s | |
(string/trim-func s (make-cutset-func cutset)))) | |
# Prompt customization | |
(import prompt) | |
(set *get-prompt* | |
(fn get-prompt [p] | |
(prompt/color :bold | |
:light-magenta "➜ " | |
:cyan (prompt/cwd) " " | |
(prompt/git-info) | |
(parser/state p) | |
:reset))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(import sh) | |
(def- escape-sequences '{ | |
:default 39 | |
:black 30 | |
:red 31 | |
:green 32 | |
:yellow 32 | |
:blue 34 | |
:magenta 35 | |
:cyan 36 | |
:light-gray 37 | |
:dark-gray 90 :light-red 91 | |
:light-green 92 | |
:light-yellow 93 | |
:light-blue 94 | |
:light-magenta 95 | |
:light-cyan 96 | |
:white 97 | |
:reset 0 | |
:bold 1 | |
:dim 2 | |
:underline 4 | |
:blink 5 | |
:reverse 7 | |
:hidden 8 }) | |
(defn color [& parts] | |
"Allows to color terminal output using shell escape code in a DSL-like fashion." | |
(string | |
;(map (fn [part] | |
(if (keyword? part) | |
(string "\e[" (get escape-sequences part) "m") | |
(string part))) parts))) | |
(defn cwd [] | |
"Formats the current working directory." | |
(let [cwd (os/cwd)] | |
(if (= cwd (string "/home/" (os/getenv "USER"))) | |
"~" | |
(string/trim (sh/$$ basename (os/cwd)) "\n")))) | |
(var *git-prefix* (color :bold :blue "git:(" :red)) | |
(var *git-suffix* "") | |
(var *git-dirty* (color :blue ") " :light-yellow "✗ ")) | |
(var *git-clean* (color :blue ") ")) | |
(defn- has-git? [] | |
"Tests wether the current working directory is a git repository or not." | |
(= 0 (sh/$? git rev-parse --short HEAD :2>1 :1>/dev/null))) | |
(defn- git-dirty [] | |
"Returns an indicator if the git repository is dirty" | |
(if (> (length (string/trim (sh/$$ git status --porcelain --ignore-submodules=dirty --untracked-files=no :2>/dev/null | tail -n1) "\n")) 0) | |
*git-dirty* | |
*git-clean*)) | |
(defn- git-ref [] | |
"Returns the current repository ref or branch" | |
(let [ref (string/trim (sh/$$ git symbolic-ref HEAD :2>/dev/null) "\n") | |
commit (string/trim (sh/$$ git rev-parse --short HEAD :2>/dev/null) "\n")] | |
(if (> (length ref) 0) | |
(string/replace "refs/heads/" "" ref) | |
commit))) | |
(defn git-info [] | |
"Formats git information for janetsh prompt." | |
(if (has-git?) | |
(color *git-prefix* :red (git-ref) (git-dirty) *git-suffix* ) | |
"")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This issue got me in a few places. You may need to add these escapes too:
andrewchambers/janetsh#118