@yahyahammad/venastine 0.3.14 → 0.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/tui/themes.py CHANGED
@@ -29,8 +29,11 @@ here sets it: these fourteen are RGB palettes, which is the whole
29
29
  premise of the contrast floors they are held to.
30
30
  """
31
31
 
32
+ import colorsys
32
33
  import logging
33
34
 
35
+ from rich.color import Color as RichColor
36
+ from rich.color import ColorSystem
34
37
  from textual.color import Color
35
38
  from textual.theme import Theme
36
39
 
@@ -332,7 +335,102 @@ def _tint(colour: str, background: str,
332
335
  return Color.parse(colour).blend(Color.parse(background), factor).hex
333
336
 
334
337
 
335
- def role_styles(theme: Theme) -> dict[str, str]:
338
+ # Batch 257. How many colours the terminal can show, by the names Rich's
339
+ # `Console.color_system` reports them under. A blended band is designed
340
+ # in truecolour, and Rich ROUNDS every colour to the nearest one the
341
+ # terminal has. On 256 colours that sent dark-plain's added-line band
342
+ # (#1E2F27) to #000000 -- the page's own colour, so the band vanished --
343
+ # while the removed-line band (#35201F) landed on #5f0000 and stayed.
344
+ # Measured on the owner's WSL install, which runs with
345
+ # TERM=xterm-256color and no COLORTERM, so Rich picks 256 there.
346
+ TRUECOLOUR = "truecolor"
347
+ EIGHT_BIT = "256"
348
+ SIXTEEN = frozenset({"standard", "windows"})
349
+
350
+ #: The six hues the xterm cube can draw at a band's depth, in the order
351
+ #: of the hue circle from red. A channel is 1 where that hue has it.
352
+ _CUBE_HUES = ((1, 0, 0), (1, 1, 0), (0, 1, 0), (0, 1, 1), (0, 0, 1),
353
+ (1, 0, 1))
354
+
355
+ #: The same six as the terminal's own colour names, for a sixteen-colour
356
+ #: terminal: the colours git, diff and patch use there.
357
+ _ANSI_HUES = ("red", "yellow", "green", "cyan", "blue", "magenta")
358
+
359
+ #: The cube levels a 256-colour band takes, as (channel absent, channel
360
+ #: present). The cube's levels are 0, 95, 135, 175, 215 and 255.
361
+ #: On a dark page the band is the hue at the cube's first step out of
362
+ #: black (#005f00, #5f0000). On a light page it is the hue two steps in
363
+ #: from white (#afd7af, #d7afaf); one step in (#d7ffd7, #ffd7d7) stands
364
+ #: off light-plain's page at 1.06:1 and 1.13:1, under X7's 1.15 floor.
365
+ _CUBE_DARK = (0, 1)
366
+ _CUBE_LIGHT = (3, 4)
367
+
368
+
369
+ def _hue_sector(colour: str) -> int:
370
+ """Which of the six cube hues `colour` is nearest, as an index into
371
+ `_CUBE_HUES`. Rounding the hue to a sixth of the circle is all the
372
+ resolution a band at one cube step has."""
373
+ red, green, blue = Color.parse(colour).rgb
374
+ hue, _sat, _val = colorsys.rgb_to_hsv(red / 255, green / 255, blue / 255)
375
+ return round(hue * 6) % 6
376
+
377
+
378
+ def _cube_band(colour: str, dark: bool) -> int:
379
+ """`colour`'s hue as a band on the xterm cube, as a colour number.
380
+
381
+ Placed on the cube rather than blended and left to Rich to round,
382
+ because the rounding is what lost the band: the cube is fixed, so
383
+ a band chosen on it is the band the terminal paints. Still derived
384
+ from the theme's own hue (#116), only at the cube's resolution."""
385
+ absent, present = _CUBE_DARK if dark else _CUBE_LIGHT
386
+ red, green, blue = (present if channel else absent
387
+ for channel in _CUBE_HUES[_hue_sector(colour)])
388
+ return 16 + 36 * red + 6 * green + blue
389
+
390
+
391
+ def _as_256(colour: str) -> int:
392
+ """The colour number Rich paints `colour` as on a 256-colour
393
+ terminal: its own downgrade, so the comparison below is against the
394
+ page the reader actually sees."""
395
+ return RichColor.parse(colour).downgrade(ColorSystem.EIGHT_BIT).number
396
+
397
+
398
+ def _diff_styles(palette: dict[str, str], dark: bool,
399
+ depth: str | None) -> tuple[str, str]:
400
+ """The added and removed rows' styles at the terminal's colour depth.
401
+
402
+ Truecolour, and a depth Rich could not name, keep X7's blended band.
403
+ 256 colours put the band on the cube (`_cube_band`). Sixteen colours
404
+ have no band to give, so the row takes the hue as its TEXT colour,
405
+ the ANSI themes' rule. An ANSI theme keeps that rule at every depth:
406
+ its colours are the terminal's own, with no RGB to measure.
407
+
408
+ A 256-colour band that would land on the page's own colour, or on
409
+ the other band, falls back to the sixteen-colour form. No shipped
410
+ theme reaches it (tests/test_themes.py holds that); a theme from
411
+ ctrl+p's list might.
412
+ """
413
+ success, error = palette["success"], palette["error"]
414
+ page, text = palette["background"], palette["foreground"]
415
+ measured = all(c.startswith("#") for c in (success, error, page))
416
+ if not measured:
417
+ add, delete = _tint(success, page), _tint(error, page)
418
+ return (f"{text} on {add}" if add else success,
419
+ f"{text} on {delete}" if delete else error)
420
+ as_text = (_ANSI_HUES[_hue_sector(success)],
421
+ _ANSI_HUES[_hue_sector(error)])
422
+ if depth in SIXTEEN:
423
+ return as_text
424
+ if depth == EIGHT_BIT:
425
+ add, delete = _cube_band(success, dark), _cube_band(error, dark)
426
+ if add == delete or _as_256(page) in (add, delete):
427
+ return as_text
428
+ return f"{text} on color({add})", f"{text} on color({delete})"
429
+ return (f"{text} on {_tint(success, page)}",
430
+ f"{text} on {_tint(error, page)}")
431
+
432
+
433
+ def role_styles(theme: Theme, depth: str | None = TRUECOLOUR) -> dict[str, str]:
336
434
  """Rich style strings keyed by transcript role, for one theme.
