Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
zoe-codez committed Nov 8, 2024
1 parent 4bea3f5 commit 9c75169
Show file tree
Hide file tree
Showing 11 changed files with 289 additions and 195 deletions.
12 changes: 6 additions & 6 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 Down Expand Up @@ -40,7 +40,7 @@
"@digital-alchemy/utils": "*"
},
"devDependencies": {
"@cspell/eslint-plugin": "^8.15.7",
"@cspell/eslint-plugin": "^8.16.0",
"@digital-alchemy/core": "^24.10.8",
"@digital-alchemy/utils": "^24.9.1",
"@eslint/compat": "^1.2.2",
Expand All @@ -51,10 +51,10 @@
"@types/js-yaml": "^4.0.9",
"@types/minimist": "^1.2.5",
"@types/mute-stream": "^0.0.4",
"@types/node": "^22.8.7",
"@types/node": "^22.9.0",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "8.12.2",
"@typescript-eslint/parser": "8.12.2",
"@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.31.0",
Expand All @@ -66,7 +66,7 @@
"eslint-plugin-sonarjs": "^2.0.4",
"eslint-plugin-sort-keys-fix": "^1.1.2",
"eslint-plugin-unicorn": "^56.0.0",
"globals": "^15.11.0",
"globals": "^15.12.0",
"jest": "^29.7.0",
"jest-environment-node": "^29.7.0",
"prettier": "^3.3.3",
Expand Down
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";
36 changes: 36 additions & 0 deletions src/extensions/components/menu-component.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
*
* 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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
*
* 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 }: TServicePa
/**
* 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 }: TServicePa
* { 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 }: TServicePa
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 }: TServicePa
.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 }: TServicePa
};
}

// #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 }: TServicePa
});
}

// #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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
/**
* 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 }: TServicePa
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 }: TServicePa
* 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 }: TServicePa
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 }: TServicePa
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 }: TServicePa
});
}

// #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 }: TServicePa
].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 }: TServicePa
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 Down Expand Up @@ -1138,6 +1166,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal }: TServicePa
* - 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 @@ -1171,6 +1200,7 @@ export function Menu<VALUE = unknown>({ config, terminal, internal }: TServicePa
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 @@ -1205,6 +1235,12 @@ export function Menu<VALUE = unknown>({ config, terminal, internal }: TServicePa
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
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ export function ObjectBuilder<
case "date": {
updated = await terminal.prompt.date({
current: current as string,
dateType: column.dateType,
defaultStyle: column.defaultStyle,
fuzzy: column.fuzzy,
label: column.name,
});
break;
Expand Down
Loading

0 comments on commit 9c75169

Please sign in to comment.