Ansi2Html

main

A Swift library to convert text with ANSI escape codes to HTML
OakCityLabs/Ansi2Html

Ansi2Html

A Swift library to convert text with ANSI escape codes to HTML.

The heavy lifting of the conversion is done by the ANSIEsc2HTML library. This SPM includes a slightly modified version of the library copied on 2023/5/5.

Usages

Direct Usage

You can create a string builder and use the conversion method directly on the builder.

import Ansi2Html

let builder = AnsiHtmlStringBuilder()

if builder.stringHasAnsi(stringWithAnsiCodes) {
    let html = builder.stringToHtml(stringWithAnsiCodes)
}

String Extension

It may be more useful to add an extension on string for convenience.

import Ansi2Html

private let builder = AnsiHtmlStringBuilder()

extension String {
    
    var ansiToHtml: String {
        guard hasAnsi else { return self }
        
        return builder.stringToHtml(self)
    }
    
    var hasAnsi: Bool {
        builder.stringHasAnsi(self)
    }
    
}

Elsewhere in your code you can use the ansiToHtml computed attribute.

let html = stringWithAnsiCodes.ansiToHtml

Description

  • Swift Tools 5.8.0
View More Packages from this Author

Dependencies

Last updated: Fri Apr 12 2024 10:53:42 GMT-0900 (Hawaii-Aleutian Daylight Time)