Skip to content

Conversation

@nahkampf
Copy link
Contributor

@nahkampf nahkampf commented Oct 15, 2024

Shortcode parser

Just a simple QOL update that allows you to put small shortcodes in a string and parse it into ANSI. It turns something like this:

$writer->eraseDisplay()->cursorPosition(12,22)->underline()->text("Hello")->reset()->cursorDown(2)->color(SGR::COLOR_FG_GREEN)->text("wo")->bold()->text("rl")->normal()->text("d!")->reset()->lf()->lf()->lf();

into:

$str = "%c%%xy12,22%%u%Hello%r%%cd2% %f2%wo%b%rl%n%d!%r%%lf%%lf%%lf%";
$parser->parse($str);

Works with both StreamWriter and BufferWriter. If using Proxy or Buffer you need to flush/echo manually as per usual.

Supported shortcodes

shortcode Equivalent
%b% bold()
%n% normal()
%f% faint()
%i% italic()
%u% underline()
%bl% blink()
%n% negative()
%s% strikethrough()
%r% nostyle()
%f<num>% color(SGR::COLOR_FG_*)
%b<num>% color(SGR::COLOR_BG_*)
%c% eraseDisplay()
%eu% eraseDisplayUp()
%ed% eraseDisplayDown()
%el% eraseLine()
%ee% eraseLineToEOL()
%es% eraseLineToSOL()
%cb<num>% cursorBack(<num>)
%cf<num>% cursorForward(<num>)
%cu<num>% cursorUp(<num>)
%cd<num>% cursorDown(<num>)
%xy<x>,<y>% cursorPosition(<x>, <y>)
%lf% lf()

Note that colors are represented by their MS-DOS equivalents (ie 0 is black, 15 is bright white etc), and they are mapped to their SGR consts in two arrays in the parser.

@bramus
Copy link
Owner

bramus commented Oct 16, 2024

Looks handy. This would need some tests, though.

@nahkampf
Copy link
Contributor Author

Aye, there is that. The Parser could use both belt and bracers for when you get bad or ambiguous input for instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants