Parcourir la source

Improve color parsing, add some tests

Miguel de Icaza il y a 4 ans
Parent
commit
70a3af884c

+ 6 - 0
Sources/SwiftTerm/HeadlessTerminal.swift

@@ -60,6 +60,12 @@ public class HeadlessTerminal : TerminalDelegate, LocalProcessDelegate {
     }
     public func colorChanged(source: Terminal, idx: Int) {
     }
+    
+    public var images: [([UInt8], Int, Int)] = []
+    
+    public func createImageFromBitmap (source: Terminal, bytes: inout [UInt8], width: Int, height: Int)  {
+        images.append((bytes, width, height))
+    }
 }
 
 #endif

+ 15 - 9
Sources/SwiftTerm/Terminal.swift

@@ -2964,15 +2964,21 @@ open class Terminal {
             
             // If this is the new style
             if v.count > 2 && v [2] == UInt8(ascii: ":") {
-                // Color style, we ignore "ColorSpace"
-                i += 1
-                if i+3 < parCount {
-                    color = Attribute.Color.trueColor(
-                          red: UInt8(min (pars [i+1], 255)),
-                        green: UInt8(min (pars [i+2], 255)),
-                         blue: UInt8(min (pars [i+3], 255)))
+                switch pars [i] {
+                case 2: // RGB color
+                    i += 1
+                    // Color style, we ignore "ColorSpace"
+
+                    if i+3 < parCount {
+                        color = Attribute.Color.trueColor(
+                              red: UInt8(min (pars [i+1], 255)),
+                            green: UInt8(min (pars [i+2], 255)),
+                             blue: UInt8(min (pars [i+3], 255)))
+                        i += 4
+                    }
+                default:
+                    break
                 }
-                i += 4
             } else {
                 switch pars [i] {
                 case 2: // RGB color
@@ -2982,8 +2988,8 @@ open class Terminal {
                               red: UInt8(min (pars [i], 255)),
                             green: UInt8(min (pars [i+1], 255)),
                              blue: UInt8(min (pars [i+2], 255)))
+                        i += 3
                     }
-                    i += 3
                     
                 case 3: // CMY color - not supported
                     break

+ 47 - 0
Tests/SwiftTermTests/ColorTests.swift

@@ -0,0 +1,47 @@
+//
+//  File.swift
+//  
+//
+//  Created by Miguel de Icaza on 4/29/21.
+//
+//
+import XCTest
+import Foundation
+
+@testable import SwiftTerm
+
+final class ColorTests: XCTestCase {
+
+    func testExtendedColor ()
+    {
+        let h = HeadlessTerminal (queue: SwiftTermTests.queue) { exitCode in }
+        
+        let t = h.terminal!
+        
+        // This tests that we are setting both foreground and background colors
+        // using the semicolon style (there was some ambiguity that I had to deal with).
+        t.feed (text: "\u{1b}[38;2;19;49;174;48;2;23;56;179mString\n\r")
+        var chattr = t.buffer.getChar(at: Position (col: 0, row: 0))
+        XCTAssertEqual(chattr.code, Int32(UInt8(ascii: "S")))
+        XCTAssertEqual(chattr.attribute.fg, .trueColor(red: 19, green: 49, blue: 174))
+        XCTAssertEqual(chattr.attribute.bg, .trueColor(red: 23, green: 56, blue: 179))
+        
+        // This is the new style
+        t.feed (text: "\u{1b}[38:2::255:10:255mHello\n\r")
+        chattr = t.buffer.getChar(at: Position (col: 0, row: 1))
+        XCTAssertEqual(chattr.code, Int32(UInt8(ascii: "H")))
+        XCTAssertEqual(chattr.attribute.fg, .trueColor(red: 255, green: 10, blue: 255))
+        XCTAssertEqual(chattr.attribute.bg, .trueColor(red: 23, green: 56, blue: 179))
+        
+        // Partial sequences do not get parsed
+        t.feed (text: "\u{1b}[38:2::255mPartial\n\r")
+        chattr = t.buffer.getChar(at: Position (col: 0, row: 2))
+        XCTAssertEqual(chattr.code, Int32(UInt8(ascii: "P")))
+        XCTAssertEqual(chattr.attribute.bg, .defaultColor)
+        XCTAssertEqual(chattr.attribute.fg, .defaultColor)
+    }
+    
+    static var allTests = [
+        ("testColors", testExtendedColor)
+    ]
+}

Fichier diff supprimé car celui-ci est trop grand
+ 19 - 0
Tests/SwiftTermTests/ImageTests.swift


+ 3 - 1
Tests/SwiftTermTests/Memory.swift

@@ -1,5 +1,7 @@
 //
-//  File.swift
+//  Memory.swift - Ensures that an allocated terminal is deallocated, this is
+// to make sure we do not regress when we use helper classes that might introduce
+// a strong cycle.
 //  
 //
 //  Created by Miguel de Icaza on 4/17/21.

+ 0 - 1
Tests/SwiftTermTests/OscTests.swift

@@ -22,7 +22,6 @@ final class SwiftTermOsc: XCTestCase {
         XCTAssertEqual(t.hostCurrentDirectory, nil)
         t.feed (text: "\u{1b}]7;file:///localhost/usr/bin\u{7}")
         XCTAssertEqual(t.hostCurrentDirectory!, "file:///localhost/usr/bin")
-        
     }
     
     static var allTests = [

Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff