SRCL Documentation

repository·main·Indexed 23 days ago

https://github.com/internet-development/www-sacred

An open-source React component and style repository (version 2.0.6) for building web, desktop, and static applications with a terminal aesthetic. SRCL emphasizes monospace character spacing and includes a lightweight, zero-dependency CLI framework to maintain visual parity between web and terminal interfaces. It provides utilities for ANSI color manipulation, terminal cursor control, and a `createApp` function for building interactive terminal applications.

Tokens
11.4K
Snippets
17
Records
55
Agent score
81%

What's inside srcl

  1. Performance discipline for www-sacred

    main

    The fast-typescript-check discipline is used to ensure www-sacred components remain fast to type-check and performant at runtime. This is specifically critical for the ASCII/canvas animation components that run inside a requestAnimationFrame loop.

    Target Components:

    • components/ASCIICanvas.tsx (Reference implementation)
    • components/MatrixLoader.tsx (Falling-glyph effect)
    • components/CanvasSnake.tsx, components/DOMSnake.tsx, components/CanvasPlatformer.tsx (Interactive games)
    • components/examples/OneLineLoaders.tsx, components/BarLoader.tsx, components/BlockLoader.tsx, components/BarProgress.tsx (Spinners)

    Scope:

    • Part 1 (Runtime): Applies only to the animation family above. Focuses on minimizing CPU cycles and DOM mutations per frame.
    • Part 2 (Compiler): Applies to the entire repository to tighten tsc --noEmit wall-clock time.
  2. React-to-CLI Primitive Mapping

    main

    Use this mapping to translate Sacred React components into Simulacrum CLI primitives:

    React surfaceCLI primitiveNotes
    <Card title="T">cardTop('T', innerW) + cardBot(innerW)Top + bottom borders
    Content <div> inside <Card>cardRow(text, innerW)2ch left indent, padded to inner width
    Key-value pair (gradient)kvTableGradient([[k, v]])24ch key column, gradient on value
    <thead / <tr>cardHeaderRow(formatRow(TH, COL_SPEC, innerW), innerW)#585858 background
    <tbody> / <tr>cardRow(formatRow(row, COL_SPEC, innerW), innerW)Per-cell alignment, status coloring
    styles.statusOk/statusOffcolSpec: { status: true }Semantic coloring (Green/Gray)
    <ActionButton hotkey="ESC">button('ESC', 'exit')Hotkey + label background pair
    <RowSpaceBetween>buttonRow(left, right, innerW)Left + right justify with windowBg gap
    Word-wrapped paragraphwordWrap(text, innerW - 6)Card padding is 3ch each side
  3. Naming conventions for Python Sacred CLI ports

    main

    The Python port of the Sacred framework is a one-to-one mirror of the JavaScript framework, but uses snake_case for all function and method names.

    JavaScriptPython
    cardTop(title, innerW)card_top(title, inner_w)
    cardRow(content, innerW)card_row(content, inner_w)
    cardSelectRow(content, innerW, selected)card_select_row(content, inner_w, selected)
    cardHeaderRow(content, innerW)card_header_row(content, inner_w)
    cardBot(innerW)card_bot(inner_w)
    formatRow(vals, colSpec, innerW)format_row(vals, col_spec, inner_w)
    kvTable(pairs)kv_table(pairs)
    kvTableGradient(pairs)kv_table_gradient(pairs)
    buttonRow(left, right, innerW)button_row(left, right, inner_w)
    wordWrap(text, maxW)word_wrap(text, max_w)
    createApp({ build })create_app(build=build)

    Note: button(hotkey, label) and the COLORS dictionary retain their original casing.

  4. Best practices and pitfalls when porting sacred UI

    main

    Do's

    • Keep the React component file structure identical to the original sacred source to allow for clean git diff updates in the future.
    • Prefix sacred primitives to avoid collisions with host components (e.g., use SacredButton instead of Button).
    • Use independent sacred theming (via its own state) for "console" or "debugger" overlays.

    Don'ts

    • Do not import global.css into the host; it will break global element styles like body, ul, and ol.
    • Do not rewrite sacred components to consume host theme tokens; this breaks the isolation required for future updates.
    • Do not re-export sacred primitives using the same names as host primitives.
  5. Coding conventions in www-sacred

    main

    Follow these established patterns to maintain consistency:

    • Comments: Use //NOTE(jimmylee): in TS/JS and # NOTE(jimmylee): in Python. Comment the why, not the what. If the code is clear, omit the comment.
    • Naming: Use descriptive names (e.g., candidateCount instead of cnt). Do not use a __private prefix; use plain const for module-private state or React refs for internal state.
    • Simulacrum CLI: This is a zero-dependency TypeScript framework run via tsx. Do not import its Node-only code (which uses process.stdout) into React components.
    • Types: Prefer as const objects over enum to ensure zero runtime overhead and better tree-shaking. Avoid const enum due to isolatedModules: true constraints.
    const DIRECTION = { Up: 'UP', Down: 'DOWN', Left: 'LEFT', Right: 'RIGHT' } as const;
    type Direction = (typeof DIRECTION)[keyof typeof DIRECTION];
  6. Runtime performance rules for animation loops

    main

    When writing code inside a requestAnimationFrame loop, follow these rules to minimize per-frame costs:

    1. Cache refs and property chains: Hoist ref.current and property lookups into local variables before entering the loop.
    2. Diff before DOM mutation: Only update textContent or style properties if the value has actually changed. This prevents unnecessary layout/reflow work.
    3. Use indexed for-loops: Avoid map, filter, forEach, or sort inside the loop to prevent per-frame closure allocations. Avoid for...of as it invokes the iterator protocol.
    4. Pre-allocate buffers: Allocate arrays and grids during setup/build phases. Never allocate new objects or arrays inside the frame loop to avoid GC pressure.
    5. Guard the entire loop: Use an IntersectionObserver to stop the animation loop entirely when the component is off-screen.
    6. Use bitwise floor for positive math: Use x | 0 instead of Math.floor(x) for truncating positive integers (e.g., grid indexing).
    7. Maintain type lanes: Keep numbers in a consistent representation (e.g., keep loop indices as integers and wave math as doubles) to avoid V8 representation changes.
    8. Strip diagnostic code: Remove console.log, JSON.stringify, and extra performance.now() calls from the production frame path.
  7. Audit runtime performance for animation loops

    main

    When writing code inside a requestAnimationFrame loop, use this checklist to ensure high performance:

    • Cache Refs: Read refs and property chains once and store them in a local variable before the loop starts.
    • Diff before DOM writes: Guard textContent or style.* updates with a check to see if the value actually changed.
    • No Allocations: Do not use new, or create closures (like map, filter, or sort callbacks) inside the loop.
    • Pre-allocate Buffers: Re-use arrays or grids; only rebuild them on resize.
    • Early Return: Use IntersectionObserver to gate the loop so it returns early when the element is off-screen.
    • Numeric Stability: Keep numeric variables in one 'lane' (e.g., use integer indices or consistent double math).
    • Clean Path: Remove all console.log and extra performance.now() calls from the frame path.
  8. Porting Sacred UI to different platforms

    main

    If you want to write a custom CLI screen or port a React surface to a terminal, SRCL provides specific guides (Skills) covering the necessary conventions:

    • Port to TypeScript CLI: skills/port-sacred-terminal-ui-to-typescript-cli/SKILL.md
    • Port to Python: skills/port-sacred-terminal-ui-to-python/SKILL.md
    • Port to React (using same conventions): skills/port-sacred-terminal-ui-to-react-using-same-conventions/SKILL.md
    • Port to a hostile React codebase: skills/port-sacred-terminal-ui-to-hostile-react-codebase/SKILL.md
  9. Verify Python CLI parity and run tests

    main

    To ensure your Python CLI matches the React implementation, use the built-in parity test suite.

    Commands:

    • npm run cli:python: Renders your specific screen in the terminal. Press ESC to quit.
    • npm run test:python: Runs the Python-specific unit tests. This command regenerates the reference fixture (scripts/python/sacred_cli/__tests__/fixtures/reference.json) before running tests.
    • npm test: Runs the full suite (JS framework tests + Python parity suite).

    Troubleshooting Parity Failures: If a parity test fails, it usually means a TypeScript module was updated without updating its Python mirror.

    1. Identify the failing assertion (e.g., test_format_row_status).
    2. Compare the TS module (e.g., scripts/cli/lib/table.ts) with its Python mirror (scripts/python/sacred_cli/table.py).
    3. Port the logic changes to Python using snake_case.
    4. Re-run npm test to automatically update the reference fixture.
    npm test                # JS framework unit tests + Python parity suite
    npm run cli:python      # your screen renders, ESC quits cleanly
    npm run test:python     # only the Python suite
  10. Port Sacred Terminal UI to React

    main

    Use this skill to transform a CLI screen written for the Simulacrum framework (found in scripts/cli/templates/*.ts or scripts/python/templates/*.py) into a React component. The goal is to achieve layout parity using sacred's existing React primitives without re-implementing animations or box-drawing logic.

    Workflow

    1. Analyze the CLI screen: Identify cardTop/cardRow/cardBot blocks and buttonRow structures.
    2. Create the React file: Place in components/examples/ (for demos) or components/ (for reusable surfaces). Use 'use client' only if browser APIs are required.
    3. Map components:
      • cardTop blocks $\rightarrow$ <Card title="...">.
      • Data tables $\rightarrow$ <SimpleTable data={[...]} />.
      • buttonRow $\rightarrow$ <RowSpaceBetween> with <ActionButton> children.
    4. Inherit theming: Do not import scripts/cli/colors.json. The React components automatically consume the ANSI palette via CSS custom properties (e.g., var(--theme-background)) defined in global.css.

    Layout & Constraints

    • Width: Replace the CLI's innerW concept with fluid browser layouts. Use <ContentFluid> or <Block> containers.
    • Avoid Canvas: Do not use <ASCIICanvas> or attempt to port CLI animations; focus on the static layout.
    • Avoid CLI Libs: Do not copy scripts/cli/lib/* into the React tree; use CSS Modules instead.
  11. Run SRCL tests

    main

    SRCL uses a multi-gate testing process. Running the full test suite executes TypeScript type-checking, the vitest suite, and a Python unittest suite (which includes a parity test between the JS and Python CLI runtimes).

    • Full test suite: npm test (Requires python3 on your PATH for the Python suite).
    • JavaScript suite only: npm run test:js.
    • Python suite only: npm run test:python.
    npm test