Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions src/tui/commands/start-screen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import type { CliFlags, DesiredTraits } from '@/types.js';
import type { Preset } from '@/presets.js';
import { getProfiles } from '@/config/index.js';
import { getProfiles, getCompanionName, renameCompanion } from '@/config/index.js';
import { selectStartAction, selectPreset } from '../prompts.ts';
import { banner } from '../display.ts';
import { runBuddies } from './buddies.ts';
Expand Down Expand Up @@ -32,7 +32,7 @@ function presetToTraits(preset: Preset): DesiredTraits {
};
}

type StartAction = 'build' | 'presets' | 'buddies';
type StartAction = 'build' | 'presets' | 'buddies' | 'rename';

export async function runStartScreen(flags: CliFlags = {}): Promise<void> {
if (hasTraitFlags(flags)) {
Expand Down Expand Up @@ -76,7 +76,7 @@ export async function runStartScreen(flags: CliFlags = {}): Promise<void> {
);
}
}
action = await selectStartAction(buddyCount);
action = await selectStartAction(buddyCount, { companionName: getCompanionName() });
}

switch (action) {
Expand Down Expand Up @@ -109,5 +109,38 @@ export async function runStartScreen(flags: CliFlags = {}): Promise<void> {

case 'buddies':
return runBuddies();

case 'rename': {
let handled = false;
try {
const { canUseBuilder } = await import('../builder/index.ts');
if (await canUseBuilder()) {
const { runRenameTUI } = await import('../rename/index.ts');
await runRenameTUI();
handled = true;
}
} catch {
// OpenTUI unavailable — fall through to inquirer
}

if (!handled) {
const { input } = await import('@inquirer/prompts');
const currentName = getCompanionName();
const newName = (
await input({
message: `New name for your buddy (current: "${currentName}")`,
})
).trim();
if (newName && newName !== currentName) {
try {
renameCompanion(newName);
console.log(chalk.green(` Renamed "${currentName}" → "${newName}"`));
} catch (err) {
console.log(chalk.yellow(` Could not rename: ${(err as Error).message}`));
}
}
}
return runStartScreen(flags);
}
}
}
20 changes: 16 additions & 4 deletions src/tui/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,22 @@ export async function selectMode(): Promise<'preset' | 'custom'> {
});
}

export type StartAction = 'build' | 'presets' | 'buddies';
export type StartAction = 'build' | 'presets' | 'buddies' | 'rename';

