@tooee/layout 0.11.0 → 0.13.0

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.
Files changed (48) hide show
  1. package/README.md +46 -0
  2. package/dist/app-layout.d.ts +9 -2
  3. package/dist/app-layout.d.ts.map +1 -1
  4. package/dist/app-layout.js +2 -4
  5. package/dist/app-layout.js.map +1 -1
  6. package/dist/app-layout.public-contract.typecheck.d.ts.map +1 -1
  7. package/dist/cell-text.d.ts +11 -0
  8. package/dist/cell-text.d.ts.map +1 -0
  9. package/dist/cell-text.js +55 -0
  10. package/dist/cell-text.js.map +1 -0
  11. package/dist/close-button.d.ts.map +1 -1
  12. package/dist/close-button.js.map +1 -1
  13. package/dist/fit-status-bar.d.ts +78 -0
  14. package/dist/fit-status-bar.d.ts.map +1 -0
  15. package/dist/fit-status-bar.js +182 -0
  16. package/dist/fit-status-bar.js.map +1 -0
  17. package/dist/fit-title-bar.d.ts +13 -0
  18. package/dist/fit-title-bar.d.ts.map +1 -0
  19. package/dist/fit-title-bar.js +20 -0
  20. package/dist/fit-title-bar.js.map +1 -0
  21. package/dist/index.d.ts +7 -1
  22. package/dist/index.d.ts.map +1 -1
  23. package/dist/index.js +3 -0
  24. package/dist/index.js.map +1 -1
  25. package/dist/overlay-panel.d.ts.map +1 -1
  26. package/dist/overlay-panel.js.map +1 -1
  27. package/dist/react-content.d.ts.map +1 -1
  28. package/dist/react-content.js.map +1 -1
  29. package/dist/status-bar.d.ts +13 -8
  30. package/dist/status-bar.d.ts.map +1 -1
  31. package/dist/status-bar.js +30 -5
  32. package/dist/status-bar.js.map +1 -1
  33. package/dist/title-bar.d.ts +7 -1
  34. package/dist/title-bar.d.ts.map +1 -1
  35. package/dist/title-bar.js +17 -4
  36. package/dist/title-bar.js.map +1 -1
  37. package/package.json +6 -6
  38. package/src/app-layout.public-contract.typecheck.ts +6 -0
  39. package/src/app-layout.tsx +17 -6
  40. package/src/cell-text.ts +79 -0
  41. package/src/close-button.tsx +1 -0
  42. package/src/fit-status-bar.ts +307 -0
  43. package/src/fit-title-bar.ts +36 -0
  44. package/src/index.ts +34 -1
  45. package/src/overlay-panel.tsx +3 -0
  46. package/src/react-content.ts +2 -0
  47. package/src/status-bar.tsx +72 -15
  48. package/src/title-bar.tsx +28 -6
@@ -0,0 +1,36 @@
1
+ import { cellWidth, truncateCells } from "./cell-text.js";
2
+
3
+ export interface TitleBarText {
4
+ title: string;
5
+ /** Empty when the subtitle does not show. */
6
+ subtitle: string;
7
+ }
8
+
9
+ /** Text between the title and the subtitle. */
10
+ export const TITLE_BAR_SEPARATOR = " — ";
11
+
12
+ const MIN_SUBTITLE_WIDTH = 8;
13
+
14
+ /**
15
+ * Fit a title and subtitle into `width` cells. The subtitle shortens first,
16
+ * down to 8 cells, then hides. Then the title shortens with an end ellipsis.
17
+ */
18
+ export const fitTitleBar = function fitTitleBar(
19
+ title: string,
20
+ subtitle: string | undefined,
21
+ width: number
22
+ ): TitleBarText {
23
+ const sub = subtitle ?? "";
24
+
25
+ if (sub === "") {
26
+ return { subtitle: "", title: truncateCells(title, width) };
27
+ }
28
+
29
+ const room = width - cellWidth(title) - cellWidth(TITLE_BAR_SEPARATOR);
30
+
31
+ if (room >= Math.min(cellWidth(sub), MIN_SUBTITLE_WIDTH)) {
32
+ return { subtitle: truncateCells(sub, room), title };
33
+ }
34
+
35
+ return { subtitle: "", title: truncateCells(title, width) };
36
+ };
package/src/index.ts CHANGED
@@ -1,10 +1,43 @@
1
1
  export { AppLayout } from "./app-layout.js";
2
+
2
3
  export type { AppLayoutProps, AppLayoutScroll } from "./app-layout.js";
4
+
3
5
  export { CloseButton } from "./close-button.js";
6
+
4
7
  export { OverlayPanel } from "./overlay-panel.js";
8
+
5
9
  export type { OverlayPanelProps, PanelInset, PanelInsetValue } from "./overlay-panel.js";
10
+
6
11
  export { decodeReactContent } from "./react-content.js";
12
+
7
13
  export type { DecodedReactContent } from "./react-content.js";
14
+
15
+ export { truncateCells } from "./cell-text.js";
16
+
17
+ export type { TruncateMode } from "./cell-text.js";
18
+
19
+ export {
20
+ fitStatusBar,
21
+ STATUS_BAR_GAP,
22
+ STATUS_BAR_HIDDEN_MARKER,
23
+ StatusPriority,
24
+ } from "./fit-status-bar.js";
25
+
26
+ export type {
27
+ FitStatusBarOptions,
28
+ PlacedStatusItem,
29
+ StatusBarAlign,
30
+ StatusBarItem,
31
+ StatusBarLayout,
32
+ StatusBarTruncate,
33
+ } from "./fit-status-bar.js";
34
+
35
+ export { fitTitleBar } from "./fit-title-bar.js";
36
+
37
+ export type { TitleBarText } from "./fit-title-bar.js";
38
+
8
39
  export { StatusBar } from "./status-bar.js";
9
- export type { StatusBarItem } from "./status-bar.js";
40
+
41
+ export type { StatusBarProps } from "./status-bar.js";
42
+
10
43
  export { TitleBar } from "./title-bar.js";
@@ -42,6 +42,7 @@ const renderThemedContent = function renderThemedContent(
42
42
  if (content.kind === "empty") {
43
43
  return null;
44
44
  }
45
+
45
46
  return content.kind === "string" ? <text content={content.value} fg={color} /> : content.value;
46
47
  };
47
48
 