337
435
 
338
436
  Roles are what a line MEANS, not where it came from: `pass` covers a
@@ -344,10 +442,16 @@ def role_styles(theme: Theme) -> dict[str, str]:
344
442
  leave a slot at None or fill it with textual's `ansi_*` names,
345
443
  and one of them took the whole app down at mount. `_palette`
346
444
  fills and translates; nothing below can see the difference.
445
+
446
+ `depth` is the terminal's colour depth as Rich names it, and only
447
+ the two diff rows read it (batch 257): they are the one place this
448
+ table sets a background, and a background is what Rich's rounding
449
+ can merge into the page. Every other role is a foreground, which
450
+ rounding moves but cannot hide. The default is truecolour, which is
451
+ what every caller but `styles_for` means.
347
452
  """
348
453
  palette = _palette(theme)
349
- add = _tint(palette["success"], palette["background"])
350
- delete = _tint(palette["error"], palette["background"])
454
+ diff_add, diff_del = _diff_styles(palette, theme.dark, depth)
351
455
  return {
352
456
  # Who is speaking. The hues, because these distinguish identity.
353
457
  "user": f"bold {palette['primary']}",
@@ -443,10 +547,12 @@ def role_styles(theme: Theme) -> dict[str, str]:
443
547
  # was the alternative, and DIFF_TINT's comment above turns that
444
548
  # down for every theme that CAN be measured; picking it for the
445
549
  # one that cannot would be backwards.
446
- "diff_add": (f"{palette['foreground']} on {add}" if add
447
- else palette["success"]),
448
- "diff_del": (f"{palette['foreground']} on {delete}" if delete
449
- else palette["error"]),
550
+ #
551
+ # And unless the terminal cannot show the blend. `_diff_styles`
552
+ # puts the band on the xterm cube at 256 colours and falls back
553
+ # to the foreground form at sixteen (batch 257).
554
+ "diff_add": diff_add,
555
+ "diff_del": diff_del,
450
556
  # Unchanged context recedes: it is there to place the change,
451
557
  # not to be read. `secondary` rather than a dimmed foreground
452
558
  # for X1's reason -- `dim` is an attribute no floor can
@@ -509,6 +615,21 @@ def role_styles(theme: Theme) -> dict[str, str]:
509
615
  _UNSTYLABLE: set[str] = set()
510
616
 
511
617
 
618
+ def colour_depth(app) -> str | None:
619
+ """How many colours `app`'s terminal shows, as Rich names it.
620
+
621
+ Read off Textual's own console, because that console is what renders
622
+ the screen: Rich decides the depth there from COLORTERM and TERM
623
+ (TEXTUAL_COLOR_SYSTEM overrides it), and downgrades every colour to
624
+ it on the way out. None when it cannot be read, which `role_styles`
625
+ treats as truecolour -- what this module did before it asked.
626
+ """
627
+ try:
628
+ return app.console.color_system
629
+ except Exception: # noqa: BLE001 -- no console yet; keep the blend
630
+ return None
631
+
632
+
512
633
  def styles_for(app) -> dict[str, str]:
513
634
  """The active theme's role styles, or unstyled when there is no app.
