A Hy pretty-printer / code formatter / beautifier.
Project description
🦑 Beautifhy
A Hy beautifier / code formatter / pretty-printer / enhanced REPL.
Probably compatible with Hy 1.0.0 and later.
Install
$ pip install -U beautifhy
REPL
Beautifhy comes with a REPL that implements multi-line editing, completion, live input validation and live syntax highlighting.
$ hy-repl
The behaviour of the repl may be modified with the following environment variables.
HY_HISTORY: Path to a file for storing command history. Defaults to~/.hy-history.HY_PYGMENTS_STYLE: The name of a pygments style to use for highlighting. Defaults tolightbulb.HY_LIVE_COMPLETION: If set, enables live/interactive autocompletion in a dropdown menu as you type. Defaults to off.
Usage: pretty-printer and syntax highlighter
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.
To convert python code to Hy (using py2hy), autoformat, then apply syntax highlighting, do
$ pip3 install py2hy
$ python3 -m py2hy some_code.py | beautifhy - | hylight -
Acknowledgements
The whole library uses pygments.
hy-repl also uses prompt-toolkit.
Plus, of course, Hy, whose REPL hy-repl extends.
The autoformatter relies on polymorphic dispatch provided by multimethod.
Docs
I'm lazy, so try clicking below.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file beautifhy-1.1.3.tar.gz.
File metadata
- Download URL: beautifhy-1.1.3.tar.gz
- Upload date:
- Size: 15.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
36df0458e03353c236badb2403aa9b1c86bb50344f48a5c435522915f906bbaf
|
|
| MD5 |
cdb1e5e331d5739f25efa73db013861b
|
|
| BLAKE2b-256 |
bd94e1dd16279f60b52370d824dc03662efe10eb09a47c0f787e3be97f8d024f
|
File details
Details for the file beautifhy-1.1.3-py3-none-any.whl.
File metadata
- Download URL: beautifhy-1.1.3-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23c00fcae012f63eecd18cdd80b621e0582779e3e7c5c156bdd5b37f7ed3bd88
|
|
| MD5 |
3dd0b7c15ae3a1c5e099d5b6d4175856
|
|
| BLAKE2b-256 |
cd57fff0d13354fbe2b5decdbb2b88e3551a1c37b8bd03e5364ecb74605cd489
|