Skip to content

Commit e9adf2a

Browse files
committed
Support Nerd Font glyps in prompt parser
1 parent ffdcc0c commit e9adf2a

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/lib/enum/nerdfontglyph.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43084,6 +43084,27 @@ export function findNerdFontGlyphByName(name: string): NerdFontGlyph | undefined
4308443084
return NERD_FONT_GLYPHS.find((glyph) => glyph.name.toLowerCase() === name.toLowerCase());
4308543085
}
4308643086

43087+
const codeRange = NERD_FONT_GLYPHS.reduce(
43088+
(range, glyph) => ({
43089+
min: Math.min(range.min, glyph.code),
43090+
max: Math.max(range.max, glyph.code),
43091+
}),
43092+
{ min: Infinity, max: -Infinity },
43093+
);
43094+
43095+
/**
43096+
* Find a Nerd Font glyph by its code point.
43097+
*
43098+
* @param code The code point of the glyph.
43099+
* @returns The glyph with the given code point or `undefined` if no such glyph exists.
43100+
*/
43101+
export function findNerdFontGlyphByCode(code: number): NerdFontGlyph | undefined {
43102+
if (code < codeRange.min || code > codeRange.max) {
43103+
return undefined;
43104+
}
43105+
return NERD_FONT_GLYPHS.find((glyph) => glyph.code === code);
43106+
}
43107+
4308743108
const fuse = new Fuse(NERD_FONT_GLYPHS, {
4308843109
keys: ['name'],
4308943110
});

src/lib/promptParser.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ANSI } from './enum/ansi';
33
import { PromptElement } from './promptElement';
44
import { PropertiesState, defaultPropertiesState } from './promptElementProperties';
55
import { COLORS } from './enum/color';
6+
import { findNerdFontGlyphByCode } from './enum/nerdfontglyph';
67

78
/**
89
* Thrown when the prompt parser encounters an error.
@@ -370,6 +371,9 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
370371
let operatingSystemCommand: boolean = false;
371372

372373
while (cursor < ps1.length) {
374+
// we need to segment graphemes to handle Nerd Font glyphs consisting of multiple code units correctly
375+
const grapheme = new Intl.Segmenter().segment(ps1.slice(cursor))[Symbol.iterator]().next().value?.segment ?? '';
376+
373377
// unparameterized elements are the most common, so we check them first
374378
const unparameterizedElement = readUnparameterized(ps1, cursor);
375379
if (unparameterizedElement !== null) {
@@ -507,6 +511,14 @@ export function parsePrompt(ps1: string, promptCommand: string): PromptElement[]
507511

508512
elements.push(applyState(element, propertiesState));
509513
}
514+
// manual handling of Nerd Font glyphs
515+
else if (findNerdFontGlyphByCode(grapheme.codePointAt(0) ?? 0) !== undefined) {
516+
const glyph = findNerdFontGlyphByCode(grapheme.codePointAt(0) ?? 0)!;
517+
const element = new PromptElement(getPromptElementTypeByNameUnsafe('Nerd Font Glyph'));
518+
element.parameters.glyph = glyph.name;
519+
elements.push(applyState(element, propertiesState));
520+
cursor += grapheme.length;
521+
}
510522
// manual handling of custom text element (fallback)
511523
else {
512524
// we create text elements with single characters only because the next char might be a special character

0 commit comments

Comments
 (0)