diff --git a/ansi_up.d.ts b/ansi_up.d.ts index 2a1ad3c..dd40842 100644 --- a/ansi_up.d.ts +++ b/ansi_up.d.ts @@ -5,6 +5,7 @@ export declare class AnsiUp { private fg; private bg; private bold; + private faint; private italic; private underline; private _use_classes; @@ -14,6 +15,10 @@ 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; @@ -21,6 +26,14 @@ export declare class AnsiUp { 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; diff --git a/ansi_up.js b/ansi_up.js index 12661fd..0668afa 100644 --- a/ansi_up.js +++ b/ansi_up.js @@ -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; @@ -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 = [ @@ -254,7 +267,7 @@ 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(';'); @@ -262,23 +275,31 @@ export class AnsiUp { 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; } @@ -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(',')})`); diff --git a/ansi_up.ts b/ansi_up.ts index d16b6cf..19e96fd 100644 --- a/ansi_up.ts +++ b/ansi_up.ts @@ -23,6 +23,7 @@ interface TextWithAttr { fg:AU_Color; bg:AU_Color; bold:boolean; + faint:boolean; italic: boolean; underline: boolean; text:string; @@ -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; @@ -80,6 +82,12 @@ export class AnsiUp private _buffer:string; + private _boldStyle:string; + private _faintStyle:string; + private _italicStyle:string; + private _underlineStyle:string; + + constructor() { // All construction occurs here @@ -87,6 +95,7 @@ export class AnsiUp this._use_classes = false; this.bold = false; + this.faint = false; this.italic = false; this.underline = false; this.fg = this.bg = null; @@ -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) @@ -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 { @@ -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) @@ -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 @@ -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 diff --git a/test/ansi_up-test.js b/test/ansi_up-test.js index bbe0002..f2fe7a0 100644 --- a/test/ansi_up-test.js +++ b/test/ansi_up-test.js @@ -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 = " " + attr + ";" + fg + " "; + + 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;