Skip to content

Commit d4e43a2

Browse files
committed
Added support for 256-colour ANSI sequences
1 parent 6f3e571 commit d4e43a2

File tree

1 file changed

+164
-100
lines changed

1 file changed

+164
-100
lines changed

scripting/methods/methods_noting.cpp

Lines changed: 164 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,10 @@ bool bItalic = false;
279279
bool bUnderline = false;
280280
int iCurrentForeGround = WHITE;
281281
int iCurrentBackGround = BLACK;
282+
bool gotANSI_TEXT_256_COLOUR = false;
283+
bool gotANSI_BACK_256_COLOUR = false;
284+
bool nextANSI_TEXT_256_COLOUR = false;
285+
bool nextANSI_BACK_256_COLOUR = false;
282286

283287
m_iNoteStyle = NORMAL; // start off with normal style
284288

@@ -314,111 +318,171 @@ long length;
314318
else
315319
if (c == ';' || c == 'm')
316320
{
317-
switch (iCode)
321+
322+
// here if we have had ESC [ 38;5;n or ESC [ 48;5;n
323+
// n is the colour in the range 0 to 255
324+
if (nextANSI_TEXT_256_COLOUR || nextANSI_BACK_256_COLOUR)
318325
{
319-
// reset colours to defaults
320-
case ANSI_RESET:
321-
iCurrentForeGround = WHITE;
322-
iCurrentBackGround = BLACK;
323-
bBold = false;
324-
bInverse = false;
325-
bItalic = false;
326-
bUnderline = false;
327-
break;
328-
329-
// bold
330-
case ANSI_BOLD:
331-
bBold = true;
332-
break;
333-
334-
// inverse
335-
case ANSI_INVERSE:
336-
bInverse = true;
337-
break;
338-
339-
// blink
340-
case ANSI_BLINK:
341-
case ANSI_SLOW_BLINK:
342-
case ANSI_FAST_BLINK:
343-
bItalic = true;
344-
break;
345-
346-
// underline
347-
case ANSI_UNDERLINE:
348-
bUnderline = true;
349-
break;
350-
351-
// not bold
352-
case ANSI_CANCEL_BOLD:
353-
bBold = false;
354-
break;
355-
356-
// not inverse
357-
case ANSI_CANCEL_INVERSE:
358-
bInverse = false;
359-
break;
360-
361-
// not blink
362-
case ANSI_CANCEL_BLINK:
363-
case ANSI_CANCEL_SLOW_BLINK:
364-
bItalic = false;
365-
break;
366-
367-
// not underline
368-
case ANSI_CANCEL_UNDERLINE:
369-
bUnderline = false;
370-
break;
371-
372-
// different foreground colour
373-
case ANSI_TEXT_BLACK:
374-
case ANSI_TEXT_RED :
375-
case ANSI_TEXT_GREEN :
376-
case ANSI_TEXT_YELLOW :
377-
case ANSI_TEXT_BLUE :
378-
case ANSI_TEXT_MAGENTA:
379-
case ANSI_TEXT_CYAN :
380-
case ANSI_TEXT_WHITE :
381-
iCurrentForeGround = iCode - ANSI_TEXT_BLACK;
382-
break;
383-
384-
// different background colour
385-
case ANSI_BACK_BLACK :
386-
case ANSI_BACK_RED :
387-
case ANSI_BACK_GREEN :
388-
case ANSI_BACK_YELLOW :
389-
case ANSI_BACK_BLUE :
390-
case ANSI_BACK_MAGENTA:
391-
case ANSI_BACK_CYAN :
392-
case ANSI_BACK_WHITE :
393-
iCurrentBackGround = iCode - ANSI_BACK_BLACK;
394-
break;
395-
396-
} // end of switch
397-
398-
m_iNoteStyle = NORMAL;
399-
400-
// select colours
401-
if (bBold)
326+
if (nextANSI_TEXT_256_COLOUR)
327+
{
328+
iCurrentForeGround = iCode & 0xFF;
329+
SetNoteColourFore (xterm_256_colours [iCurrentForeGround]);
330+
}
331+
else
332+
{
333+
iCurrentBackGround = iCode & 0xFF;
334+
SetNoteColourBack (xterm_256_colours [iCurrentBackGround]);
335+
}
336+
}
337+
// here if we have had ESC [ 38 or ESC [ 48
338+
// we expect a 5 to follow, otherwise cancel
339+
else if (gotANSI_TEXT_256_COLOUR || gotANSI_BACK_256_COLOUR)
402340
{
403-
SetNoteColourFore (m_boldcolour [iCurrentForeGround]);
404-
SetNoteColourBack (m_normalcolour [iCurrentBackGround]);
405-
m_iNoteStyle |= HILITE;
341+
if (iCode == 5)
342+
{
343+
if (gotANSI_TEXT_256_COLOUR)
344+
nextANSI_TEXT_256_COLOUR = true;
345+
else
346+
nextANSI_BACK_256_COLOUR = true;
347+
}
348+
else
349+
{
350+
gotANSI_TEXT_256_COLOUR = false;
351+
gotANSI_BACK_256_COLOUR = false;
352+
nextANSI_TEXT_256_COLOUR = false;
353+
nextANSI_BACK_256_COLOUR = false;
354+
} // not 5
406355
}
407356
else
357+
{ // we are not doing 256-colour ANSI right now
358+
switch (iCode)
359+
{
360+
// reset colours to defaults
361+
case ANSI_RESET:
362+
iCurrentForeGround = WHITE;
363+
iCurrentBackGround = BLACK;
364+
bBold = false;
365+
bInverse = false;
366+
bItalic = false;
367+
bUnderline = false;
368+
break;
369+
370+
// bold
371+
case ANSI_BOLD:
372+
bBold = true;
373+
break;
374+
375+
// inverse
376+
case ANSI_INVERSE:
377+
bInverse = true;
378+
break;
379+
380+
// blink
381+
case ANSI_BLINK:
382+
case ANSI_SLOW_BLINK:
383+
case ANSI_FAST_BLINK:
384+
bItalic = true;
385+
break;
386+
387+
// underline
388+
case ANSI_UNDERLINE:
389+
bUnderline = true;
390+
break;
391+
392+
// not bold
393+
case ANSI_CANCEL_BOLD:
394+
bBold = false;
395+
break;
396+
397+
// not inverse
398+
case ANSI_CANCEL_INVERSE:
399+
bInverse = false;
400+
break;
401+
402+
// not blink
403+
case ANSI_CANCEL_BLINK:
404+
case ANSI_CANCEL_SLOW_BLINK:
405+
bItalic = false;
406+
break;
407+
408+
// not underline
409+
case ANSI_CANCEL_UNDERLINE:
410+
bUnderline = false;
411+
break;
412+
413+
// different foreground colour
414+
case ANSI_TEXT_BLACK:
415+
case ANSI_TEXT_RED :
416+
case ANSI_TEXT_GREEN :
417+
case ANSI_TEXT_YELLOW :
418+
case ANSI_TEXT_BLUE :
419+
case ANSI_TEXT_MAGENTA:
420+
case ANSI_TEXT_CYAN :
421+
case ANSI_TEXT_WHITE :
422+
iCurrentForeGround = iCode - ANSI_TEXT_BLACK;
423+
break;
424+
425+
// different background colour
426+
case ANSI_BACK_BLACK :
427+
case ANSI_BACK_RED :
428+
case ANSI_BACK_GREEN :
429+
case ANSI_BACK_YELLOW :
430+
case ANSI_BACK_BLUE :
431+
case ANSI_BACK_MAGENTA:
432+
case ANSI_BACK_CYAN :
433+
case ANSI_BACK_WHITE :
434+
iCurrentBackGround = iCode - ANSI_BACK_BLACK;
435+
break;
436+
437+
case ANSI_TEXT_256_COLOUR :
438+
gotANSI_TEXT_256_COLOUR = true; // 256-colour ANSI
439+
break;
440+
441+
case ANSI_BACK_256_COLOUR :
442+
gotANSI_BACK_256_COLOUR = true; // 256-colour ANSI
443+
break;
444+
445+
} // end of switch
446+
} // end of not gotANSI_TEXT_256_COLOUR || gotANSI_BACK_256_COLOUR ||
447+
// nextANSI_TEXT_256_COLOUR || nextANSI_BACK_256_COLOUR
448+
449+
// skip this stuff for 256-colour ANSI
450+
if (!gotANSI_TEXT_256_COLOUR &&
451+
!gotANSI_BACK_256_COLOUR &&
452+
!nextANSI_TEXT_256_COLOUR &&
453+
!nextANSI_BACK_256_COLOUR)
408454
{
409-
SetNoteColourFore (m_normalcolour [iCurrentForeGround]);
410-
SetNoteColourBack (m_normalcolour [iCurrentBackGround]);
411-
}
412-
413-
// select other style bits
414-
if (bInverse)
415-
m_iNoteStyle |= INVERSE;
416-
417-
if (bItalic)
418-
m_iNoteStyle |= BLINK;
419-
420-
if (bUnderline)
421-
m_iNoteStyle |= UNDERLINE;
455+
m_iNoteStyle = NORMAL;
456+
457+
// select colours
458+
if (bBold)
459+
{
460+
SetNoteColourFore (m_boldcolour [iCurrentForeGround]);
461+
SetNoteColourBack (m_normalcolour [iCurrentBackGround]);
462+
m_iNoteStyle |= HILITE;
463+
}
464+
else
465+
{
466+
SetNoteColourFore (m_normalcolour [iCurrentForeGround]);
467+
SetNoteColourBack (m_normalcolour [iCurrentBackGround]);
468+
}
469+
470+
// select other style bits
471+
if (bInverse)
472+
m_iNoteStyle |= INVERSE;
473+
474+
if (bItalic)
475+
m_iNoteStyle |= BLINK;
476+
477+
if (bUnderline)
478+
m_iNoteStyle |= UNDERLINE;
479+
} // not starting 256-colour ANSI
480+
481+
// if we got the whole 256-colour ANSI sequence, cancel ready for something else
482+
if (gotANSI_TEXT_256_COLOUR && nextANSI_TEXT_256_COLOUR)
483+
gotANSI_TEXT_256_COLOUR = false;
484+
if (gotANSI_BACK_256_COLOUR && nextANSI_BACK_256_COLOUR)
485+
gotANSI_BACK_256_COLOUR = false;
422486

423487
p++; // skip m or ;
424488
} // end of ESC [ nn ; or ESC [ nn m

0 commit comments

Comments
 (0)