forked from aws/agentcore-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLayoutContext.tsx
More file actions
52 lines (42 loc) · 1.74 KB
/
Copy pathLayoutContext.tsx
File metadata and controls
52 lines (42 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { useStdout } from 'ink';
import React, { type ReactNode, createContext, useContext } from 'react';
/** Maximum content width cap */
const MAX_CONTENT_WIDTH = 60;
interface LayoutContextValue {
/** Global content width: min(terminalWidth, MAX_CONTENT_WIDTH) */
contentWidth: number;
}
const LayoutContext = createContext<LayoutContextValue>({
contentWidth: MAX_CONTENT_WIDTH,
});
// eslint-disable-next-line react-refresh/only-export-components
export function useLayout(): LayoutContextValue {
return useContext(LayoutContext);
}
/**
* Build the logo dynamically based on width.
* The logo has fixed text " >_ AgentCore" on left and version on right,
* with padding in between to fill the width.
*/
// eslint-disable-next-line react-refresh/only-export-components
export function buildLogo(width: number, version?: string): string {
const left = '│ >_ AgentCore';
const right = version ? `v${version} │` : '│';
// -2 for the border chars already in left/right
const innerWidth = width - 2;
const paddingNeeded = innerWidth - (left.length - 1) - (right.length - 1);
const padding = ' '.repeat(Math.max(0, paddingNeeded));
const topBorder = '┌' + '─'.repeat(innerWidth) + '┐';
const bottomBorder = '└' + '─'.repeat(innerWidth) + '┘';
const middle = left + padding + right;
return `\n${topBorder}\n${middle}\n${bottomBorder}`;
}
interface LayoutProviderProps {
children: ReactNode;
}
export function LayoutProvider({ children }: LayoutProviderProps) {
const { stdout } = useStdout();
const terminalWidth = stdout?.columns ?? MAX_CONTENT_WIDTH;
const contentWidth = Math.min(terminalWidth, MAX_CONTENT_WIDTH);
return <LayoutContext.Provider value={{ contentWidth }}>{children}</LayoutContext.Provider>;
}