514
635
 
@@ -550,7 +671,7 @@ def styles_for(app) -> dict[str, str]:
550
671
  if theme is None:
551
672
  return {}
552
673
  try:
553
- return role_styles(theme)
674
+ return role_styles(theme, colour_depth(app))
554
675
  except Exception: # noqa: BLE001 -- a theme this build cannot resolve
555
676
  name = getattr(theme, "name", "?")
556
677
  if name not in _UNSTYLABLE:
package/tui/widgets.py CHANGED
@@ -430,7 +430,7 @@ class PostureBadge(Static):
430
430
  §40 (UN3). The startup banner scrolls away; a posture does not. The
431
431
  case this exists for is not the researcher who just typed a flag --
432
432
  it is the user who set `ALLOW_INSECURE_SANDBOX_FALLBACK` months ago
433
- and forgot, or who inherited a config.py from somewhere. Neither of
433
+ and forgot, or who inherited a config.yaml from somewhere. Neither of
434
434
  them reads app.log.
435
435
 
436
436
  Hidden when the posture is the shipped one, GoalBanner-style, so it
@@ -1197,14 +1197,23 @@ class AgentPanel(Static):
1197
1197
  self.update(as_content(body))
1198
1198
 
1199
1199
  class SessionPanel(Static):
1200
- """What is running in the background, and nothing else (§49, SS14).
1201
-
1202
- LIVE SESSIONS ONLY, which is the decision rather than a consequence
1203
- of how it is fed. A finished session is still readable -- SS14 puts
1204
- that on the tool-call line that started it, resolved at press time
1205
- the way §47 resolves a spawn -- so listing finished ones here would
1206
- spend contested sidebar rows on a second route to the same pane,
1207
- and would grow without bound in a session that starts many.
1200
+ """What is running in the background, and what just finished (§49,
1201
+ SS14 as amended in batch 258).
1202
+
1203
+ RUNNING FIRST, THEN THE FIVE MOST RECENT FINISHED, marked. This was
1204
+ "live sessions only", and that was measured wrong in use: a session
1205
+ left the panel the moment it ended, and its end is also what wakes
1206
+ the agent, so the row vanished just as a new turn began. It read as
1207
+ the turn hiding it, and nobody could tell which sessions had finished
1208
+ without opening each one. The tool-call line still opens a session
1209
+ of any age; this is the glance, and five keeps it bounded in a
1210
+ session that starts many. The cap is app.py's, with the filter.
1211
+
1212
+ A ROW SAYS HOW IT ENDED in the research panel's three marks (E14):
1213
+ `▸` running, `☑` exited 0, `☒` anything else -- a non-zero code,
1214
+ killed, timed out, or failed to start -- with the outcome after the
1215
+ command. The outcome is fitted FIRST and the command gives way to
1216
+ it, because the outcome is the half a finished row exists to show.
1208
1217
 
