Skip to content

Commit

Permalink
Add support for faint
Browse files Browse the repository at this point in the history
  • Loading branch information
drudru committed Aug 14, 2023
1 parent c1fd665 commit ad72b8f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 42 deletions.
13 changes: 13 additions & 0 deletions ansi_up.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export declare class AnsiUp {
private fg;
private bg;
private bold;
private faint;
private italic;
private underline;
private _use_classes;
Expand All @@ -14,13 +15,25 @@ export declare class AnsiUp {
private _url_whitelist;
private _escape_html;
private _buffer;
private _boldStyle;
private _faintStyle;
private _italicStyle;
private _underlineStyle;
constructor();
set use_classes(arg: boolean);
get use_classes(): boolean;
set url_whitelist(arg: {});
get url_whitelist(): {};
set escape_html(arg: boolean);
get escape_html(): boolean;
set boldStyle(arg: string);
get boldStyle(): string;
set faintStyle(arg: string);
get faintStyle(): string;
set italicStyle(arg: string);
get italicStyle(): string;
set underlineStyle(arg: string);
get underlineStyle(): string;
private setup_palettes;
private escape_txt_for_html;
private append_buffer;
Expand Down
35 changes: 29 additions & 6 deletions ansi_up.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ export class AnsiUp {
this.setup_palettes();
this._use_classes = false;
this.bold = false;
this.faint = false;
this.italic = false;
this.underline = false;
this.fg = this.bg = null;
this._buffer = '';
this._url_whitelist = { 'http': 1, 'https': 1 };
this._escape_html = true;
this.boldStyle = 'font-weight:bold';
this.faintStyle = 'opacity:0.7';
this.italicStyle = 'font-style:italic';
this.underlineStyle = 'text-decoration:underline';
}
set use_classes(arg) {
this._use_classes = arg;
Expand All @@ -44,6 +49,14 @@ export class AnsiUp {
get escape_html() {
return this._escape_html;
}
set boldStyle(arg) { this._boldStyle = arg; }
get boldStyle() { return this._boldStyle; }
set faintStyle(arg) { this._faintStyle = arg; }
get faintStyle() { return this._faintStyle; }
set italicStyle(arg) { this._italicStyle = arg; }
get italicStyle() { return this._italicStyle; }
set underlineStyle(arg) { this._underlineStyle = arg; }
get underlineStyle() { return this._underlineStyle; }
setup_palettes() {
this.ansi_colors =
[
Expand Down Expand Up @@ -254,31 +267,39 @@ export class AnsiUp {
return blocks.join("");
}
with_state(pkt) {
return { bold: this.bold, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text };
return { bold: this.bold, faint: this.faint, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text };
}
process_ansi(pkt) {
let sgr_cmds = pkt.text.split(';');
while (sgr_cmds.length > 0) {
let sgr_cmd_str = sgr_cmds.shift();
let num = parseInt(sgr_cmd_str, 10);
if (isNaN(num) || num === 0) {
this.fg = this.bg = null;
this.fg = null;
this.bg = null;
this.bold = false;
this.faint = false;
this.italic = false;
this.underline = false;
}
else if (num === 1) {
this.bold = true;
}
else if (num === 2) {
this.faint = true;
}
else if (num === 3) {
this.italic = true;
}
else if (num === 4) {
this.underline = true;
}
else if (num === 22) {
else if (num === 21) {
this.bold = false;
}
else if (num === 22) {
this.faint = false;
}
else if (num === 23) {
this.italic = false;
}
Expand Down Expand Up @@ -344,11 +365,13 @@ export class AnsiUp {
let fg = fragment.fg;
let bg = fragment.bg;
if (fragment.bold)
styles.push('font-weight:bold');
styles.push(this._boldStyle);
if (fragment.faint)
styles.push(this._faintStyle);
if (fragment.italic)
styles.push('font-style:italic');
styles.push(this._italicStyle);
if (fragment.underline)
styles.push('text-decoration:underline');
styles.push(this._underlineStyle);
if (!this._use_classes) {
if (fg)
styles.push(`color:rgb(${fg.rgb.join(',')})`);
Expand Down
90 changes: 54 additions & 36 deletions ansi_up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ interface TextWithAttr {
fg:AU_Color;
bg:AU_Color;
bold:boolean;
faint:boolean;
italic: boolean;
underline: boolean;
text:string;
Expand Down Expand Up @@ -66,6 +67,7 @@ export class AnsiUp
private fg:AU_Color;
private bg:AU_Color;
private bold:boolean;
private faint:boolean;
private italic: boolean;
private underline:boolean;
private _use_classes:boolean;
Expand All @@ -80,13 +82,20 @@ export class AnsiUp

private _buffer:string;

private _boldStyle:string;
private _faintStyle:string;
private _italicStyle:string;
private _underlineStyle:string;


constructor()
{
// All construction occurs here
this.setup_palettes();
this._use_classes = false;

this.bold = false;
this.faint = false;
this.italic = false;
this.underline = false;
this.fg = this.bg = null;
Expand All @@ -95,6 +104,11 @@ export class AnsiUp

this._url_whitelist = { 'http':1, 'https':1 };
this._escape_html = true;

this.boldStyle = 'font-weight:bold';
this.faintStyle = 'opacity:0.7';
this.italicStyle = 'font-style:italic';
this.underlineStyle = 'text-decoration:underline'
}

set use_classes(arg:boolean)
Expand Down Expand Up @@ -127,6 +141,15 @@ export class AnsiUp
return this._escape_html;
}

set boldStyle(arg:string) { this._boldStyle = arg; }
get boldStyle():string { return this._boldStyle; }
set faintStyle(arg:string) { this._faintStyle = arg; }
get faintStyle():string { return this._faintStyle; }
set italicStyle(arg:string) { this._italicStyle = arg; }
get italicStyle():string { return this._italicStyle; }
set underlineStyle(arg:string) { this._underlineStyle = arg; }
get underlineStyle():string { return this._underlineStyle; }


private setup_palettes():void
{
Expand Down Expand Up @@ -570,7 +593,7 @@ export class AnsiUp
}

private with_state(pkt:TextPacket):TextWithAttr {
return { bold: this.bold, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text };
return { bold: this.bold, faint: this.faint, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text };
}

private process_ansi(pkt:TextPacket)
Expand All @@ -583,39 +606,38 @@ export class AnsiUp

// Why do we shift through the array instead of a forEach??
// ... because some commands consume the params that follow !

while (sgr_cmds.length > 0) {
let sgr_cmd_str = sgr_cmds.shift();
let num = parseInt(sgr_cmd_str, 10);

// TODO
// AT SOME POINT, JUST CONVERT TO A LOOKUP TABLE
if (isNaN(num) || num === 0) {
this.fg = this.bg = null;
this.bold = false;
this.italic = false;
this.fg = null;
this.bg = null;
this.bold = false;
this.faint = false;
this.italic = false;
this.underline = false;
} else if (num === 1) {
this.bold = true;
} else if (num === 3) {
this.italic = true;
} else if (num === 4) {
this.underline = true;
} else if (num === 22) {
this.bold = false;
} else if (num === 23) {
this.italic = false;
} else if (num === 24) {
this.underline = false;
} else if (num === 39) {
this.fg = null;
} else if (num === 49) {
this.bg = null;
} else if ((num >= 30) && (num < 38)) {
this.fg = this.ansi_colors[0][(num - 30)];
} else if ((num >= 40) && (num < 48)) {
this.bg = this.ansi_colors[0][(num - 40)];
} else if ((num >= 90) && (num < 98)) {
this.fg = this.ansi_colors[1][(num - 90)];
} else if ((num >= 100) && (num < 108)) {
this.bg = this.ansi_colors[1][(num - 100)];

} else if (num === 1) { this.bold = true;
} else if (num === 2) { this.faint = true;
} else if (num === 3) { this.italic = true;
} else if (num === 4) { this.underline = true;
} else if (num === 21) { this.bold = false;
} else if (num === 22) { this.faint = false;
} else if (num === 23) { this.italic = false;
} else if (num === 24) { this.underline = false;

} else if (num === 39) { this.fg = null;
} else if (num === 49) { this.bg = null;

} else if ((num >= 30) && (num < 38)) { this.fg = this.ansi_colors[0][(num - 30)];
} else if ((num >= 40) && (num < 48)) { this.bg = this.ansi_colors[0][(num - 40)];
} else if ((num >= 90) && (num < 98)) { this.fg = this.ansi_colors[1][(num - 90)];
} else if ((num >= 100) && (num < 108)) { this.bg = this.ansi_colors[1][(num - 100)];

} else if (num === 38 || num === 48) {

// extended set foreground/background color
Expand Down Expand Up @@ -676,14 +698,10 @@ export class AnsiUp
let bg = fragment.bg;

// Note on bold: https://stackoverflow.com/questions/6737005/what-are-some-advantages-to-using-span-style-font-weightbold-rather-than-b?rq=1
if (fragment.bold)
styles.push('font-weight:bold');

if (fragment.italic)
styles.push('font-style:italic');

if (fragment.underline)
styles.push('text-decoration:underline');
if (fragment.bold) styles.push(this._boldStyle);
if (fragment.faint) styles.push(this._faintStyle);
if (fragment.italic) styles.push(this._italicStyle);
if (fragment.underline) styles.push(this._underlineStyle);

if (!this._use_classes) {
// USE INLINE STYLES
Expand Down
12 changes: 12 additions & 0 deletions test/ansi_up-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ describe('ansi_up', function () {
l.should.eql(expected);
});

it('should transform a faint attr;foreground to html', function () {
var attr = 2;
var fg = 32;
var start = "\x1B[" + attr + ";" + fg + "m " + attr + ";" + fg + " \x1B[0m";

var expected = "<span style=\"opacity:0.7;color:rgb(0,187,0)\"> " + attr + ";" + fg + " </span>";

var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});

it('should transform an italic attr;foreground to html', function () {
var attr = 3;
var fg = 32;
Expand Down

0 comments on commit ad72b8f

Please sign in to comment.