@f5-sales-demo/pi-tui 23.8.0 → 23.9.1

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "@f5-sales-demo/pi-tui",
4
- "version": "23.8.0",
4
+ "version": "23.9.1",
5
5
  "description": "Terminal User Interface library with differential rendering for efficient text-based applications",
6
6
  "homepage": "https://github.com/f5-sales-demo/xcsh",
7
7
  "author": "Can Boluk",
@@ -37,8 +37,8 @@
37
37
  "fmt": "biome format --write ."
38
38
  },
39
39
  "dependencies": {
40
- "@f5-sales-demo/pi-natives": "23.8.0",
41
- "@f5-sales-demo/pi-utils": "23.8.0",
40
+ "@f5-sales-demo/pi-natives": "23.9.1",
41
+ "@f5-sales-demo/pi-utils": "23.9.1",
42
42
  "marked": "18.0.11"
43
43
  },
44
44
  "devDependencies": {
@@ -1,5 +1,6 @@
1
1
  import * as path from "node:path";
2
2
  import { pathToFileURL } from "node:url";
3
+ import { isCodeBlockComplete, presentCode, wrapCodeLine } from "@f5-sales-demo/pi-utils/code-presentation";
3
4
  import { Marked, type Token, type TokenizerExtension, type Tokens } from "marked";
4
5
  import { renderLatex } from "../latex";
5
6
  import type { SymbolTheme } from "../symbols";
@@ -277,6 +278,7 @@ export interface MarkdownTheme {
277
278
  code: (text: string) => string;
278
279
  codeBlock: (text: string) => string;
279
280
  codeBlockBorder: (text: string) => string;
281
+ codeBlockBackground?: (text: string) => string;
280
282
  quote: (text: string) => string;
281
283
  quoteBorder: (text: string) => string;
282
284
  hr: (text: string) => string;
@@ -700,19 +702,41 @@ export class Markdown implements Component {
700
702
  }
701
703
  }
702
704
 
703
- const indentWidth = Math.min(this.#codeBlockIndent, Math.max(0, width - 1));
704
- const codeIndent = padding(indentWidth);
705
- if (token.lang) lines.push(...wrapTextWithAnsi(this.#theme.codeBlockBorder(token.lang), width));
705
+ const view = presentCode(
706
+ token.text,
707
+ token.lang,
708
+ isCodeBlockComplete(token.raw, token.codeBlockStyle === "indented"),
709
+ );
710
+ const indentWidth = Math.min(this.#codeBlockIndent, Math.max(0, width - 5));
711
+ const prefixWidth = Math.min(width - 1, indentWidth + 4);
712
+ const available = Math.max(1, width - prefixWidth);
706
713
  const codeLines = this.#theme.highlightCode
707
- ? this.#theme.highlightCode(token.text, token.lang)
708
- : token.text.split("\n").map((line: string) => this.#theme.codeBlock(line));
714
+ ? this.#theme.highlightCode(view.display, view.language)
715
+ : view.display.split("\n").map(line => this.#theme.codeBlock(line));
709
716
  for (const codeLine of codeLines) {
710
717
  const displayed = replaceTabs(codeLine);
711
- const leadingSpaces = /^ */.exec(Bun.stripANSI(displayed))?.[0].length ?? 0;
712
- const sourceIndentWidth = Math.min(leadingSpaces, Math.max(0, width - indentWidth - 1));
713
- const body = sliceWithWidth(displayed, sourceIndentWidth, visibleWidth(displayed), true).text;
714
- for (const visualLine of wrapTextWithAnsi(body, width - indentWidth - sourceIndentWidth)) {
715
- lines.push(codeIndent + padding(sourceIndentWidth) + visualLine);
718
+ const plain = Bun.stripANSI(displayed);
719
+ const leading = /^ */.exec(plain)?.[0].length ?? 0;
720
+ const sourceIndent = leading < available / 2 ? leading : 0;
721
+ const body = plain.slice(sourceIndent);
722
+ for (const part of wrapCodeLine(body, available - sourceIndent, visibleWidth)) {
723
+ const a = visibleWidth(plain.slice(0, sourceIndent + part.start));
724
+ const b = visibleWidth(plain.slice(0, sourceIndent + part.end));
725
+ const gutter =
726
+ this.#theme.symbols.boxSharp.vertical +
727
+ " " +
728
+ (part.continuation ? this.#theme.symbols.boxSharp.teeRight : " ") +
729
+ " ";
730
+ const prefix = padding(indentWidth) + this.#theme.codeBlockBorder(gutter);
731
+ const visual =
732
+ sliceWithWidth(prefix, 0, prefixWidth, true).text +
733
+ padding(sourceIndent) +
734
+ sliceWithWidth(displayed, a, b - a, true).text;
735
+ lines.push(
736
+ this.#theme.codeBlockBackground
737
+ ? applyBackgroundToLine(visual, width, this.#theme.codeBlockBackground)
738
+ : visual,
739
+ );
716
740
  }
717
741
  }
718
742
  if (nextTokenType && nextTokenType !== "space") {