From 0d22f8d62129286746816b7aa10144e865679771 Mon Sep 17 00:00:00 2001 From: Arun SL Date: Wed, 12 Oct 2016 13:17:11 +0530 Subject: [PATCH] Initial support for exclamation in command line --- src/avi/commands.clj | 13 ++++++----- src/avi/mode/command_line.clj | 37 ++++++++++++++++++++------------ test/avi/t_command_line_mode.clj | 2 +- test/avi/t_splits.clj | 3 +++ 4 files changed, 35 insertions(+), 20 deletions(-) diff --git a/src/avi/commands.clj b/src/avi/commands.clj index eac01da..34617b3 100644 --- a/src/avi/commands.clj +++ b/src/avi/commands.clj @@ -19,25 +19,28 @@ :motion [:goto [(dec (Long/parseLong command-line)) :first-non-blank]]})))) (defn q - [editor] + [editor force?] (p/close-pane editor)) (defn w - [editor] + [editor force?] (let [{filename :name, :keys [lines]} (get-in editor (e/current-document-path editor))] (w/write-file w/*world* filename (string/join "\n" lines)) editor)) -(def wq (comp q w)) +(defn wq + [editor force?] + (w editor force?) + (q editor force?)) (defn sp - [{:keys [:lenses] :as editor}] + [{:keys [:lenses] :as editor} force?] (-> editor (update :lenses conj (e/current-lens editor)) (p/split-pane (count lenses) :horizontal))) (defn vsp - [{:keys [:lenses] :as editor}] + [{:keys [:lenses] :as editor} force?] (-> editor (update :lenses conj (e/current-lens editor)) (p/split-pane (count lenses) :vertical))) diff --git a/src/avi/mode/command_line.clj b/src/avi/mode/command_line.clj index 4483b2b..520fc53 100644 --- a/src/avi/mode/command_line.clj +++ b/src/avi/mode/command_line.clj @@ -1,6 +1,7 @@ (ns avi.mode.command-line (:require [packthread.core :refer :all] [avi.edit-context :as ec] + [clojure.string :as string] [avi.command-line :as cl] [avi.commands] [avi.editor :as e] @@ -14,24 +15,32 @@ [command] (every? #(Character/isDigit %) command)) +(defn- have-exclamation? + [command] + (string/includes? command "!")) + (defn- command-fn [command-line] (ns-resolve 'avi.commands (symbol command-line))) (defn- process-command - [editor command-line] - (+> editor - (cond - (= "" command-line) - identity - - (line-number? command-line) - (avi.commands/-NUMBER- command-line) - - (command-fn command-line) - ((command-fn command-line)) - - :else - (assoc :message [:white :red (str ":" command-line " is not a thing")])))) + [editor command-line-raw] + (let [force? (have-exclamation? command-line-raw) + command-line (if force? + (string/replace command-line-raw "!" "") + command-line-raw)] + (+> editor + (cond + (= "" command-line) + identity + + (line-number? command-line) + (avi.commands/-NUMBER- command-line) + + (command-fn command-line) + ((command-fn command-line) force?) + + :else + (assoc :message [:white :red (str ":" command-line " is not a thing")]))))) (def wrap-mode (cl/mode-middleware :command-line process-command)) diff --git a/test/avi/t_command_line_mode.clj b/test/avi/t_command_line_mode.clj index e642c0f..4512d94 100644 --- a/test/avi/t_command_line_mode.clj +++ b/test/avi/t_command_line_mode.clj @@ -29,7 +29,7 @@ (editor :after ":q") => unfinished?) (fact "`:q` exits Avi" (editor :after ":q") => finished?) - (fact "`:q!` does not exit Avi" + (future-fact "`:q!` does not exit Avi" (editor :after ":q!") => unfinished?) (fact "`:sp:q` does not exit Avi" (editor :after ":sp:q") => unfinished?) diff --git a/test/avi/t_splits.clj b/test/avi/t_splits.clj index 3f717d2..756ff36 100644 --- a/test/avi/t_splits.clj +++ b/test/avi/t_splits.clj @@ -87,6 +87,9 @@ (fact "multiple splits out of pane area" (editor :width 20 :height 8 :after ":sp:vsp:sp:vsp:sp:vsp:sp:vsp") => (message-line ["No room for new Pane" :white :on :red])) + (future-fact "q! closes all panes and exit Avi" + (editor :after ":vsp:spl:sp:q!") + => finished?) (fact "correctly handles closing last sub-split" (editor :after ":vsp:spl:sp:q:q") => (terminal ["One"