Skip to content

Commit 1ef6ff6

Browse files
committed
Fix the terminal size response returns
1 parent f32578a commit 1ef6ff6

4 files changed

Lines changed: 33 additions & 8 deletions

File tree

Sources/SwiftTerm/Mac/MacTerminalView.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,10 +1313,9 @@ open class TerminalView: NSView, NSTextInputClient, NSUserInterfaceValidations,
13131313
case .reportTextAreaPixelDimension:
13141314
guard let cellDimension else { return nil }
13151315
let factor = self.backingScaleFactor()
1316-
let h = Int(bounds.height/cellDimension.height/factor) * terminal.rows
1317-
let w = Int(bounds.width/cellDimension.width/factor) * terminal.cols
1318-
1319-
return terminal.cc.CSI + "5;\(h);\(w)t".utf8
1316+
let h = Int(round(cellDimension.height * factor * CGFloat(terminal.rows)))
1317+
let w = Int(round(cellDimension.width * factor * CGFloat(terminal.cols)))
1318+
return terminal.cc.CSI + "4;\(h);\(w)t".utf8
13201319
case .reportSizeOfScreenInPixels:
13211320
return nil
13221321
case .reportTextAreaCharacters:

Sources/SwiftTerm/Terminal.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2756,25 +2756,30 @@ open class Terminal {
27562756
if let r = tdel.windowCommand(source: self, command: .reportTextAreaPixelDimension) {
27572757
sendResponse(r)
27582758
} else {
2759-
sendResponse (cc.CSI, "5;768;1024t")
2759+
let cellSize = tdel.cellSizeInPixels(source: self) ?? (width: 10, height: 16)
2760+
sendResponse(cc.CSI, "4;\(rows * cellSize.height);\(cols * cellSize.width)t")
27602761
}
27612762
case [14, 2]:
27622763
if let r = tdel.windowCommand(source: self, command: .reportTerminalWindowPixelDimension) {
27632764
sendResponse(r)
27642765
} else {
2765-
sendResponse (cc.CSI, "5;768;1024t")
2766+
let cellSize = tdel.cellSizeInPixels(source: self) ?? (width: 10, height: 16)
2767+
sendResponse(cc.CSI, "4;\(rows * cellSize.height);\(cols * cellSize.width)t")
27662768
}
27672769
case [15]: // Report size in pixels
27682770
if let r = tdel.windowCommand(source: self, command: .reportSizeOfScreenInPixels) {
27692771
sendResponse(r)
27702772
} else {
2771-
sendResponse (cc.CSI, "5;768;1024t")
2773+
let cellSize = tdel.cellSizeInPixels(source: self) ?? (width: 10, height: 16)
2774+
sendResponse(cc.CSI, "5;\(rows * cellSize.height);\(cols * cellSize.width)t")
27722775
}
27732776
case [16]: // Report cell size in pixels
27742777
// If no value is returned send 16x10
27752778
// TODO: should surface that to the UI, should not do this here
27762779
if let r = tdel.windowCommand(source: self, command: .reportCellSizeInPixels) {
27772780
sendResponse(r)
2781+
} else if let cellSize = tdel.cellSizeInPixels(source: self) {
2782+
sendResponse(cc.CSI, "6;\(cellSize.height);\(cellSize.width)t")
27782783
} else {
27792784
sendResponse (cc.CSI, "6;16;10t")
27802785
}

Sources/SwiftTerm/iOS/iOSTerminalView.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1626,7 +1626,23 @@ open class TerminalView: UIScrollView, UITextInputTraits, UIKeyInput, UIScrollVi
16261626

16271627
// Terminal.Delegate method implementation
16281628
open func windowCommand(source: Terminal, command: Terminal.WindowManipulationCommand) -> [UInt8]? {
1629-
return nil
1629+
switch command {
1630+
case .reportTextAreaPixelDimension, .reportTerminalWindowPixelDimension:
1631+
guard let cellSize = cellSizeInPixels(source: source) else { return nil }
1632+
let height = cellSize.height * source.rows
1633+
let width = cellSize.width * source.cols
1634+
return source.cc.CSI + "4;\(height);\(width)t".utf8
1635+
case .reportSizeOfScreenInPixels:
1636+
guard let cellSize = cellSizeInPixels(source: source) else { return nil }
1637+
let height = cellSize.height * source.rows
1638+
let width = cellSize.width * source.cols
1639+
return source.cc.CSI + "5;\(height);\(width)t".utf8
1640+
case .reportCellSizeInPixels:
1641+
guard let cellSize = cellSizeInPixels(source: source) else { return nil }
1642+
return source.cc.CSI + "6;\(cellSize.height);\(cellSize.width)t".utf8
1643+
default:
1644+
return nil
1645+
}
16301646
}
16311647

16321648
public func clipboardCopy(source: Terminal, content: Data) {

Tests/SwiftTermTests/TerminalTestHarness.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Testing
33

44
final class TerminalTestDelegate: TerminalDelegate {
55
private(set) var sentData: [[UInt8]] = []
6+
var cellSizeInPixelsValue: (width: Int, height: Int)? = nil
67

78
func showCursor(source: Terminal) {}
89
func hideCursor(source: Terminal) {}
@@ -18,6 +19,10 @@ final class TerminalTestDelegate: TerminalDelegate {
1819
func send(source: Terminal, data: ArraySlice<UInt8>) {
1920
sentData.append(Array(data))
2021
}
22+
23+
func cellSizeInPixels(source: Terminal) -> (width: Int, height: Int)? {
24+
return cellSizeInPixelsValue
25+
}
2126
}
2227

2328
enum TerminalTestHarness {

0 commit comments

Comments
 (0)