export async function selectStartAction(buddyCount: number): Promise<StartAction> {
const choices: { name: string; value: StartAction }[] = [
export async function selectStartAction(
buddyCount: number,
{ companionName }: { companionName?: string | null } = {},
): Promise<StartAction> {
const choices: { name: string; value: StartAction }[] = [];

if (companionName) {
choices.push({
name: `Rename buddy ${chalk.dim(`— current: "${companionName}"`)}`,
value: 'rename',
});
}

choices.push(
{
name: `Build your own ${chalk.dim('— customize species, rarity, eyes, hat')}`,
value: 'build',
Expand All @@ -103,7 +115,7 @@ export async function selectStartAction(buddyCount: number): Promise<StartAction
name: `Browse presets ${chalk.dim(`— pick from ${PRESETS.length} curated builds`)}`,
value: 'presets',
},
];
);

if (buddyCount > 0) {
choices.push({
Expand Down
153 changes: 153 additions & 0 deletions src/tui/rename/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import { ISSUE_URL } from '@/constants.js';
import { BORDER_COLOR, DIM_COLOR } from '@/tui/builder/colors.js';
import { getCompanionName, renameCompanion } from '@/config/index.js';

/**
* Full-screen rename TUI. Returns true if a rename was performed, false on cancel.
*/
export async function runRenameTUI(): Promise<boolean> {
const otui = await import('@opentui/core');
const { createCliRenderer, Box, Text, Input, InputRenderableEvents } = otui;
type TextRenderableType = InstanceType<typeof otui.TextRenderable>;
type InputRenderableType = InstanceType<typeof otui.InputRenderable>;

let renderer: Awaited<ReturnType<typeof createCliRenderer>> | null = null;

try {
renderer = await createCliRenderer({
exitOnCtrlC: false,
screenMode: 'alternate-screen',
});

const r = renderer;

return await new Promise<boolean>((resolve) => {
let resolved = false;

const currentName = getCompanionName() ?? '';

const handleCtrlC = (key: { ctrl?: boolean; name?: string }) => {
if (key.ctrl && key.name === 'c') finish(false);
};

function finish(result: boolean): void {
if (resolved) return;
resolved = true;
r.keyInput.removeListener('keypress', handleCtrlC);
r.keyInput.removeListener('keypress', handleEscape);
r.destroy();
renderer = null;
resolve(result);
}

function handleEscape(key: { name?: string }): void {
if (key.name === 'escape') finish(false);
}

const rootBox = Box(
{
id: 'root',
flexDirection: 'column',
width: '100%',
height: '100%',
borderStyle: 'rounded',
border: true,
borderColor: BORDER_COLOR,
title: ' Rename buddy ',
titleAlignment: 'center',
padding: 0,
justifyContent: 'center',
alignItems: 'center',
},

Text({
id: 'label',
content: ` Current name: "${currentName}"`,
fg: DIM_COLOR,
height: 1,
}),

Text({ content: '', height: 1 }),

Text({
content: ' New name:',
fg: '#ffffff',
height: 1,
}),

Text({ content: '', height: 1 }),

Input({
id: 'rename-input',
placeholder: currentName,
width: 40,
focusedTextColor: '#ffffff',
placeholderColor: '#555555',
}),

Text({ content: '', height: 2 }),

Text({
id: 'status',
content: '',
height: 1,
}),

Text({ content: '', height: 1 }),

Text({
content: 'Enter confirm Esc back',
fg: DIM_COLOR,
height: 1,
}),
);

r.root.add(rootBox);

const input = r.root.findDescendantById('rename-input') as InputRenderableType | null;
const status = r.root.findDescendantById('status') as TextRenderableType | null;

if (input) {
input.focus();
input.on(InputRenderableEvents.ENTER, () => {
const newName = (input.value ?? '').trim();
if (!newName) {
if (status) {
status.content = ' Name cannot be empty';
status.fg = '#ff5555';
}
return;
}
if (newName === currentName) {
finish(false);
return;
}
try {
renameCompanion(newName);
finish(true);
} catch (err) {
if (status) {
status.content = ` Error: ${(err as Error).message}`;
status.fg = '#ff5555';
}
}
});
}

r.keyInput.on('keypress', handleCtrlC);
r.keyInput.on('keypress', handleEscape);
r.auto();
});
} catch (err) {
if (renderer) {
try {
renderer.destroy();
} catch {
/* ignore */
}
}
console.error(` Rename error: ${(err as Error).message}`);
console.error(` If this persists, please report at: ${ISSUE_URL}`);
return false;
}
}
20 changes: 17 additions & 3 deletions src/tui/start/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ISSUE_URL } from '@/constants.js';
import { BORDER_COLOR, DIM_COLOR, FOCUS_BORDER } from '@/tui/builder/colors.js';
import { getCompanionName } from '@/config/index.js';

export type StartAction = 'build' | 'presets' | 'buddies';
export type StartAction = 'build' | 'presets' | 'buddies' | 'rename';

interface MenuEntry {
value: StartAction;
Expand Down Expand Up @@ -38,7 +39,20 @@ export async function runStartTUI(buddyCount: number): Promise<StartAction | nul
let resolved = false;
let selected = 0;

const entries: MenuEntry[] = [
const currentName = getCompanionName();

const entries: MenuEntry[] = [];

if (currentName !== null) {
entries.push({
value: 'rename',
icon: '✏',
title: 'Rename buddy',
desc: `Current: "${currentName}"`,
});
}

entries.push(
{
value: 'build',
icon: '⚡',
Expand All @@ -51,7 +65,7 @@ export async function runStartTUI(buddyCount: number): Promise<StartAction | nul
title: 'Browse presets',
desc: 'Pick from curated themed builds with live preview',
},
];
);

if (buddyCount > 0) {
entries.push({
Expand Down
Loading