2026-11-26 21:53:51 +01:00
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
from contextlib import suppress
|
|
|
|
|
from getpass import getpass
|
|
|
|
|
from textwrap import wrap
|
|
|
|
|
|
|
|
|
|
from .config import InstallError
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PreviousStepError(Exception):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlainTerminal:
|
|
|
|
|
step = None
|
|
|
|
|
can_go_back = False
|
2026-11-26 22:50:40 +01:00
|
|
|
context = ""
|
|
|
|
|
supports_key_picker = False
|
2026-11-26 21:53:51 +01:00
|
|
|
|
|
|
|
|
def heading(self, title):
|
2026-11-26 22:50:40 +01:00
|
|
|
context = f"{self.context}\n" if self.context else ""
|
|
|
|
|
print(f"\n{context}=== {title} ===", flush=True)
|
2026-11-26 21:53:51 +01:00
|
|
|
|
|
|
|
|
def show(self, message):
|
|
|
|
|
print(message, flush=True)
|
|
|
|
|
|
|
|
|
|
def status(self, message):
|
|
|
|
|
self.show(message)
|
|
|
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
|
print(f" {message}", file=sys.stderr)
|
|
|
|
|
|
|
|
|
|
def prompt(self, label, default="", *, hint=""):
|
|
|
|
|
if hint:
|
|
|
|
|
self.show(f" {hint}")
|
|
|
|
|
cursor = f" [{default}]" if default else ""
|
|
|
|
|
return input(f"{label}{cursor}: ").strip() or default
|
|
|
|
|
|
|
|
|
|
def choose(self, label, choices, default, *, hint=""):
|
|
|
|
|
options = ", ".join(choices)
|
|
|
|
|
while True:
|
|
|
|
|
value = self.prompt(f"{label} ({options})", default, hint=hint)
|
|
|
|
|
if value.lower() in choices:
|
|
|
|
|
return value.lower()
|
|
|
|
|
self.error(f"Choose {options}.")
|
|
|
|
|
|
|
|
|
|
def password(self, label):
|
|
|
|
|
if not sys.stdin.isatty():
|
|
|
|
|
raise InstallError(
|
|
|
|
|
"Use a terminal or provide a private password file."
|
|
|
|
|
)
|
|
|
|
|
return getpass(f"{label}: ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ScreenTerminal:
|
2026-11-26 22:50:40 +01:00
|
|
|
supports_key_picker = True
|
|
|
|
|
|
2026-11-26 21:53:51 +01:00
|
|
|
def __init__(self, window, curses):
|
|
|
|
|
self.window = window
|
|
|
|
|
self.curses = curses
|
|
|
|
|
self.title = ""
|
|
|
|
|
self.notes = []
|
|
|
|
|
self.problem = ""
|
|
|
|
|
self.step = None
|
2026-11-26 22:50:40 +01:00
|
|
|
self.context = ""
|
2026-11-26 21:53:51 +01:00
|
|
|
self.can_go_back = False
|
|
|
|
|
self.scroll = 0
|
|
|
|
|
|
|
|
|
|
def heading(self, title):
|
|
|
|
|
self.title = title
|
|
|
|
|
self.notes.clear()
|
|
|
|
|
self.problem = ""
|
|
|
|
|
self.scroll = 0
|
|
|
|
|
|
|
|
|
|
def show(self, message):
|
|
|
|
|
self.notes.extend(message.strip().splitlines())
|
|
|
|
|
|
|
|
|
|
def status(self, message):
|
|
|
|
|
self.show(message)
|
|
|
|
|
self._render()
|
|
|
|
|
|
|
|
|
|
def error(self, message):
|
|
|
|
|
self.problem = str(message)
|
|
|
|
|
|
|
|
|
|
def _line(self, row, text, width, attr=0):
|
|
|
|
|
# The terminal can resize between layout and painting.
|
|
|
|
|
with suppress(self.curses.error):
|
|
|
|
|
self.window.addnstr(row, 1, text, width, attr)
|
|
|
|
|
|
|
|
|
|
def _cursor(self, visible):
|
|
|
|
|
with suppress(self.curses.error):
|
|
|
|
|
self.curses.curs_set(visible)
|
|
|
|
|
|
|
|
|
|
def _render(self, label="", hint="", *, choices=(), selected=0, text=None):
|
|
|
|
|
win = self.window
|
|
|
|
|
height, columns = win.getmaxyx()
|
|
|
|
|
win.erase()
|
|
|
|
|
self._cursor(0)
|
|
|
|
|
width = max(0, columns - 3)
|
|
|
|
|
if height < 18 or columns < 48:
|
|
|
|
|
self._line(
|
|
|
|
|
0, "Resize the terminal to continue. Esc cancels.", width
|
|
|
|
|
)
|
|
|
|
|
win.refresh()
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
title = "Coulobre Panel setup"
|
|
|
|
|
if self.step:
|
|
|
|
|
title += f" Step {self.step[0]} of {self.step[1]}"
|
|
|
|
|
self._line(0, title, width, self.curses.A_REVERSE)
|
2026-11-26 22:50:40 +01:00
|
|
|
self._line(2, self.context, width, self.curses.A_DIM)
|
|
|
|
|
self._line(3, self.title, width, self.curses.A_BOLD)
|
2026-11-26 21:53:51 +01:00
|
|
|
choice_rows = min(len(choices), 5)
|
2026-11-26 22:50:40 +01:00
|
|
|
prompt_rows = choice_rows or int(text is not None)
|
2026-11-26 21:53:51 +01:00
|
|
|
notes = [line for note in self.notes for line in wrap(note, width)]
|
2026-11-26 22:50:40 +01:00
|
|
|
headers = [*wrap(self.problem, width), *wrap(hint, width)]
|
|
|
|
|
capacity = max(1, height - prompt_rows - 10)
|
|
|
|
|
if label:
|
|
|
|
|
capacity = min(capacity, max(9, len(headers)))
|
|
|
|
|
notes = headers + notes
|
2026-11-26 21:53:51 +01:00
|
|
|
max_scroll = max(0, len(notes) - capacity)
|
2026-11-26 22:50:40 +01:00
|
|
|
if not label:
|
|
|
|
|
self.scroll = max_scroll
|
2026-11-26 21:53:51 +01:00
|
|
|
self.scroll = min(self.scroll, max_scroll)
|
2026-11-26 22:50:40 +01:00
|
|
|
visible = notes[self.scroll : self.scroll + capacity]
|
|
|
|
|
problem_lines = len(wrap(self.problem, width))
|
|
|
|
|
for index, line in enumerate(visible):
|
|
|
|
|
attr = (
|
|
|
|
|
self.curses.A_BOLD
|
|
|
|
|
if self.scroll + index < problem_lines
|
|
|
|
|
else 0
|
|
|
|
|
)
|
|
|
|
|
self._line(index + 4, line, width, attr)
|
|
|
|
|
label_row = 5 + len(visible)
|
2026-11-26 21:53:51 +01:00
|
|
|
if max_scroll:
|
2026-11-26 22:50:40 +01:00
|
|
|
self._line(label_row, "Page Up / Page Down for details", width)
|
|
|
|
|
label_row += 1
|
2026-11-26 21:53:51 +01:00
|
|
|
|
|
|
|
|
self._line(label_row, label, width, self.curses.A_BOLD)
|
|
|
|
|
cursor_position = None
|
|
|
|
|
if choices:
|
|
|
|
|
first = max(0, selected - choice_rows + 1)
|
|
|
|
|
for index in range(first, min(first + choice_rows, len(choices))):
|
|
|
|
|
mark = "> " if index == selected else " "
|
|
|
|
|
attr = self.curses.A_REVERSE if index == selected else 0
|
|
|
|
|
self._line(
|
|
|
|
|
label_row + 1 + index - first,
|
|
|
|
|
mark + choices[index],
|
|
|
|
|
width,
|
|
|
|
|
attr,
|
|
|
|
|
)
|
|
|
|
|
elif text is not None:
|
|
|
|
|
value, cursor = text
|
|
|
|
|
first = max(0, cursor - width + 1)
|
|
|
|
|
self._line(
|
|
|
|
|
label_row + 1, value[first:], width, self.curses.A_UNDERLINE
|
|
|
|
|
)
|
|
|
|
|
cursor_position = (label_row + 1, min(width, cursor - first + 1))
|
|
|
|
|
|
|
|
|
|
footer_text = "Enter Next Esc Cancel"
|
|
|
|
|
if choices:
|
|
|
|
|
footer_text = "↑/↓ Select " + footer_text
|
|
|
|
|
if self.can_go_back:
|
|
|
|
|
footer_text += " F2 Back"
|
2026-11-26 22:50:40 +01:00
|
|
|
if label:
|
|
|
|
|
self._line(
|
|
|
|
|
label_row + prompt_rows + 2,
|
|
|
|
|
footer_text,
|
|
|
|
|
width,
|
|
|
|
|
self.curses.A_DIM,
|
|
|
|
|
)
|
2026-11-26 21:53:51 +01:00
|
|
|
if cursor_position:
|
|
|
|
|
with suppress(self.curses.error):
|
|
|
|
|
win.move(*cursor_position)
|
|
|
|
|
self._cursor(1)
|
|
|
|
|
win.refresh()
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _key(self):
|
|
|
|
|
key = self.window.get_wch()
|
|
|
|
|
if key in {"\x1b", "\x03"}:
|
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
if key == self.curses.KEY_F2 and self.can_go_back:
|
|
|
|
|
raise PreviousStepError
|
|
|
|
|
if key == self.curses.KEY_PPAGE:
|
|
|
|
|
self.scroll = max(0, self.scroll - 3)
|
|
|
|
|
elif key == self.curses.KEY_NPAGE:
|
|
|
|
|
self.scroll += 3
|
|
|
|
|
return key
|
|
|
|
|
|
|
|
|
|
def choose(self, label, choices, default, *, hint=""):
|
|
|
|
|
choices = tuple(choices)
|
|
|
|
|
index = choices.index(default)
|
|
|
|
|
while True:
|
|
|
|
|
ready = self._render(label, hint, choices=choices, selected=index)
|
|
|
|
|
key = self._key()
|
|
|
|
|
if not ready:
|
|
|
|
|
continue
|
|
|
|
|
if key in {self.curses.KEY_UP, self.curses.KEY_BTAB}:
|
|
|
|
|
index = (index - 1) % len(choices)
|
|
|
|
|
elif key in {self.curses.KEY_DOWN, "\t"}:
|
|
|
|
|
index = (index + 1) % len(choices)
|
|
|
|
|
elif key in {"\n", "\r", self.curses.KEY_ENTER}:
|
|
|
|
|
self.problem = ""
|
|
|
|
|
self.scroll = 0
|
|
|
|
|
return choices[index]
|
|
|
|
|
|
|
|
|
|
def _text(self, label, default="", *, hint="", secret=False):
|
|
|
|
|
value = str(default)
|
|
|
|
|
cursor, untouched = len(value), bool(value)
|
|
|
|
|
while True:
|
|
|
|
|
visible = "*" * len(value) if secret else value
|
|
|
|
|
ready = self._render(label, hint, text=(visible, cursor))
|
|
|
|
|
key = self._key()
|
|
|
|
|
if not ready:
|
|
|
|
|
continue
|
|
|
|
|
if key in {"\n", "\r", self.curses.KEY_ENTER}:
|
|
|
|
|
self.problem = ""
|
|
|
|
|
self.scroll = 0
|
|
|
|
|
return value if secret else value.strip()
|
|
|
|
|
if key == self.curses.KEY_LEFT:
|
|
|
|
|
cursor = max(0, cursor - 1)
|
|
|
|
|
elif key == self.curses.KEY_RIGHT:
|
|
|
|
|
cursor = min(len(value), cursor + 1)
|
|
|
|
|
elif key in {self.curses.KEY_HOME, "\x01"}:
|
|
|
|
|
cursor = 0
|
|
|
|
|
elif key in {self.curses.KEY_END, "\x05"}:
|
|
|
|
|
cursor = len(value)
|
|
|
|
|
elif key in {self.curses.KEY_BACKSPACE, "\x7f", "\b"}:
|
|
|
|
|
value = value[: max(0, cursor - 1)] + value[cursor:]
|
|
|
|
|
cursor = max(0, cursor - 1)
|
|
|
|
|
elif key == self.curses.KEY_DC:
|
|
|
|
|
value = value[:cursor] + value[cursor + 1 :]
|
|
|
|
|
elif key == "\x15":
|
|
|
|
|
value, cursor = "", 0
|
|
|
|
|
elif isinstance(key, str) and key.isprintable():
|
|
|
|
|
if untouched:
|
|
|
|
|
value, cursor = "", 0
|
|
|
|
|
value = value[:cursor] + key + value[cursor:]
|
|
|
|
|
cursor += 1
|
|
|
|
|
else:
|
|
|
|
|
continue
|
|
|
|
|
untouched = False
|
|
|
|
|
|
|
|
|
|
def prompt(self, label, default="", *, hint=""):
|
|
|
|
|
return self._text(label, default, hint=hint) or default
|
|
|
|
|
|
|
|
|
|
def password(self, label):
|
|
|
|
|
return self._text(label, secret=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def configure_terminal(callback):
|
|
|
|
|
if (
|
|
|
|
|
sys.stdin.isatty()
|
|
|
|
|
and sys.stdout.isatty()
|
|
|
|
|
and os.environ.get("TERM") not in {None, "", "dumb"}
|
|
|
|
|
):
|
|
|
|
|
try:
|
|
|
|
|
import curses
|
|
|
|
|
except ImportError:
|
|
|
|
|
return callback(PlainTerminal())
|
|
|
|
|
try:
|
|
|
|
|
curses.setupterm()
|
|
|
|
|
except curses.error:
|
|
|
|
|
return callback(PlainTerminal())
|
|
|
|
|
return curses.wrapper(
|
|
|
|
|
lambda screen: callback(ScreenTerminal(screen, curses))
|
|
|
|
|
)
|
|
|
|
|
return callback(PlainTerminal())
|