Skip to main content

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 to lightbulb.
  • 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.

Ask DeepWiki

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

beautifhy-1.1.5.tar.gz (15.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

beautifhy-1.1.5-py3-none-any.whl (16.4 kB view details)

Uploaded Python 3

File details

Details for the file beautifhy-1.1.5.tar.gz.

File metadata

  • Download URL: beautifhy-1.1.5.tar.gz
  • Upload date:
  • Size: 15.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for beautifhy-1.1.5.tar.gz
Algorithm Hash digest
SHA256 6379b0e7401be06ef11877a9dc9b14d6026c392fb1476feb26529d408feee4c6
MD5 acbecad24a871170f40e97db13084d35
BLAKE2b-256 dc0eda92473310b60991e4022cf2e96bc4eec894f935c936dff397e1ff0c61b7

See more details on using hashes here.

File details

Details for the file beautifhy-1.1.5-py3-none-any.whl.

File metadata

  • Download URL: beautifhy-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 16.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for beautifhy-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b0c015f59f9e71aaa92c8d23dc667583667c269ef70d722db96d7a9b43f8ad47
MD5 aa1a9c7a27f00d92bf501094dae59d9b
BLAKE2b-256 dbe6111ec9a0d8a763948a9077f5354c62d3e07f58b72ae037483e87d022cf0c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page