Beautifhy
๐ฆ - beautifhy, a Hy code autoformatter / pretty-printer / code beautifier.
Install / Use
/learn @atisharma/BeautifhyREADME
๐ฆ Beautifhy
A Hy beautifier / code formatter / pretty-printer / linter.
Probably compatible with Hy 1.0.0 and later.
Install
$ pip install -U beautifhy
The pygments style may be modified with the environment variable
HY_PYGMENTS_STYLE. This sets the name of a pygments style to use for
highlighting. Defaults to lightbulb.
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])
(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 -
Usage: linter
hylint checks Hy source for syntax errors and common issues.
$ hylint myfile.hy
With --style / -s, also runs opinionated style checks (camelCase names,
missing docstrings, earmuff variables):
$ hylint --style myfile.hy
With --error-only / -e, only errors are shown (suppresses warnings and info):
$ hylint --error-only myfile.hy
Reads from stdin with -:
$ cat myfile.hy | hylint -
Exit code is 1 if any errors are found, 0 otherwise. Suitable for CI.
Lint rules (always on):
(defn f (x) ...)โ use[]for parameter lists, not()(defn f [...] (do ...))โ remove redundantdo(fn [...] (do ...))โ remove redundantdo(when cond (do ...))โ remove redundantdo(if cond (do ...))โ use(when cond ...)(do expr)โ redundant single-expressiondo(+ x 0),(* x 1)โ identity arithmetic(+ x 1)โ(inc x),(- x 1)โ(dec x)(from hyrule)
Style rules (--style):
- camelCase function names โ use kebab-case
- Missing docstrings on
defn/defmacro - Trailing commas in parameter lists
- Earmuff variables (
*var*) โ use UPPER_SNAKE or kebab-case
Acknowledgements
The whole library uses pygments. The autoformatter relies on polymorphic dispatch provided by multimethod.
Docs
The docstrings are not bad. Otherwise, try clicking below.
Related Skills
node-connect
353.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
111.6kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
353.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
353.1kQQBot ๅฏๅชไฝๆถๅ่ฝๅใไฝฟ็จ <qqmedia> ๆ ็ญพ๏ผ็ณป็ปๆ นๆฎๆไปถๆฉๅฑๅ่ชๅจ่ฏๅซ็ฑปๅ๏ผๅพ็/่ฏญ้ณ/่ง้ข/ๆไปถ๏ผใ