1209
1218
  PROCESS-WIDE, like AgentPanel above it and like ctrl+b's picker.
1210
1219
  SS11 scopes the five TOOLS to the caller's own thread, which is a
@@ -1220,14 +1229,19 @@ class SessionPanel(Static):
1220
1229
  surface only through check_output_policy, and keeping that call in
1221
1230
  app.py means this file never holds an unredacted one.
1222
1231
 
1223
- Hidden when nothing is running, GoalBanner-style, which is almost
1224
- always -- the sidebar is twenty columns wide and its rows are
1225
- contested.
1232
+ Hidden until a session has started, GoalBanner-style -- the sidebar
1233
+ is twenty columns wide and its rows are contested.
1226
1234
  """
1227
1235
 
1228
1236
  #: AgentPanel's, measured the same way and for the same box.
1229
1237
  WIDTH = 18
1230
1238
 
1239
+ #: `status` -> (mark, style role). The research panel's pass marks
1240
+ #: and roles, so the sidebar's two progress lists read as one family.
1241
+ STATUS = {"running": (MARK_RUNNING, "tool"),
1242
+ "done": (MARK_DONE, "pass_done"),
1243
+ "failed": (MARK_FAILED, "error")}
1244
+
1231
1245
  def __init__(self, **kwargs):
1232
1246
  super().__init__(**kwargs)
1233
1247
  self._rows: list = []
@@ -1243,8 +1257,11 @@ class SessionPanel(Static):
1243
1257
  return themes.styles_for(app)
1244
1258
 
1245
1259
  def show(self, rows) -> None:
1246
- """Draw the live sessions. `rows` is [{"id", "label"}], already
1247
- redacted, in the order they started.
1260
+ """Draw the sessions. `rows` is [{"id", "label", "status",
1261
+ "outcome"}], already redacted and in the order to draw: the
1262
+ running ones as they started, then the finished, newest first.
1263
+ `status` is a key of STATUS and defaults to running; `outcome` is
1264
+ empty while a session runs.
1248
1265
 
1249
1266
  An empty list hides the widget, which is how the panel closes
1250
1267
  without a second method meaning the same thing (ThreadCrumb's
@@ -1279,10 +1296,19 @@ class SessionPanel(Static):
1279
1296
  body = Text()
1280
1297
  body.append("sessions\n\n")
1281
1298
  for row in self._rows:
1282
- label = AgentPanel._fit(row.get("label", ""), self.WIDTH - 2)
1299
+ mark, role = self.STATUS.get(row.get("status", "running"),
1300
+ self.STATUS["running"])
1301
+ outcome = row.get("outcome", "")
1302
+ room = self.WIDTH - 2
1303
+ if outcome:
1304
+ # The outcome is never cut: a row is the mark, a space,
1305
+ # and sixteen cells, and the command takes what is left.
1306
+ room -= cell_len(outcome) + 1
1307
+ label = AgentPanel._fit(row.get("label", ""), room)
1308
+ text = f"{label} {outcome}" if outcome else label
1283
1309
  body.append(
1284
- f"{MARK_RUNNING} {label}\n",
1285
- meta_style(styles.get("tool", ""), "session", row.get("id")))
1310
+ f"{mark} {text}\n",
1311
+ meta_style(styles.get(role, ""), "session", row.get("id")))
1286
1312
  # AgentPanel's note: the nowrap/clip rule lives on the widget in
1287
1313
  # app.tcss, because `Content` carries neither flag.
1288
1314
  self.display = True