"""Code: the trace of a behaviour through the code, as numbered steps with code chips and excerpts.
CSS: code.css."""
from .glyphs import glyph
from .links import text
from .page import e
def code_chip(c, commit=''):
"""A chip shaped like a smart link, linking a file at the cited lines: the angle-brackets glyph, then the file
name, "idleTimer.ts". Its tooltip holds the path, the lines and the commit, and screen readers hear the lines
after the name. A test chip adds the test's name to both.
Data: {"path", "lines": [first, last], "url", "name"?}. name marks a supporting test.
code_chip({"path": "packages/session/src/idleTimer.ts", "lines": [8, 31], "url": "#"}, commit='4f2c9a1')
"""
first, last = c['lines']
at = f' at {commit}' if commit else ''
test = f'Test: {c["name"]}' if c.get('name') else ''
tip = f'{c["path"]} · lines {first} to {last}{at}' + (f' · {test}' if test else '')
hidden = f', lines {first} to {last}' + (f', {test}' if test else '')
return (f''
f'{glyph("angle-brackets")}{e(c["path"].rsplit("/", 1)[-1])}'
f'{e(hidden)}'
' (opens in a new tab)')
def excerpt(lines):
"""A collapsed "Show the code" disclosure holding numbered lines, after ADS CodeBlock: the code font, line
numbers in subtlest text in their own column, an ellipsis row between runs that aren't next to each other,
and scrolling inside its own box, which takes keyboard focus.
Data: [{"n", "code"}, {"gap": true}, ...], up to 12 lines with secrets cut.
excerpt([{"n": 4, "code": "export const DEFAULT_IDLE_TIMEOUT_MINUTES = 30;"}, {"gap": True}, {"n": 9, "code": "}"}])
"""
digits = max((len(str(x['n'])) for x in lines if not x.get('gap')), default=1)
rows = '\n'.join('…' if x.get('gap')
else f'{e(x["n"])}{e(x["code"])}' for x in lines)
return (f'{glyph("chevron-right")}Show the code
'
f'{rows}
Step {i}. {text(s)}
' + (f'