Metadata-Version: 2.4
Name: cui-py
Version: 0.2.0
Summary: Cython bindings for the cui UI framework
Keywords: cui,ui,cython,bindings
Author-Email: Shakeeb Alireza <shakfu@users.noreply.github.com>
License-Expression: MIT AND OFL-1.1 AND ISC
License-File: LICENSE
License-File: thirdparty/cui/LICENSE
License-File: thirdparty/cui/THIRD_PARTY.md
License-File: thirdparty/cui/assets/fonts/LICENSE-Geist.txt
License-File: thirdparty/cui/assets/fonts/LICENSE-Inter.txt
License-File: thirdparty/cui/assets/fonts/LICENSE-Lucide.txt
License-File: thirdparty/cui-repl/LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Cython
Classifier: Typing :: Typed
Requires-Python: >=3.11
Description-Content-Type: text/markdown

# cui-py

Cython bindings for [CUI](https://github.com/yappologistic/CUI), an immediate-mode UI library in C99 with about 190 components. The distribution is `cui-py`; the import name is `cui`.

CUI 1.5.0 is vendored in `thirdparty/cui` and compiled into the extension, so there are no runtime dependencies. Linux needs the X11 development headers to build (`libx11-dev`).

Status: alpha. Tested on macOS (Cocoa, arm64, CPython 3.14). The CI workflows build macos, Linux and Windows.

```python
import cui

clicks = 0

def frame(ui: cui.Context) -> None:
    global clicks
    with ui.column(pad=32, gap=16):
        ui.heading("Hello, CUI", 2)
        if ui.button("Click me", variant=cui.BUTTON_PRIMARY):
            clicks += 1
        ui.text(f"Clicked {clicks} times")

cui.App("Hello", 420, 300).run(frame)
```

`examples/` has `hello.py`, `todo.py` (a port of CUI's tutorial app), `dashboard.py` and `showcase.py` (forms, charts, a node graph, a live Markdown editor and a console; `--png DIR` saves each page headless).

## Install

cui-py needs CPython 3.11 or later. One `abi3` wheel per platform covers every later version. The macOS wheels need macOS 13 or later.

```bash
pip install cui-py
```

or if you want to build from source:

```bash
git clone https://github.com/shakfu/cui-py && cd cui-py
uv sync          # builds the extension
make test
```

## How the C API maps to Python

- **Names.** `cui_button_ex` is `ui.button`. `CUI_BUTTON_PRIMARY` is `cui.BUTTON_PRIMARY`. Icons are strings: `cui.ICON_PLUS`.

- **Options.** The fields of a C options struct become keyword-only arguments. An argument you leave out keeps CUI's default.

- **Outputs.** A pointer the component writes to becomes a return value, after the component's own result:

  ```python
  changed, value = ui.checkbox("Remember me", value)
  changed, index = ui.select("fruit", ["apple", "pear"], index)
  ```

- **Option pointers.** An option C reads and writes takes a `cui.Ref`. Read `.value` after the frame, since some components write it at the end of a scope:

  ```python
  sel = cui.Ref(-1)
  ui.treemap("t", items, selected=sel)
  if sel.value >= 0: ...
  ```

  An option array takes a list. cui-py sets its count from the list, or checks the list against the length CUI reads, such as one icon per segment.

- **Text fields.** A text field edits a `cui.TextBuffer(capacity, value)` in place. Capacity counts bytes, including the trailing NUL, as in C.

- **Log and terminal.** `ui.log_view` reads a `cui.LogBuffer` and `ui.terminal` a `cui.TerminalBuffer`. Both are rings that drop the oldest lines when full, and any thread may write to them:

  ```python
  log = cui.LogBuffer(lines=4096)
  log.append("disk almost full", cui.LOG_WARN)    # from any thread
  ui.log_view("log", log, timestamps=True)

  r = ui.terminal("term", term)
  if r.command is not None:
      term.write(f"\x1b[32mran\x1b[0m {r.command}\n")
  ```

  A write made after the view in the same frame shows from the next frame. CUI cannot be woken from another thread, so call `ui.request_wakeup(seconds)` while a thread is writing.

- **Lists.** A list of strings or floats replaces a C array and its count. A list of records replaces an array of structs. Each record can be a namedtuple (`cui.VizItem(label="a", value=3)`), a tuple, or a dict. Missing fields are zero.

- **Containers.** A begin/end pair is a context manager. When `begin` can decline, as a closed dialog does, the scope is falsy and `end` is skipped:

  ```python
  with ui.dialog("Settings", open) as d:
      if d:
          ui.text("inside")
  open = d.open
  ```

  The scope exposes the pair's outputs as attributes (`d.open`, `vl.first`). The value `end` returns is in `.result`.

- **Values.**
  - Colors: `0xRRGGBB`, `(r, g, b[, a])`, `cui.rgb()`, or theme tokens such as `cui.COLOR_ACCENT`.

  - Sizes: a number in points, `cui.FILL`, `cui.pct(50)` or `cui.grow(2)`.

  - Dates: `datetime.date`, or `None` for unset.

- **Images.** `image_create` and `image_update` take any C-contiguous buffer of RGBA bytes: `bytes`, `memoryview`, or a NumPy `(h, w, 4)` uint8 array.

- **Drawing.** `ui.canvas(id, width, height, paint)` calls `paint(rect)` after layout. Only there do the `draw_*` methods work. `focusable=True` makes it a Tab stop. With `component_input=True` it also receives keys and text through `ctx.dispatch_input(event, route)`, replaying events an input hook (`ctx.set_input_hook`) captured.

`cui.Context()` with no window renders headless with CUI's software renderer. Its parts:

- input arrives through `ui.input_*`

- `render()` and `save_png()` read the pixels

- copy and paste use `ctx.clipboard`

The tests use this.

## Terminal on a pty

`cui.Pty` runs a child process on a pseudo-terminal, and `ui.pty(id, p)` shows it. [libghostty-vt](https://lib.ghostty.org/vt/) 1.0.0 parses the output. The view code comes from [cui-repl](thirdparty/cui-repl/VENDORED). The Linux and macOS wheels include it. Windows has no pty, and `cui.PTY_AVAILABLE` is false there.

A source build leaves it off. To turn it on you need [Zig 0.17](https://lib.ghostty.org/vt/installing.html) and CMake 3.19 or later:

```bash
make build-pty                                           # from a checkout
pip install cui-py --no-binary cui-py -C cmake.define.CUI_PY_PTY=ON
```

The first build downloads libghostty-vt (SHA-256 pinned in `CMakeLists.txt`).

```python
with cui.Pty(["python3", "-q"]) as p:
    for ui in app.frames():
        ui.pty("repl", p)
        if p.exited:
            break
```

- The view reads the child's output each frame. A Pty no view shows stalls a child that writes a lot.
- `p.write()` sends input, and `p.screen()` reads the screen as text.
- A Pty works only on the thread that made it.

`examples/term.py` is a terminal window.

## Errors instead of crashes

Misuse that would be undefined behaviour in C raises an exception:

- building outside a frame

- building inside a paint callback

- drawing outside one

- ending a frame with a scope open

- using a closed context

- passing strings with NUL characters

- passing arrays of unequal length, or of a length CUI would read past

- building UI or injecting input inside an input hook or route

- updating a region outside an image, or a texture that is not one

- passing color channels outside 0..255

- writing more text than a buffer holds

Strings, arrays and `Ref`s passed to CUI stay alive until the frame is painted.

The vendored CUI carries one patch, for state shared by two component types with one id. A narrower case remains; see [docs/dev/known-issues.md](docs/dev/known-issues.md).

## Coverage

The generator wraps 421 entries: 403 Context methods, including 53 scopes, and 18 module functions. Another 48 functions are wrapped by hand. [docs/dev/coverage.md](docs/dev/coverage.md) lists the 34 functions not wrapped yet and why, and the 12 options that have no keyword argument.

## Development

`tools/gen_bindings.py` reads CUI's headers with libclang and writes these files:

- `src/cui/_cui.pxd`

- `src/cui/_gen.pxi`

- `src/cui/_core.pyi`

- `docs/dev/coverage.md`

The output is committed. Run `make gen` after updating `thirdparty/cui`. Hand-written code lives in `src/cui/_core.pyx`.

| Target | |
|-|-|
| `make build` | rebuild the extension |
| `make build-pty` | rebuild with `cui.Pty` (needs Zig 0.17) |
| `make test` | headless tests, including one call to every generated method |
| `make test-window` | also open a hidden window |
| `make test-asan` | tests under AddressSanitizer (macOS) |
| `make qa` | lint, format check, mypy, tests |

## License

MIT. The vendored CUI is MIT, with its fonts under OFL (Inter, Geist) and ISC (Lucide). See `thirdparty/cui/LICENSE`, `thirdparty/cui/THIRD_PARTY.md` and `thirdparty/cui/assets/fonts/`. A `CUI_PY_PTY` build also links cui-repl and libghostty-vt, both MIT. Their licenses go in `cui/licenses/`.
