Skip to content

Commit

Permalink
Simplify output writing (#163)
Browse files Browse the repository at this point in the history
* refactor: simplify output writing
* fix: unnecessary logging
  • Loading branch information
dainiusjocas authored Feb 17, 2022
1 parent 3b0316d commit 26486e2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 25 deletions.
28 changes: 13 additions & 15 deletions src/lmgrep/cli/analysis_conf.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,26 @@
(re-matches #".*[Ss]tem.*" (name (get token-filter :name))))
token-filters))
{:name (let [stemmer-kw (keyword (get flags :stemmer))]
(get stemmer
stemmer-kw
(do
(when stemmer-kw
(.println System/err
(format "Stemmer '%s' not found! EnglishStemmer is used." stemmer-kw)))
"englishMinimalStem")))})))
(or (get stemmer stemmer-kw)
(do
(when stemmer-kw
(.println System/err
(format "Stemmer '%s' not found! EnglishStemmer is used." stemmer-kw)))
"englishMinimalStem")))})))
(pos-int? (get flags :word-delimiter-graph-filter))
(cons {:name "worddelimitergraph"
:args (wdgf->token-filter-args
(get flags :word-delimiter-graph-filter))})))

(defn override-acm [acm flags]
(let [tokenizer (or (when-let [tokenizer-kw (get flags :tokenizer)]
(get tokenizer
tokenizer-kw
(do
(when tokenizer-kw
(.println System/err
(format "Tokenizer '%s' not found. StandardTokenizer is used." tokenizer-kw)))
{:name "standard"})))
(:tokenizer acm))
(or (get tokenizer tokenizer-kw)
(do
(when tokenizer-kw
(.println System/err
(format "Tokenizer '%s' not found. StandardTokenizer is used." tokenizer-kw)))
{:name "standard"})))
(get acm :tokenizer))
token-filters (override-token-filters (get acm :token-filters) flags)]
(assoc acm
:tokenizer tokenizer
Expand Down
5 changes: 2 additions & 3 deletions src/lmgrep/matching.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
format (get options :format)
scored? (or (get options :with-score) (get options :with-scored-highlights))]
(fn [line-nr line]
(if-let [highlights (seq (highlighter-fn line highlight-opts))]
(when-let [highlights (seq (highlighter-fn line highlight-opts))]
(let [details (cond-> {:line-number line-nr
:line line}
file-path (assoc :file file-path)
Expand All @@ -23,5 +23,4 @@
:edn (pr-str details)
:json (json/write-value-as-string details)
:string (formatter/string-output highlights details options)
(formatter/string-output highlights details options)))
""))))
(formatter/string-output highlights details options)))))))
14 changes: 7 additions & 7 deletions src/lmgrep/unordered.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
(.execute matcher-thread-pool-executor
^Runnable (fn []
(let [^String out-str (matcher-fn line-nr line)]
(if (.equals "" out-str)
(if out-str
(.execute writer-thread-pool-executor
^Runnable (fn [] (.println writer out-str)))
(when with-empty-lines
(.execute writer-thread-pool-executor
^Runnable (fn [] (.println writer out-str))))
(.execute writer-thread-pool-executor
^Runnable (fn [] (.println writer out-str)))))))
^Runnable (fn [] (.println writer out-str))))))))
(recur (.readLine rdr) (inc line-nr))))))

(defn ordered-consume-reader
Expand All @@ -45,10 +45,10 @@
^Callable (fn [] (matcher-fn line-nr line)))]
(.execute writer-thread-pool-executor
^Runnable (fn [] (let [out-str (.get f)]
(if (.equals "" out-str)
(if out-str
(.println writer out-str)
(when with-empty-lines
(.println writer out-str))
(.println writer out-str))))))
(.println writer "")))))))
(recur (.readLine rdr) (inc line-nr))))))

(defn grep [file-paths-to-analyze highlighter-fn options]
Expand Down

0 comments on commit 26486e2

Please sign in to comment.