Skip to content
Merged
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ If you are using a [NerdFont](https://www.nerdfonts.com/) patched font, you can
useNerdFont = true
```

### Max Suggestions

You can change the maximum number of suggestions displayed in the autocomplete list at one time in your config file:


```toml
maxSuggestions = 10
```


## Unsupported Specs

Specs for the `az`, `gcloud`, & `aws` CLIs are not supported in inshellisense due to their large size.
Expand Down
5 changes: 3 additions & 2 deletions src/ui/suggestionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import log from "../utils/log.js";
import { getConfig } from "../utils/config.js";
import { calculateReplacement, applyReplacement } from "../runtime/replacement.js";

const maxSuggestions = 5;
const getMaxSuggestions = () => getConfig().maxSuggestions ?? 5;
const suggestionWidth = 40;
const descriptionWidth = 30;
const descriptionHeight = 5;
const borderWidth = 2;
const activeSuggestionBackgroundColor = "#7D56F4";
export const MAX_LINES = borderWidth + Math.max(maxSuggestions, descriptionHeight) + 1; // accounts when there is a unhandled newline at the end of the command
export const getMaxLines = () => borderWidth + Math.max(getMaxSuggestions(), descriptionHeight) + 1; // accounts when there is a unhandled newline at the end of the command
export const MIN_WIDTH = borderWidth + descriptionWidth;

export type KeyPressEvent = [string | null | undefined, KeyPress];
Expand Down Expand Up @@ -122,6 +122,7 @@ export class SuggestionManager {
}
const { suggestions, argumentDescription } = this.#suggestBlob;

const maxSuggestions = getMaxSuggestions();
const page = Math.min(Math.floor(this.#activeSuggestionIdx / maxSuggestions) + 1, Math.floor(suggestions.length / maxSuggestions) + 1);
const pagedSuggestions = suggestions.filter((_, idx) => idx < page * maxSuggestions && idx >= (page - 1) * maxSuggestions);
const activePagedSuggestionIndex = this.#activeSuggestionIdx % maxSuggestions;
Expand Down
10 changes: 5 additions & 5 deletions src/ui/ui-root.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Command } from "commander";
import log from "../utils/log.js";
import { getBackspaceSequence, Shell } from "../utils/shell.js";
import { enableWin32InputMode, resetToInitialState } from "../utils/ansi.js";
import { MAX_LINES, type KeyPressEvent, type SuggestionManager } from "./suggestionManager.js";
import { getMaxLines, type KeyPressEvent, type SuggestionManager } from "./suggestionManager.js";
import type { ISTerm } from "../isterm/pty.js";

export const renderConfirmation = (live: boolean): string => {
Expand All @@ -29,7 +29,7 @@ const writeOutput = (data: string) => {
const _render = (term: ISTerm, suggestionManager: SuggestionManager, data: string, handlingBackspace: boolean, handlingSuggestion: boolean): boolean => {
const direction = _direction(term);
const { hidden: cursorHidden, shift: cursorShift } = term.getCursorState();
const linesOfInterest = MAX_LINES;
const linesOfInterest = getMaxLines();

const suggestion = suggestionManager.render(direction);
const hasSuggestion = suggestion.length != 0;
Expand Down Expand Up @@ -59,18 +59,18 @@ const _render = (term: ISTerm, suggestionManager: SuggestionManager, data: strin
const _clear = (term: ISTerm): void => {
const clearDirection = _direction(term) == "above" ? "below" : "above"; // invert direction to clear what was previously rendered
const { hidden: cursorHidden } = term.getCursorState();
const patch = term.getPatch(MAX_LINES, [], clearDirection);
const patch = term.getPatch(getMaxLines(), [], clearDirection);

const ansiCursorShow = cursorHidden ? "" : ansi.cursorShow;
if (clearDirection == "above") {
writeOutput(ansi.cursorHide + ansi.cursorSavePosition + ansi.cursorPrevLine.repeat(MAX_LINES) + patch + ansi.cursorRestorePosition + ansiCursorShow);
writeOutput(ansi.cursorHide + ansi.cursorSavePosition + ansi.cursorPrevLine.repeat(getMaxLines()) + patch + ansi.cursorRestorePosition + ansiCursorShow);
} else {
writeOutput(ansi.cursorHide + ansi.cursorSavePosition + ansi.cursorNextLine + patch + ansi.cursorRestorePosition + ansiCursorShow);
}
};

const _direction = (term: ISTerm): "above" | "below" => {
return term.getCursorState().remainingLines > MAX_LINES ? "below" : "above";
return term.getCursorState().remainingLines > getMaxLines() ? "below" : "above";
};

export const render = async (program: Command, shell: Shell, underTest: boolean, login: boolean) => {
Expand Down
7 changes: 7 additions & 0 deletions src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config = {
};
useAliases: boolean;
useNerdFont: boolean;
maxSuggestions?: number;
};

const bindingSchema: JSONSchemaType<Binding> = {
Expand Down Expand Up @@ -81,6 +82,11 @@ const configSchema = {
nullable: true,
default: false,
},
maxSuggestions: {
type: "number",
nullable: true,
default: 5,
},
},
additionalProperties: false,
};
Expand Down Expand Up @@ -133,6 +139,7 @@ export const loadConfig = async (program: Command) => {
},
useAliases: config.useAliases ?? false,
useNerdFont: config?.useNerdFont ?? false,
maxSuggestions: config?.maxSuggestions ?? 5,
};
}
}
Expand Down
Loading