Skip to content

Commit

Permalink
Merge pull request #67 from DaoDaoNoCode/master
Browse files Browse the repository at this point in the history
Add italic and underline styles
  • Loading branch information
drudru committed Nov 2, 2021
2 parents aaec94d + 628128f commit 8eec201
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 15 deletions.
24 changes: 22 additions & 2 deletions ansi_up.js
Expand Up @@ -38,6 +38,8 @@ var AnsiUp = (function () {
this.setup_palettes();
this._use_classes = false;
this.bold = false;
this.italic = false;
this.underline = false;
this.fg = this.bg = null;
this._buffer = '';
this._url_whitelist = { 'http': 1, 'https': 1 };
Expand Down Expand Up @@ -266,7 +268,7 @@ var AnsiUp = (function () {
return blocks.join("");
};
AnsiUp.prototype.with_state = function (pkt) {
return { bold: this.bold, fg: this.fg, bg: this.bg, text: pkt.text };
return { bold: this.bold, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text };
};
AnsiUp.prototype.process_ansi = function (pkt) {
var sgr_cmds = pkt.text.split(';');
Expand All @@ -276,13 +278,27 @@ var AnsiUp = (function () {
if (isNaN(num) || num === 0) {
this.fg = this.bg = null;
this.bold = 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;
}
Expand Down Expand Up @@ -335,14 +351,18 @@ var AnsiUp = (function () {
if (txt.length === 0)
return txt;
txt = this.escape_txt_for_html(txt);
if (!fragment.bold && fragment.fg === null && fragment.bg === null)
if (!fragment.bold && !fragment.italic && !fragment.underline && fragment.fg === null && fragment.bg === null)
return txt;
var styles = [];
var classes = [];
var fg = fragment.fg;
var bg = fragment.bg;
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 (!this._use_classes) {
if (fg)
styles.push("color:rgb(" + fg.rgb.join(',') + ")");
Expand Down
29 changes: 25 additions & 4 deletions ansi_up.ts
Expand Up @@ -23,6 +23,8 @@ interface TextWithAttr {
fg:AU_Color;
bg:AU_Color;
bold:boolean;
italic: boolean;
underline: boolean;
text:string;
}

Expand Down Expand Up @@ -64,7 +66,8 @@ class AnsiUp
private fg:AU_Color;
private bg:AU_Color;
private bold:boolean;

private italic: boolean;
private underline:boolean;
private _use_classes:boolean;

private _csi_regex:RegExp;
Expand All @@ -83,6 +86,8 @@ class AnsiUp
this._use_classes = false;

this.bold = false;
this.italic = false;
this.underline = false;
this.fg = this.bg = null;

this._buffer = '';
Expand Down Expand Up @@ -538,7 +543,7 @@ class AnsiUp
}

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

private process_ansi(pkt:TextPacket)
Expand All @@ -558,10 +563,20 @@ class AnsiUp
if (isNaN(num) || num === 0) {
this.fg = this.bg = null;
this.bold = 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) {
Expand Down Expand Up @@ -624,7 +639,7 @@ class AnsiUp
txt = this.escape_txt_for_html(txt);

// If colors not set, default style is used
if (!fragment.bold && fragment.fg === null && fragment.bg === null)
if (!fragment.bold && !fragment.italic && !fragment.underline && fragment.fg === null && fragment.bg === null)
return txt;

let styles:string[] = [];
Expand All @@ -635,7 +650,13 @@ class AnsiUp

// 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')
styles.push('font-weight:bold');

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

if (fragment.underline)
styles.push('text-decoration:underline');

if (!this._use_classes) {
// USE INLINE STYLES
Expand Down
4 changes: 4 additions & 0 deletions dist/ansi_up.d.ts
Expand Up @@ -6,6 +6,8 @@ export interface TextWithAttr {
fg: AU_Color;
bg: AU_Color;
bold: boolean;
italic: boolean;
underline: boolean;
text: string;
}
declare enum PacketKind {
Expand All @@ -29,6 +31,8 @@ export default class AnsiUp {
private fg;
private bg;
private bold;
private italic;
private underline;
private _use_classes;
private _csi_regex;
private _osc_st;
Expand Down
24 changes: 22 additions & 2 deletions dist/ansi_up.js.include
Expand Up @@ -19,6 +19,8 @@ var AnsiUp = (function () {
this.setup_palettes();
this._use_classes = false;
this.bold = false;
this.italic = false;
this.underline = false;
this.fg = this.bg = null;
this._buffer = '';
this._url_whitelist = { 'http': 1, 'https': 1 };
Expand Down Expand Up @@ -247,7 +249,7 @@ var AnsiUp = (function () {
return blocks.join("");
};
AnsiUp.prototype.with_state = function (pkt) {
return { bold: this.bold, fg: this.fg, bg: this.bg, text: pkt.text };
return { bold: this.bold, italic: this.italic, underline: this.underline, fg: this.fg, bg: this.bg, text: pkt.text };
};
AnsiUp.prototype.process_ansi = function (pkt) {
var sgr_cmds = pkt.text.split(';');
Expand All @@ -257,13 +259,27 @@ var AnsiUp = (function () {
if (isNaN(num) || num === 0) {
this.fg = this.bg = null;
this.bold = 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;
}
Expand Down Expand Up @@ -316,14 +332,18 @@ var AnsiUp = (function () {
if (txt.length === 0)
return txt;
txt = this.escape_txt_for_html(txt);
if (!fragment.bold && fragment.fg === null && fragment.bg === null)
if (!fragment.bold && !fragment.italic && !fragment.underline && fragment.fg === null && fragment.bg === null)
return txt;
var styles = [];
var classes = [];
var fg = fragment.fg;
var bg = fragment.bg;
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 (!this._use_classes) {
if (fg)
styles.push("color:rgb(" + fg.rgb.join(',') + ")");
Expand Down

0 comments on commit 8eec201

Please sign in to comment.