"""unicurses drawing helpers."""

from __future__ import annotations

from pygittools.tui.ucurses import clrtoeol, move, mvaddstr  # type: ignore[import-untyped]


def clear_row(row: int) -> None:
	"""Clear a screen row before multi-segment redraws."""
	move(row, 0)
	clrtoeol()


def draw_text(row: int, col: int, text: str, width: int, attr: int = 0) -> int:
	"""Write ``text`` at ``col`` without clearing the rest of the row."""
	if col >= width or not text:
		return col
	clipped = text[: max(0, width - col - 1)]
	if not clipped:
		return col
	move(row, col)
	if attr:
		mvaddstr(row, col, clipped, attr)
	else:
		mvaddstr(row, col, clipped)
	return col + len(clipped)


def draw_line(row: int, col: int, text: str, width: int, attr: int = 0) -> None:
	"""Write ``text`` on one row, clipped to the terminal width."""
	if col == 0:
		clear_row(row)
	draw_text(row, col, text, width, attr)