Permalink
Browse files

Merge pull request #28 from astanin/master

show-fns to lookup by function name, compact output
  • Loading branch information...
2 parents 67f0787 + 295146b commit 3ec4ca7e8152c3fcff173e3b66fabef4b46e6bae @samaaron samaaron committed Jul 9, 2012
Showing with 80 additions and 20 deletions.
  1. +14 −11 README.md
  2. +66 −9 src/quil/core.clj
View
@@ -99,17 +99,20 @@ Quil supports an explorable API. For a full list of API categories and subcatego
.
. etc
-In order to see the fns within a specific category use `(show-fns 10.1)` if `10.1` is the index of the category you wish to examine.
-
- user=> (show-fns 10.1)
- 2D Primitives
- - arc
- - ellipse
- - line
- - point
- - quad
- - rect
- - triangle
+In order to see the fns within a specific category use `(show-fns 11.1)` if `11.1` is the index of the category you wish to examine.
+
+ user=> (show-fns 11.1)
+ 11.1 2D Primitives
+ arc ellipse line point quad rect triangle
+
+You can also lookup functions by name of the category or function name.
+
+ user=> (show-fns "trans")
+ 14 Transform
+ apply-matrix pop-matrix print-matrix push-matrix reset-matrix rotate
+ rotate-x rotate-y rotate-z scale shear-x shear-y translate
+ 14.1 Utility Macros
+ with-translation
If you know the start of Processing API method name such as `bezier`, you can use `(show-meths "bezier")` to list all Processing API methods starting with `bezier` alongside their Quil equivalents:
View
@@ -4247,16 +4247,73 @@
(:subcategories cat))))
cats))))
+(defn- wrap-lines
+ "Split a list of words in lists (lines) not longer than width chars each,
+ space between words included."
+ [width words]
+ (letfn [(wrap-lines-rec
+ [ws accum-lns]
+ (if (empty? ws)
+ accum-lns
+ (let [lens (map count ws)
+ cumlens (reduce
+ (fn [sums len]
+ (conj sums
+ (if (empty? sums)
+ len
+ (+ (last sums) len 1))))
+ [] lens)
+ [line other] (split-with #(> width %) cumlens)
+ [line other] (split-at (count line) ws)]
+ (recur other (conj accum-lns line)))))]
+ (wrap-lines-rec words [])))
+
+(defn- pprint-wrapped-lines
+ "Pretty print words across several lines by wrapping lines at word boundary."
+ [words & {:keys [fromcolumn width] :or {fromcolumn 0 width 80}}]
+ (let [w (- width fromcolumn)
+ wordss (wrap-lines w words)
+ indent (apply str (repeat fromcolumn \space))
+ lines (map #(apply str indent (interpose " " %)) wordss)]
+ (doseq [l lines] (println l))))
+
(defn show-fns
- "Print all the functions within category or subcategory specified by
- cat-idx (use print-cats to see a list of index nums). If a category is
- specified, it will not print out the fns in any of cat's
- subcategories. "
- [cat-idx]
- (let [res (get (all-category-map) (str cat-idx))]
- (println (:name res))
- (dorun
- (map #(println " -" %) (:fns res)))))
+ "If given a number, print all the functions within category or
+ subcategory specified by the category index (use show-cats to see a
+ list of index nums).
+
+ If given a string or a regular expression, print all the functions
+ whose name or category name contains that string.
+
+ If a category is specified, it will not print out the fns in any of
+ cat's subcategories."
+ [q]
+ (letfn [(list-category [cid c & {:keys [only]}]
+ (let [category-fns (:fns c)
+ display-fns (if (nil? only)
+ category-fns
+ (clojure.set/intersection
+ (set only) (set category-fns)))
+ names (sort (map str display-fns))]
+ (if (not (empty? names))
+ (do
+ (println cid (:name c))
+ (pprint-wrapped-lines names :fromcolumn 4)))))
+ (show-fns-by-cat-idx [cat-idx]
+ (let [c (get (all-category-map) (str cat-idx))]
+ (list-category cat-idx c)))
+ (show-fns-by-name-regex [re]
+ (doseq [[cid c] (sort-by key (all-category-map))]
+ (let [in-cat-name? (re-find re (:name c))
+ matching-fns (filter #(re-find re (str %)) (:fns c))
+ in-fn-names? (not (empty? matching-fns))]
+ (cond
+ in-cat-name? (list-category cid c) ;; print an entire category
+ in-fn-names? (list-category cid c :only matching-fns)))))]
+ (cond
+ (string? q) (show-fns-by-name-regex (re-pattern (str "(?i)" q)))
+ (isa? (type q) java.util.regex.Pattern) (show-fns-by-name-regex q)
+ :else (show-fns-by-cat-idx q))))
(defn show-meths
"Takes a string representing the start of a method name in the

0 comments on commit 3ec4ca7

Please sign in to comment.