@@ -52,9 +53,11 @@ const renderPrompt = function renderPrompt(
52
53
  if (prompt.kind === "empty") {
53
54
  return null;
54
55
  }
56
+
55
57
  if (prompt.kind === "node") {
56
58
  return prompt.value;
57
59
  }
60
+
58
61
  return (
59
62
  <box paddingLeft={1} paddingRight={1}>
60
63
  <text fg={textColor}>
@@ -23,9 +23,11 @@ export const decodeReactContent = function decodeReactContent(
23
23
  if (content === null || content === undefined) {
24
24
  return { kind: "empty" };
25
25
  }
26
+
26
27
  // oxlint-disable-next-line anti-slop/no-runtime-typeof -- ReactNode boundary distinguishes themed primitive text from nodes rendered by React
27
28
  if (typeof content === "string") {
28
29
  return { kind: "string", value: content };
29
30
  }
31
+
30
32
  return { kind: "node", value: content };
31
33
  };
@@ -1,35 +1,92 @@
1
+ import { useTerminalDimensions } from "@opentui/react";
1
2
  import { useTheme } from "@tooee/themes";
2
3
  import type { ReactNode } from "react";
3
4
 
4
- interface StatusBarProps {
5
- items: StatusBarItem[];
5
+ import { fitStatusBar, STATUS_BAR_GAP } from "./fit-status-bar.js";
6
+ import type { PlacedStatusItem, StatusBarItem } from "./fit-status-bar.js";
7
+
8
+ export type { StatusBarItem } from "./fit-status-bar.js";
9
+
10
+ const PADDING_X = 1;
11
+
12
+ export interface StatusBarProps {
13
+ items: readonly StatusBarItem[];
14
+ /** Bar width in cells. Defaults to the terminal width. */
15
+ width?: number;
16
+ /** Glyph that shows when items hide. `false` turns it off. Default `"⋯"`. */
17
+ hiddenMarker?: string | false;
6
18
  }
7
19
 
8
- export interface StatusBarItem {
9
- label: string;
10
- value?: string;
20
+ interface StatusTextProps {
21
+ item: PlacedStatusItem;
22
+ first: boolean;
11
23
  }
12
24
 
13
- export const StatusBar = function StatusBar({ items }: StatusBarProps): ReactNode {
25
+ const StatusText = function StatusText({ item, first }: StatusTextProps): ReactNode {
14
26
  const { theme } = useTheme();
27
+ const gap = item.label !== "" && item.value !== "" ? " " : "";
28
+
29
+ return (
30
+ <text wrapMode="none" style={{ flexShrink: 0, marginLeft: first ? 0 : STATUS_BAR_GAP }}>
31
+ <span fg={theme.textMuted}>{item.label}</span>
32
+ {gap}
33
+ <span fg={theme.text}>{item.value}</span>
34
+ </text>
35
+ );
36
+ };
37
+
38
+ /**
39
+ * One-row status bar. Items that do not fit use their compact form, shorten,
40
+ * or hide, lowest priority first. The bar never wraps.
41
+ */
42
+ export const StatusBar = function StatusBar({
43
+ items,
44
+ width,
45
+ hiddenMarker,
46
+ }: StatusBarProps): ReactNode {
47
+ const { theme } = useTheme();
48
+ const terminal = useTerminalDimensions();
49
+ const inner = Math.max(0, (width ?? terminal.width) - PADDING_X * 2);
50
+ const layout = fitStatusBar(items, inner, { hiddenMarker });
51
+
15
52
  return (
16
53
  <box
17
54
  style={{
18
55
  backgroundColor: theme.backgroundPanel,
19
56
  flexDirection: "row",
20
57
  flexShrink: 0,
58
+ height: 1,
59
+ overflow: "hidden",
21
60
  padding: 0,
22
- paddingLeft: 1,
23
- paddingRight: 1,
61
+ paddingLeft: PADDING_X,
62
+ paddingRight: PADDING_X,
63
+ width,
24
64
  }}
25
65
  >
26
- {items.map((item, index): ReactNode => (
27
- <box key={index} style={{ flexDirection: "row", marginRight: 2 }}>
28
- <text content={item.label} style={{ fg: theme.textMuted }} />
29
- {(item.value?.length ?? 0) > 0 && (
30
- <text content={` ${item.value}`} style={{ fg: theme.text }} />
31
- )}
32
- </box>
66
+ {layout.start.map((item, index): ReactNode => (
67
+ <StatusText key={item.key} item={item} first={index === 0} />
68
+ ))}
69
+ {layout.marker !== null && (
70
+ <text
71
+ wrapMode="none"
72
+ content={layout.marker}
73
+ style={{
74
+ fg: theme.textMuted,
75
+ flexShrink: 0,
76
+ marginLeft: layout.start.length > 0 ? STATUS_BAR_GAP : 0,
77
+ }}
78
+ />
79
+ )}
80
+ {layout.end.length > 0 && (
81
+ <box
82
+ style={{
83
+ flexGrow: 1,
84
+ minWidth: layout.start.length > 0 || layout.marker !== null ? STATUS_BAR_GAP : 0,
85
+ }}
86
+ />
87
+ )}
88
+ {layout.end.map((item, index): ReactNode => (
89
+ <StatusText key={item.key} item={item} first={index === 0} />
33
90
  ))}
34
91
  </box>
35
92
  );
package/src/title-bar.tsx CHANGED
@@ -1,27 +1,49 @@
1
+ import { useTerminalDimensions } from "@opentui/react";
1
2
  import { useTheme } from "@tooee/themes";
2
3
  import type { ReactNode } from "react";
3
4
 
5
+ import { fitTitleBar, TITLE_BAR_SEPARATOR } from "./fit-title-bar.js";
6
+
7
+ const PADDING_X = 1;
8
+
4
9
  interface TitleBarProps {
5
10
  title: string;
6
11
  subtitle?: string;
12
+ /** Bar width in cells. Defaults to the terminal width. */
13
+ width?: number;
7
14
  }
8
15
 
9
- export const TitleBar = function TitleBar({ title, subtitle }: TitleBarProps): ReactNode {
16
+ /**
17
+ * One-row title bar. When the bar is narrow, the subtitle shortens, then
18
+ * hides, then the title shortens. The bar never wraps.
19
+ */
20
+ export const TitleBar = function TitleBar({ title, subtitle, width }: TitleBarProps): ReactNode {
10
21
  const { theme } = useTheme();
22
+ const terminal = useTerminalDimensions();
23
+ const inner = Math.max(0, (width ?? terminal.width) - PADDING_X * 2);
24
+ const fitted = fitTitleBar(title, subtitle, inner);
25
+
11
26
  return (
12
27
  <box
13
28
  style={{
14
29
  backgroundColor: theme.backgroundPanel,
15
30
  flexDirection: "row",
16
31
  flexShrink: 0,
32
+ height: 1,
33
+ overflow: "hidden",
17
34
  padding: 0,
18
- paddingLeft: 1,
19
- paddingRight: 1,
35
+ paddingLeft: PADDING_X,
36
+ paddingRight: PADDING_X,
37
+ width,
20
38
  }}
21
39
  >
22
- <text content={title} style={{ fg: theme.primary }} />
23
- {(subtitle?.length ?? 0) > 0 && (
24
- <text content={` — ${subtitle}`} style={{ fg: theme.textMuted }} />
40
+ <text wrapMode="none" content={fitted.title} style={{ fg: theme.primary, flexShrink: 0 }} />
41
+ {fitted.subtitle !== "" && (
42
+ <text
43
+ wrapMode="none"
44
+ content={`${TITLE_BAR_SEPARATOR}${fitted.subtitle}`}
45
+ style={{ fg: theme.textMuted, flexShrink: 0 }}
46
+ />
25
47
  )}
26
48
  </box>
27
49
  );