"""Modal dialogs: run sanity, check in, tag, confirm, message, register new files, help.""" import os import shlex from rich.table import Table from rich.text import Text from textual.binding import Binding from textual.containers import Horizontal, Vertical, VerticalScroll from textual.screen import ModalScreen from textual.widgets import Button, Checkbox, Input, Label, RadioButton, RadioSet, SelectionList, Static from ..ops import sanity_form_error from .widgets import ACCENT, DIM def file_notes(paths, limit=8): notes = [(" • " + p, "") for p in paths[:limit]] if len(paths) > limit: notes.append((" … and %d more" % (len(paths) - limit), "dim")) return notes class Dialog(ModalScreen): """Title, notes, a body of fields, an error line and buttons. Enter submits, Esc cancels.""" BINDINGS = [Binding("escape", "cancel", "Cancel")] OK_LABEL = "OK" CANCEL_LABEL = "Cancel" def __init__(self, heading, notes=()): super().__init__(classes="dialog-screen") self.heading = heading self.notes = list(notes) def compose(self): with Vertical(id="dialog"): yield Label(self.heading, id="dialog-title") with VerticalScroll(id="dialog-body"): for text, kind in self.notes: yield Static(Text(text), classes="note note-%s" % (kind or "plain")) yield from self.body() yield Label("", id="dialog-error") with Horizontal(id="dialog-buttons"): yield Button(self.OK_LABEL, variant="primary", id="ok") if self.CANCEL_LABEL: yield Button(self.CANCEL_LABEL, id="cancel") def body(self): return iter(()) def on_mount(self): first = self.first_focus() (self.query_one(first) if first else self.query_one("#ok")).focus() def first_focus(self): return None def values(self): return True def validate(self, values): return None def submit(self): values = self.values() error = self.validate(values) if error: self.query_one("#dialog-error", Label).update("✘ " + error) return self.dismiss(values) def on_button_pressed(self, event): event.stop() if event.button.id == "ok": self.submit() else: self.dismiss(None) def on_input_submitted(self, event): event.stop() self.submit() def action_cancel(self): self.dismiss(None) def field(text): return Label(text, classes="field") def radio_set(options, pressed=0, id=None): return RadioSet(*[RadioButton(o, value=i == pressed) for i, o in enumerate(options)], id=id, compact=True) class MessageDialog(Dialog): OK_LABEL, CANCEL_LABEL = "Close", "" class ConfirmDialog(Dialog): CANCEL_LABEL = "Cancel" def __init__(self, heading, notes=(), yes="Yes"): super().__init__(heading, notes) self.OK_LABEL = yes class SanityDialog(Dialog): """Configs, what to do on pass, comment, tag, refresh. Returns a dict for new_job().""" OK_LABEL = "Run sanity" def __init__(self, notes, configs, tags, trunk, last_msg): super().__init__("Run sanity", notes) self.configs, self.tags, self.trunk, self.last_msg = configs, tags, trunk, last_msg def body(self): yield field("Sanity configs") yield SelectionList(*[(c, c, True) for c in self.configs], id="configs", compact=True) yield field("On pass") yield radio_set(["Check in", "Unlock only (check in later)"], id="mode") yield field("Check-in comment") yield Input(value=self.last_msg, placeholder="what changed and why", id="msg") yield field("Then tag") yield radio_set(["none"] + self.tags, id="tag") yield Checkbox("Refresh unmodified files to %s first" % self.trunk, value=True, id="refresh", compact=True, classes="field-check") def first_focus(self): return "#msg" def values(self): chosen = set(self.query_one("#configs", SelectionList).selected) tag_index = self.query_one("#tag", RadioSet).pressed_index return dict(configs=[c for c in self.configs if c in chosen], mode="checkin" if self.query_one("#mode", RadioSet).pressed_index == 0 else "unlock", msg=self.query_one("#msg", Input).value.strip(), tag=self.tags[tag_index - 1] if tag_index > 0 else "", refresh=self.query_one("#refresh", Checkbox).value) def validate(self, values): return sanity_form_error(values, self.configs) class CheckinDialog(Dialog): OK_LABEL = "Check in" def __init__(self, heading, notes, tags, msg): super().__init__(heading, notes) self.tags, self.msg = tags, msg def body(self): yield field("Check-in comment") yield Input(value=self.msg, placeholder="what changed and why", id="msg") yield field("Then tag") yield radio_set(["none"] + self.tags, id="tag") def first_focus(self): return "#msg" def values(self): i = self.query_one("#tag", RadioSet).pressed_index return dict(msg=self.query_one("#msg", Input).value.strip(), tag=self.tags[i - 1] if i > 0 else "") def validate(self, values): return None if values["msg"] else "a check-in comment is required" class TagDialog(Dialog): OK_LABEL = "Tag" def __init__(self, notes, tags): super().__init__("Batch tag", notes) self.tags = tags def body(self): yield field("Tag") yield radio_set(self.tags, id="tag") def first_focus(self): return "#tag" def values(self): return self.tags[max(0, self.query_one("#tag", RadioSet).pressed_index)] class AddFilesDialog(Dialog): OK_LABEL = "Register" def __init__(self, ws): super().__init__("Register new files", [ ("Brand-new files aren't in the vault, so dssc can't see them as modified. Register them here and " "they're checked in with the rest (same as `dsflow add`).", "dim")]) self.ws = ws def body(self): yield field("Path(s), workspace-relative, space separated") yield Input(placeholder="rtl/rx/rx_new.sv", id="paths") def first_focus(self): return "#paths" def values(self): try: return shlex.split(self.query_one("#paths", Input).value) except ValueError: return None def validate(self, paths): if not paths: return "give one or more workspace-relative paths" bad = [p for p in paths if not os.path.isfile(os.path.join(self.ws, p))] return "not a file: %s" % ", ".join(bad) if bad else None HELP_KEYS = [ ("Views", [("1-5", "Changes, Locks, Checked-out, Sanity run, Logs (or click a tab)"), ("↑↓ PgUp PgDn", "move; the mouse works too (click, scroll)"), ("/", "filter files by path; esc closes the filter"), ("ctrl+p", "command palette: themes, keys, screenshot")]), ("Files", [("space enter", "mark / unmark (no marks = the highlighted row)"), ("a esc", "mark everything in the tab / clear marks"), ("n", "register brand-new files for check-in"), ("m", "rebase stale files onto Trunk:Latest"), ("l u", "lock / release a lock"), ("t", "batch-tag the marked files")]), ("Sanity", [("s", "run sanity: lock, refresh, every config, then check in or unlock"), ("c", "check in marked files a passing run already covers"), ("x", "cancel the run, or clean up after a lost runner")]), ("", [("r q", "refresh from DesignSync / quit (a running sanity keeps going)")]), ] class HelpDialog(Dialog): OK_LABEL, CANCEL_LABEL = "Close", "" def __init__(self, version, conf): super().__init__("dsflow_tui %s · keys" % version) self.conf = conf def body(self): for section, keys in HELP_KEYS: if section: yield field(section) grid = Table.grid(padding=(0, 2)) grid.add_column(style="bold " + ACCENT, no_wrap=True, min_width=14) grid.add_column() for key, text in keys: grid.add_row(key, text) yield Static(grid) yield Static(Text("\nconf %s" % self.conf, style=DIM, overflow="fold"))