Skip to content

Commit

Permalink
Merge branch 'fixes'
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Nov 8, 2024
2 parents 882c849 + 9c75169 commit 443765b
Show file tree
Hide file tree
Showing 16 changed files with 1,925 additions and 1,302 deletions.
44 changes: 22 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "@digital-alchemy/terminal",
"repository": "https://github.com/Digital-Alchemy-TS/terminal",
"homepage": "https://docs.digital-alchemy.app",
"version": "24.9.1",
"version": "24.11.1",
"scripts": {
"build": "tsc",
"test": "./scripts/test.sh",
Expand All @@ -29,51 +29,51 @@
"chalk": "^5.3.0",
"chrono-node": "^2.7.7",
"dayjs": "^1.11.13",
"execa": "^9.4.0",
"execa": "^9.5.1",
"external-editor": "^3.1.0",
"figlet": "^1.7.0",
"fuzzysort": "^3.0.2",
"mute-stream": "^1.0.0"
"figlet": "^1.8.0",
"fuzzysort": "^3.1.0",
"mute-stream": "^2.0.0"
},
"peerDependencies": {
"@digital-alchemy/core": "*",
"@digital-alchemy/utils": "*"
},
"devDependencies": {
"@cspell/eslint-plugin": "^8.14.4",
"@digital-alchemy/core": "^24.9.3",
"@cspell/eslint-plugin": "^8.16.0",
"@digital-alchemy/core": "^24.10.8",
"@digital-alchemy/utils": "^24.9.1",
"@eslint/compat": "^1.1.1",
"@eslint/compat": "^1.2.2",
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.11.1",
"@types/figlet": "^1.5.8",
"@types/jest": "^29.5.13",
"@eslint/js": "^9.14.0",
"@types/figlet": "^1.7.0",
"@types/jest": "^29.5.14",
"@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.7.0",
"@types/node": "^22.9.0",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "8.7.0",
"@typescript-eslint/parser": "8.7.0",
"eslint": "9.11.1",
"@typescript-eslint/eslint-plugin": "8.13.0",
"@typescript-eslint/parser": "8.13.0",
"eslint": "9.14.0",
"eslint-config-prettier": "9.1.0",
"eslint-plugin-import": "^2.30.0",
"eslint-plugin-import": "^2.31.0",
"eslint-plugin-jsonc": "^2.16.0",
"eslint-plugin-no-unsanitized": "^4.1.1",
"eslint-plugin-no-unsanitized": "^4.1.2",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-security": "^3.0.1",
"eslint-plugin-simple-import-sort": "^12.1.1",
"eslint-plugin-sonarjs": "^2.0.2",
"eslint-plugin-sonarjs": "^2.0.4",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-unicorn": "^55.0.0",
"globals": "^15.9.0",
"eslint-plugin-unicorn": "^56.0.0",
"globals": "^15.12.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"prettier": "^3.3.3",
"ts-jest": "^29.2.5",
"tsx": "^4.19.1",
"tsx": "^4.19.2",
"type-fest": "^4.26.1",
"typescript": "^5.6.2"
"typescript": "^5.6.3"
},
"packageManager": "[email protected]"
}
2 changes: 1 addition & 1 deletion src/extensions/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export * from "./acknowledge-component.extension";
export * from "./array-builder.extension";
export * from "./confirm-component.extension";
export * from "./menu-component.extension";
export * from "./object-builder-component.extension";
export * from "./object-builder-component.service";
export * from "./pick-many-component.extension";
59 changes: 48 additions & 11 deletions src/extensions/components/menu-component.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type MenuRestoreCacheData<VALUE = unknown> = {
};
const DEFAULT_HEADER_PADDING = 4;

const CACHE_KEY_RESTORE = (id: string) => `MENU_COMPONENT_RESTORE_${id}`;
// const CACHE_KEY_RESTORE = (id: string) => `MENU_COMPONENT_RESTORE_${id}`;

