From 719fecefc15345e34ceccd069ad22ec5fa00de3d Mon Sep 17 00:00:00 2001 From: Scriptim Date: Fri, 26 Jan 2024 11:05:48 +0100 Subject: [PATCH] Make prompt element type name case-insensitive --- src/lib/enum/promptElementType.ts | 15 +++++++++++++-- src/lib/prompt.ts | 6 ++---- src/lib/promptParser.ts | 14 +++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/lib/enum/promptElementType.ts b/src/lib/enum/promptElementType.ts index 1702e06..c1db4d3 100644 --- a/src/lib/enum/promptElementType.ts +++ b/src/lib/enum/promptElementType.ts @@ -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", [], @@ -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. @@ -296,7 +307,7 @@ export const PROMPT_ELEMENT_TYPES_SEPARATORS = [ 'Terminal', 'History Number', 'Prompt Sign', - 'Git branch', + 'Git Branch', '␣', 'Text', ]; diff --git a/src/lib/prompt.ts b/src/lib/prompt.ts index be610ac..07758ea 100644 --- a/src/lib/prompt.ts +++ b/src/lib/prompt.ts @@ -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. @@ -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'], diff --git a/src/lib/promptParser.ts b/src/lib/promptParser.ts index 82b29da..2281d3e 100644 --- a/src/lib/promptParser.ts +++ b/src/lib/promptParser.ts @@ -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'; @@ -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; @@ -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)); } @@ -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)); } @@ -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)); } @@ -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;