Skip to content

Commit

Permalink
Make prompt element type name case-insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Scriptim committed Jan 26, 2024
1 parent f50fd1a commit 719fece
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
15 changes: 13 additions & 2 deletions src/lib/enum/promptElementType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const PROMPT_ELEMENT_TYPES = [
new PromptElementType('Prompt Sign', '\\$', [], true, false, 'If the effective uid is 0, #, otherwise $.', '$'),
new PromptElementType('Exit Status', '$?', [], true, false, 'Exit status ($?).', '0'),
new PromptElementType(
'Git branch',
'Git Branch',
// eslint-disable-next-line quotes
"git branch 2>/dev/null | grep '*' | colrm 1 2",
[],
Expand Down Expand Up @@ -284,6 +284,17 @@ export const PROMPT_ELEMENT_TYPES = [
),
];

/**
* Find a prompt element type by its name. This function should only be used if the name is known to be valid.
*
* @param name The case-insensitive name of an existing (!) prompt element type.
* @returns the prompt element type with the given name
*/
export function getPromptElementTypeByNameUnsafe(name: string): PromptElementType {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return PROMPT_ELEMENT_TYPES.find((promptElementType) => promptElementType.name.toLowerCase() === name.toLowerCase())!;
}

/**
* {@linkcode PROMPT_ELEMENT_TYPES_SEPARATORS} is a list labels of {@linkcode PromptElementType}s in
* {@linkcode PROMPT_ELEMENT_TYPES} *before* which a separator should be inserted in the UI.
Expand All @@ -296,7 +307,7 @@ export const PROMPT_ELEMENT_TYPES_SEPARATORS = [
'Terminal',
'History Number',
'Prompt Sign',
'Git branch',
'Git Branch',
'␣',
'Text',
];
6 changes: 2 additions & 4 deletions src/lib/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { defineStore, storeToRefs } from 'pinia';
import { PromptElement, UniquePromptElement } from './promptElement';
import { PromptElementType, PROMPT_ELEMENT_TYPES } from './enum/promptElementType';
import { PromptElementType, getPromptElementTypeByNameUnsafe } from './enum/promptElementType';

/**
* The prompt store holding the global state of the current prompt.
Expand All @@ -17,9 +17,7 @@ const prompt = defineStore({
serialize: (state) =>
JSON.stringify(state, (key, value) => (key === 'type' ? (value as PromptElementType).name : value)),
deserialize: (state) =>
JSON.parse(state, (key, value) =>
key === 'type' ? PROMPT_ELEMENT_TYPES.find((type) => type.name === value) : value,
),
JSON.parse(state, (key, value) => (key === 'type' ? getPromptElementTypeByNameUnsafe(value) : value)),
},
// we don't want to persist the selected element because it is only used for the UI
paths: ['elementsIdCounter', 'elements'],
Expand Down
14 changes: 7 additions & 7 deletions src/lib/promptParser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PROMPT_ELEMENT_TYPES } from './enum/promptElementType';
import { PROMPT_ELEMENT_TYPES, getPromptElementTypeByNameUnsafe } from './enum/promptElementType';
import { ANSI } from './enum/ansi';
import { PromptElement } from './promptElement';
import { PropertiesState, defaultPropertiesState } from './promptElementProperties';
Expand Down Expand Up @@ -322,14 +322,14 @@ function applyPromptCommand(ps1: PromptElement[], promptCommand: string): Prompt
// we cannot use the above check for predefined commands because the command string is not constant

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
newElement = new PromptElement(PROMPT_ELEMENT_TYPES.find((e) => e.name === 'Advanced Git Prompt')!);
newElement = new PromptElement(getPromptElementTypeByNameUnsafe('Advanced Git Prompt'));
const formatString = command.match(/__git_ps1\s+"(.*)"/)?.[1];
if (formatString !== undefined) {
newElement.parameters.format = formatString;
}
} else {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
newElement = new PromptElement(PROMPT_ELEMENT_TYPES.find((e) => e.name === 'Command')!);
newElement = new PromptElement(getPromptElementTypeByNameUnsafe('Command'));
newElement.parameters.command = command;
}
newElement.foregroundColor = element.foregroundColor;
Expand Down Expand Up @@ -386,7 +386,7 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
cursor += 1;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const element = new PromptElement(PROMPT_ELEMENT_TYPES.find((e) => e.name === 'Date (formatted)')!);
const element = new PromptElement(getPromptElementTypeByNameUnsafe('Date (formatted)'));
element.parameters.dateformat = dateformat;
elements.push(applyState(element, propertiesState));
}
Expand Down Expand Up @@ -414,7 +414,7 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
cursor += 1;

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const element = new PromptElement(PROMPT_ELEMENT_TYPES.find((e) => e.name === 'Command')!);
const element = new PromptElement(getPromptElementTypeByNameUnsafe('Command'));
element.parameters.command = command;
elements.push(applyState(element, propertiesState));
}
Expand Down Expand Up @@ -449,7 +449,7 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const element = new PromptElement(PROMPT_ELEMENT_TYPES.find((e) => e.name === 'Environment Variable')!);
const element = new PromptElement(getPromptElementTypeByNameUnsafe('Environment Variable'));
element.parameters.variable = variableName;
elements.push(applyState(element, propertiesState));
}
Expand All @@ -458,7 +458,7 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
// we create text elements with single characters only because the next char might be a special character
// consecutive text elements will be merged later
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const element = new PromptElement(PROMPT_ELEMENT_TYPES.find((e) => e.name === 'Text')!);
const element = new PromptElement(getPromptElementTypeByNameUnsafe('Text'));
element.parameters.text = ps1[cursor];
elements.push(applyState(element, propertiesState));
cursor += 1;
Expand Down

0 comments on commit 719fece

Please sign in to comment.