interface LastMenuResultInfo<VALUE = unknown> {
key?: {
Expand Down Expand Up @@ -133,7 +133,7 @@ function isSearchEnabled(options: MenuSearchOptions) {
let LAST_RESULT: LastMenuResultInfo<unknown>;
type LR = "left" | "right";

export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TServiceParams) {
export function Menu<VALUE = unknown>({ config, terminal, internal }: TServiceParams) {
const { ansiPadEnd, template, GV } = terminal.internals;

let value: VALUE;
Expand Down Expand Up @@ -177,6 +177,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Run callbacks from the keyMap
*/
// #MARK: activateKeyMap
async function activateKeyMap(mixed: string, modifiers: KeyModifiers): Promise<void> {
const { keyMap, keyMapCallback: callback } = opt;
const entry = findKeyEntry(keyMap, mixed);
Expand Down Expand Up @@ -232,6 +233,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Move the cursor to the bottom of the list
*/
// #MARK: bottom
function bottom(): void {
const list = side(selectedType);
value = GV(list[list.length - ARRAY_OFFSET].entry);
Expand All @@ -243,6 +245,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
*
* mode: "select"
*/
// #MARK: navigateSearch
function navigateSearch(key: string): void {
// * Grab list of items from current side
const all = side(selectedType);
Expand Down Expand Up @@ -277,6 +280,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Move down 1 entry
*/
// #MARK: next
function next(): void {
setImmediate(() => component.render(false));
const list = side(selectedType);
Expand All @@ -296,6 +300,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* on left key press - attempt to move to left menu
*/
// #MARK: onLeft
function onLeft(): void {
if (is.empty(opt.left) || selectedType === "left") {
return;
Expand All @@ -308,6 +313,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* On right key press - attempt to move editor to right side
*/
// #MARK: onRight
function onRight(): void {
if (is.empty(opt.right) || selectedType === "right") {
return;
Expand All @@ -317,6 +323,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
component.render(false);
}

// #MARK: onSearchFindInputKeyPress
function onSearchFindInputKeyPress(key: string) {
let update = false;
setImmediate(() => component.render(update));
Expand Down Expand Up @@ -391,6 +398,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Key handler for widget while in search mode
*/
// #MARK: onSearchKeyPress
function onSearchKeyPress(key: string): void {
let update = false;
setImmediate(() => component.render(update));
Expand Down Expand Up @@ -464,6 +472,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Attempt to move up 1 item in the active list
*/
// #MARK: previous
function previous(): void {
setImmediate(() => component.render(false));
const list = side(selectedType);
Expand All @@ -483,6 +492,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Simple toggle function
*/
// #MARK: toggleFind
function toggleFind(): void {
mode = mode === "select" ? FIND_INPUT : "select";
if (mode === "select") {
Expand Down Expand Up @@ -515,6 +525,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Move cursor to the top of the current list
*/
// #MARK: top
function top(): void {
const list = side(selectedType);
value = GV(list[FIRST].entry);
Expand All @@ -529,6 +540,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
*
* The goal is to maintain as much functionality as possible as the screen shrinks
*/
// #MARK: assembleMessage
function assembleMessage(construction: MenuConstruction): string {
let height = terminal.environment.getHeight();
let caught = false;
Expand All @@ -555,6 +567,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Auto detect selectedType based on the current value
*/
// #MARK: detectSide
function detectSide(): void {
const isLeftSide = side("left").some(i => GV(i.entry) === value);
selectedType = isLeftSide ? "left" : "right";
Expand All @@ -570,6 +583,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
* { entry: ["combined"] }
* ```
*/
// #MARK: filterMenu
function filterMenu(
data: MainMenuEntry<VALUE>[],
side: LR,
Expand Down Expand Up @@ -642,6 +656,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
return highlighted;
}

// #MARK: transferCursor
function transferCursor() {
const { left, right } = filteredRangedSides();
const leftRange = visualRange(left);
Expand All @@ -657,6 +672,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
.find((item, index) => index >= reversedIndex && item !== BLANK_SPACE) as VALUE;
}

// #MARK: filteredRangedSides
function filteredRangedSides() {
let [right, left] = [side("right"), side("left")];

Expand All @@ -675,6 +691,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
};
}

// #MARK: visualRange
function visualRange(list: MainMenuEntry<VALUE>[]) {
let previous: string | symbol;
return list.flatMap(i => {
Expand All @@ -686,6 +703,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
});
}

// #MARK: findKeyEntry
function findKeyEntry(map: KeyMap<VALUE>, key: string) {
if (map[key]) {
return map[key];
Expand All @@ -703,6 +721,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* The final frame of a menu, informing what happened
*/
// #MARK: renderFinal
function renderFinal() {
const item = selectedEntry();
let message = terminal.text.mergeHelp("", item);
Expand All @@ -721,6 +740,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Rendering for search mode
*/
// #MARK: renderFind
function renderFind(updateValue = false): void {
searchCache.old = searchCache.current;
searchCache.current = {
Expand Down Expand Up @@ -765,6 +785,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
/**
* Rendering for standard keyboard navigation
*/
// #MARK: renderSelect
function renderSelect() {
const construction = {} as MenuConstruction;

Expand Down Expand Up @@ -827,6 +848,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
terminal.screen.render(message);
}

// #MARK: renderSelectKeymap
function renderSelectKeymap() {
const prefix = Object.keys(opt.keyMap).map(key => {
let item = opt.keyMap[key];
Expand Down Expand Up @@ -875,6 +897,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
* Render a menu from a side
*/

// #MARK: renderSide
function renderSide(
side: "left" | "right" = selectedType,
header = opt.showHeaders,
Expand Down Expand Up @@ -948,6 +971,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
return out;
}

// #MARK: renderSideHeader
function renderSideHeader(side: "left" | "right", max: number): string {
const padding = " ".repeat(headerPadding);
if (side === "left") {
Expand All @@ -956,6 +980,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
return `${padding}${rightHeader}`.padEnd(max, " ");
}

// #MARK: renderSideSetup
function renderSideSetup(selected: "left" | "right" = selectedType, updateValue = false) {
const out: MainMenuEntry[] = [];
let menu = side(selected);
Expand Down Expand Up @@ -1018,6 +1043,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
});
}

// #MARK: selectedEntry
function selectedEntry(): MainMenuEntry {
const values = Object.values(opt.keyMap);
return [
Expand All @@ -1027,6 +1053,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
].find(item => GV(item.entry) === value);
}

// #MARK: setKeymap
function setKeymap(): void {
// show if keyOnly, or falsy condensed
const hidden = opt.keyOnly || opt.condensed;
Expand Down Expand Up @@ -1070,6 +1097,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
terminal.keyboard.setKeymap(component, keymap);
}

// #MARK: setValue
// eslint-disable-next-line sonarjs/cognitive-complexity
async function setValue(incoming: VALUE, restore: MenuRestore): Promise<void> {
value = undefined;
Expand All @@ -1089,7 +1117,8 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe

// If a restore id is available, attempt to get data from that
if (!is.empty(restore?.id)) {
const data = await cache.get<MenuRestoreCacheData<VALUE>>(CACHE_KEY_RESTORE(restore.id));
const data = undefined as MenuRestoreCacheData<VALUE>;
// const data = await cache.get<MenuRestoreCacheData<VALUE>>(CACHE_KEY_RESTORE(restore.id));

if (data) {
// Position based value restoration
Expand Down Expand Up @@ -1137,6 +1166,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
* - Items sorted within types, priority first, then ansi stripped label
*/

// #MARK: side
function side(side: "left" | "right"): MainMenuEntry<VALUE>[] {
let temp = opt[side].map(item => [
item,
Expand Down Expand Up @@ -1170,6 +1200,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
return temp.map(([item]) => item as MainMenuEntry<VALUE>);
}

// #MARK: <configure>
const component = terminal.registry.registerComponent("menu", {
async configure(
config: MenuComponentOptions<VALUE>,
Expand Down Expand Up @@ -1204,6 +1235,12 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
opt.right.forEach(i => (i.type ??= ""));
opt.keyMap ??= {};

// rename
if (opt.keyMap.esc) {
opt.keyMap.escape = opt.keyMap.esc;
delete opt.keyMap.esc;
}

done = isDone;

// * Set local properties based on config
Expand Down Expand Up @@ -1249,14 +1286,14 @@ export function Menu<VALUE = unknown>({ config, terminal, internal, cache }: TSe
};
component.render();
done = undefined;
if (opt.restore) {
setImmediate(async () => {
await cache.set<MenuRestoreCacheData<VALUE>>(CACHE_KEY_RESTORE(opt.restore?.id), {
position: [selectedType, index],
value: GV(list[index]) ?? value,
});
});
}
// if (opt.restore) {
// setImmediate(async () => {
// await cache.set<MenuRestoreCacheData<VALUE>>(CACHE_KEY_RESTORE(opt.restore?.id), {
// position: [selectedType, index],
// value: GV(list[index]) ?? value,
// });
// });
// }
},

/**
Expand Down
Loading

0 comments on commit 443765b

Please sign in to comment.