-
-
Notifications
You must be signed in to change notification settings - Fork 6k
Switch to ansi_up for ansi rendering in actions #25401
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
84dc590
Switch to ansi_up for ansi rendering in actions
silverwind 4b05ec1
fix up tests
silverwind 872b9f9
move to own file, restore replacements and all previous tests
silverwind 4652457
add 256 color test and comments
silverwind 567e386
undo gitignore change
silverwind 3d14ba0
unwrap commment
silverwind 77393f4
Merge branch 'main' into consolecolors
GiteaBot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import AnsiUp from 'ansi_up'; | ||
|
||
const replacements = [ | ||
[/\x1b\[\d+[A-H]/g, ''], // Move cursor, treat them as no-op | ||
[/\x1b\[\d?[JK]/g, '\r'], // Erase display/line, treat them as a Carriage Return | ||
]; | ||
|
||
// render ANSI to HTML | ||
export function renderAnsi(line) { | ||
// create a fresh ansi_up instance because otherwise previous renders can influence | ||
// the output of future renders, because ansi_up is stateful and remembers things like | ||
// unclosed opening tags for colors. | ||
const ansi_up = new (AnsiUp.default || AnsiUp)(); | ||
|
||
if (line.endsWith('\r\n')) { | ||
line = line.substring(0, line.length - 2); | ||
} else if (line.endsWith('\n')) { | ||
line = line.substring(0, line.length - 1); | ||
} | ||
|
||
if (line.includes('\x1b')) { | ||
for (const [regex, replacement] of replacements) { | ||
line = line.replace(regex, replacement); | ||
} | ||
} | ||
|
||
if (!line.includes('\r')) { | ||
return ansi_up.ansi_to_html(line); | ||
} | ||
|
||
// handle "\rReading...1%\rReading...5%\rReading...100%", | ||
// convert it into a multiple-line string: "Reading...1%\nReading...5%\nReading...100%" | ||
const lines = []; | ||
for (const part of line.split('\r')) { | ||
if (part === '') continue; | ||
const partHtml = ansi_up.ansi_to_html(part); | ||
if (partHtml !== '') { | ||
lines.push(partHtml); | ||
} | ||
} | ||
|
||
// the log message element is with "white-space: break-spaces;", so use "\n" to break lines | ||
return lines.join('\n'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import {expect, test} from 'vitest'; | ||
import {renderAnsi} from './ansi.js'; | ||
|
||
test('renderAnsi', () => { | ||
expect(renderAnsi('abc')).toEqual('abc'); | ||
expect(renderAnsi('abc\n')).toEqual('abc'); | ||
expect(renderAnsi('abc\r\n')).toEqual('abc'); | ||
expect(renderAnsi('\r')).toEqual(''); | ||
expect(renderAnsi('\rx\rabc')).toEqual('x\nabc'); | ||
expect(renderAnsi('\rabc\rx\r')).toEqual('abc\nx'); | ||
expect(renderAnsi('\x1b[30mblack\x1b[37mwhite')).toEqual('<span style="color:rgb(0,0,0)">black</span><span style="color:rgb(255,255,255)">white</span>'); // unclosed | ||
expect(renderAnsi('<script>')).toEqual('<script>'); | ||
expect(renderAnsi('\x1b[1A\x1b[2Ktest\x1b[1B\x1b[1A\x1b[2K')).toEqual('test'); | ||
expect(renderAnsi('\x1b[1A\x1b[2K\rtest\r\x1b[1B\x1b[1A\x1b[2K')).toEqual('test'); | ||
expect(renderAnsi('\x1b[1A\x1b[2Ktest\x1b[1B\x1b[1A\x1b[2K')).toEqual('test'); | ||
expect(renderAnsi('\x1b[1A\x1b[2K\rtest\r\x1b[1B\x1b[1A\x1b[2K')).toEqual('test'); | ||
|
||
// treat "\033[0K" and "\033[0J" (Erase display/line) as "\r", then it will be covered to "\n" finally. | ||
expect(renderAnsi('a\x1b[Kb\x1b[2Jc')).toEqual('a\nb\nc'); | ||
expect(renderAnsi('\x1b[48;5;88ma\x1b[38;208;48;5;159mb\x1b[m')).toEqual(`<span style="background-color:rgb(135,0,0)">a</span><span style="background-color:rgb(175,255,255)">b</span>`); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.