8000 GitHub - atisharma/beautifhy: 🦑 - beautifhy, a Hy code autoformatter / pretty-printer / code beautifier.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

🦑 - beautifhy, a Hy code autoformatter / pretty-printer / code beautifier.

License

Notifications You must be signed in to change notification settings

atisharma/beautifhy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🦑 Beautifhy

A Hy beautifier / code formatter / pretty-printer.

Probably compatible with Hy 1.0.0 and later.

Install

$ pip install -U beautifhy

If you want syntax highlighting available (which requires pygments), do instead

$ pip install -U beautifhy[hylight]

Usage

From the command line, to pretty-print the file core.hy:

$ beautifhy core.hy

gives the output

(import toolz [first second last])

 ;; * Utility things
 ;; -----------------------------------------

(defmacro defmethod [#* args]
  "Define a multimethod (using multimethod.multimethod).
  For example, the Hy code

  `(defmethod f [#^ int x #^ float y]
    (// x (int y)))`

  is equivalent to the following Python code:

  `@multimethod
  def f(x: int, y: float):
      return await x // int(y)`

  You can also define an asynchronous multimethod:

  `(defmethod :async f [#* args #** kwargs]
    (await some-async-function #* args #** kwargs))`
  "
  (if (= :async (first args))
    (let [f (second args) body (cut args 2 None)]
      `(defn :async [hy.I.multimethod.multimethod] ~f ~@body))
    (let [f (first args) body (cut args 1 None)]
      `(defn [hy.I.multimethod.multimethod] ~f ~@body))))


(defn slurp [fname #** kwargs]
  "Read a file and return as a string.
  kwargs can include mode, encoding and buffering, and will be passed
  to open()."
  (let [f (if (:encoding kwargs None) hy.I.codecs.open open)]
    (with [o (f fname #** kwargs)]
      (o.read))))


(defmacro rest [xs]
  "A slice of all but the first element of a sequence."
  `(cut ~xs 1 None))

To apply syntax highlighting (no pretty-printing), do

$ hylight core.hy

You can use stdin and pipe by replacing the filename with -:

$ beautifhy core.hy | hylight -

which will pretty-print core.hy and then syntax highlight the output.

About

🦑 - beautifhy, a Hy code autoformatter / pretty-printer / code beautifier.

Resources

License

Stars

Watchers

Forks

0