Browse Source

Assorted commands implemented and assorted fixes

Miguel de Icaza 5 years ago
parent
commit
4fbee8ebc5

+ 5 - 1
MacTerminal/AppDelegate.swift

@@ -10,6 +10,10 @@ import Cocoa
 import SwiftUI
 import SwiftTerm
 
+class TerminalViewController: NSViewController {
+    
+}
+
 @NSApplicationMain
 class AppDelegate: NSObject, NSApplicationDelegate, LocalProcessTerminalViewDelegate {
     func sizeChanged(source: LocalProcessTerminalView, newCols: Int, newRows: Int) {
@@ -35,7 +39,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, LocalProcessTerminalViewDele
         // Create the window and set the content view. 
         window = NSWindow(
             contentRect: NSRect(x: 0, y: 0, width: 480, height: 300),
-            styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView],
+            styleMask: [.titled, .closable, .miniaturizable, .resizable], //, .fullSizeContentView],
             backing: .buffered, defer: false)
         window.center()
         window.setFrameAutosaveName("Main Window")

+ 4 - 0
README.md

@@ -57,3 +57,7 @@ Desired:
 
 
 Against version: 57cf109188551c5d5e7fa7d2158448b4e8d2be64 from Feb 27, 2020
+
+# Features
+
+* 

+ 1 - 1
SwiftTerm/Sources/SwiftTerm/CharData.swift

@@ -24,7 +24,7 @@ struct CharacterAttribute : OptionSet {
      */
     public init (attribute: Int32)
     {
-        rawValue = UInt8 ((attribute >> 18) & 127)
+        rawValue = UInt8 ((attribute >> 18) & 0xff)
     }
     static let bold = CharacterAttribute (rawValue: 1)
     static let underline = CharacterAttribute (rawValue: 2)

+ 3 - 1
SwiftTerm/Sources/SwiftTerm/CircularList.swift

@@ -106,9 +106,11 @@ class CircularList<T> {
         }
         if items.count != 0 {
             // add items
+            var i = count-1
             let ic = items.count
-            for i in (start...count-1).reversed () {
+            while i >= start {
                 array [getCyclicIndex(i + ic)] = array [getCyclicIndex(i)]
+                i -= 1
             }
             for i in 0..<ic {
                 array [getCyclicIndex(start + i)] = items [i]

+ 67 - 12
SwiftTerm/Sources/SwiftTerm/MacTerminalView.swift

@@ -222,6 +222,49 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
         }
     }
 
+    var userScrolling = false
+    public func scroll (toPosition: Double)
+    {
+        userScrolling = true
+        let oldPosition = terminal.buffer.yDisp
+        
+        let maxScrollback = terminal.buffer.lines.count - terminal.rows
+        var newScrollPosition = maxScrollback * Int (toPosition)
+        if newScrollPosition < 0 {
+            newScrollPosition = 0
+        }
+        if newScrollPosition > maxScrollback {
+            newScrollPosition = maxScrollback
+        }
+        
+        if newScrollPosition != oldPosition {
+            scrollTo(row: newScrollPosition)
+        }
+        userScrolling = false
+    }
+    
+    public func pageUp()
+    {
+        scrollUp (lines: terminal.rows)
+    }
+    
+    public func pageDown ()
+    {
+        scrollDown (lines: terminal.rows);
+    }
+
+    public func scrollUp (lines: Int)
+    {
+        var newPosition = max (terminal.buffer.yDisp - lines, 0)
+        scrollTo (row: newPosition)
+    }
+    
+    public func scrollDown (lines: Int)
+    {
+        var newPosition = min (terminal.buffer.yDisp + lines, terminal.buffer.lines.count - terminal.rows);
+        scrollTo (row: newPosition);
+    }
+
     var colors: [NSColor?] = Array.init(repeating: nil, count: 257)
 
     func mapColor (color: Int, isFg: Bool) -> NSColor
@@ -292,17 +335,19 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
             font = fontNormal
         }
         
+        var fgColor = mapColor (color: Int (fg), isFg: true)
         var nsattr: [NSAttributedString.Key:Any] = [
             .font: font,
-            .foregroundColor: mapColor (color: Int (fg), isFg: true),
+            .foregroundColor: fgColor,
             .backgroundColor: mapColor (color: Int (bg), isFg: false)
         ]
         if flags.contains (.underline) {
-            nsattr [.underlineColor] = mapColor (color: Int (fg), isFg: true)
+            nsattr [.underlineColor] = fgColor
             nsattr [.underlineStyle] = NSUnderlineStyle.single.rawValue
         }
         if flags.contains (.crossedOut) {
             nsattr [.strikethroughStyle] = NSUnderlineStyle.single.rawValue
+            nsattr [.strikethroughColor] = fgColor
         }
         attributes [attribute] = nsattr
         return nsattr
@@ -361,7 +406,7 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
     {
         updateCursorPosition ()
 
-        guard let (rowStart, rowEnd) = terminal.getUpdateRange() else {
+         guard let (rowStart, rowEnd) = terminal.getUpdateRange() else {
             return
         }
         
@@ -382,7 +427,7 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
         // print ("Dirty range: \(rowStart),\(rowEnd)");
         
         // BROKWN:
-        let baseLine = frame.height
+        let baseLine = frame.height - cellDelta
         let region = CGRect (x: 0,
                              y: baseLine - (cellHeight + CGFloat (rowEnd) * cellHeight) + 2*cellDelta,
                              width: frame.width,
@@ -410,10 +455,17 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
             return
         }
         context.saveGState()
+        // var path = NSBezierPath()
+        // path.move(to: NSPoint(x: 0,y: 0))
+        // path.line(to: NSPoint (x: frame.width, y: frame.height))
+        // path.lineWidth = 3
+        // NSColor.red.set ()
+        // path.stroke ()
+        
         s += 1
         let maxRow = terminal.rows
         let yDisp = terminal.buffer.yDisp
-        let baseLine = frame.height + cellDelta
+        let baseLine = frame.height - cellDelta
         for row in 0..<maxRow {
             context.textPosition = CGPoint (x: 0, y: baseLine - (cellHeight + CGFloat (row) * cellHeight))
             let attrLine = buffer [row + yDisp]
@@ -427,8 +479,7 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
     
     func updateCursorPosition ()
     {
-        let pos = getCaretPos (terminal.buffer.x, terminal.buffer.y + terminal.buffer.yBase)
-        
+        let pos = getCaretPos (terminal.buffer.x, terminal.buffer.y)
         caretView.frame = CGRect (
             // -1 to pad outside the character a little bit
             x: pos.x - 1,
@@ -443,7 +494,7 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
 
     func getCaretPos(_ x: Int, _ y: Int) -> (x: CGFloat, y: CGFloat)
     {
-        let baseLine = frame.height + cellDelta
+        let baseLine = frame.height - cellDelta
         let ip = (cellHeight + CGFloat (y) * cellHeight)
         let x_ = CGFloat (x) * cellWidth
         
@@ -696,9 +747,9 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
                     case NSRightArrowFunctionKey:
                         send (EscapeSequences.MoveRightNormal)
                     case NSPageUpFunctionKey:
-                        abort()
+                        pageUp ();
                     case NSPageDownFunctionKey:
-                        abort()
+                        pageDown();
                     default:
                         break
                     }
@@ -734,17 +785,21 @@ public class TerminalView: NSView, TerminalDelegate, NSTextInputClient, NSUserIn
             send (terminal.applicationCursor ? EscapeSequences.MoveHomeApp : EscapeSequences.MoveHomeNormal)
         case #selector(moveToEndOfLine(_:)):
             send (terminal.applicationCursor ? EscapeSequences.MoveEndApp : EscapeSequences.MoveEndNormal)
+        case #selector(scrollPageUp(_:)):
+            fallthrough
         case #selector(pageUp(_:)):
             if terminal.applicationCursor {
                 send (EscapeSequences.CmdPageUp)
             } else {
-                // TODO: view should scroll one page up
+                pageUp()
             }
+        case #selector(scrollPageDown(_:)):
+            fallthrough
         case #selector(pageDown(_:)):
             if terminal.applicationCursor {
                 send (EscapeSequences.CmdPageDown)
             } else {
-                // TODO: view should scroll one page down
+                pageDown()
             }
         case #selector(pageDownAndModifySelection(_:)):
             if terminal.applicationCursor {

+ 22 - 16
SwiftTerm/Sources/SwiftTerm/Terminal.swift

@@ -450,27 +450,32 @@ public class Terminal {
             if idx < putbackBuffer.count {
                 let v = putbackBuffer [idx]
                 idx += 1
-                if v == 27 { abort () }
                 return v
             }
             let v = rest [idx-putbackBuffer.count+rest.startIndex]
             idx += 1
-            if v == 27 { abort () }
             return v
         }
         
         // Puts back the code, and everything that was pending
         mutating func putback (_ code: UInt8)
         {
-            putbackBuffer.append (code)
-            for x in idx..<(idx+count) {
-                putbackBuffer.append (getNext ())
+            var newPutback: [UInt8] = [code]
+            let left = bytesLeft()
+            for x in 0..<left {
+                newPutback.append (getNext ())
             }
+            putbackBuffer = newPutback
             rest = [][...]
         }
         
         mutating func done  ()
         {
+            if idx < putbackBuffer.count {
+                putbackBuffer.removeFirst(idx)
+            } else {
+                putbackBuffer = []
+            }
             rest = [][...]
         }
         
@@ -495,7 +500,9 @@ public class Terminal {
         var bufferRow = buffer.lines [buffer.y + buffer.yBase]
 
         updateRange (buffer.y)
-
+        let left = readingBuffer.bytesLeft()
+        var idx = readingBuffer.idx
+        var copy = readingBuffer.putbackBuffer
         while readingBuffer.hasNext() {
             var ch: Character = " "
             var chWidth: Int = 0
@@ -570,7 +577,6 @@ public class Terminal {
             //
             // To get this off the ground, none of these operations are done.   Notice that neither
             // the JS or C# implementations solve this yet.
-            
             if screenReaderMode {
                 emitChar (ch)
             }
@@ -1527,6 +1533,7 @@ public class Terminal {
         }
 
         let parCount = pars.count
+        let empty = CharacterAttribute (attribute: 0)
         var flags = CharacterAttribute (attribute: curAttr)
         var fg = (curAttr >> 9) & 0x1ff
         var bg = curAttr & 0x1ff
@@ -1584,26 +1591,26 @@ public class Terminal {
                 flags = [flags, .dim]
             } else if p == 22 {
                 // not bold nor faint
-                flags = flags.remove (.bold)!
-                flags = flags.remove (.dim)!
+                flags = flags.remove (.bold) ?? empty
+                flags = flags.remove (.dim) ?? empty
             } else if p == 23 {
                 // not italic
-                flags = flags.remove (.italic)!
+                flags = flags.remove (.italic) ?? empty
             } else if p == 24 {
                 // not underlined
-                flags = flags.remove (.underline)!
+                flags = flags.remove (.underline) ?? empty
             } else if p == 25 {
                 // not blink
-                flags = flags.remove (.blink)!
+                flags = flags.remove (.blink) ?? empty
             } else if p == 27 {
                 // not inverse
-                flags = flags.remove (.inverse)!
+                flags = flags.remove (.inverse) ?? empty
             } else if p == 28 {
                 // not invisible
-                flags = flags.remove (.invisible)!
+                flags = flags.remove (.invisible) ?? empty
             } else if p == 29 {
                 // not crossed out
-                flags = flags.remove (.crossedOut)!
+                flags = flags.remove (.crossedOut) ?? empty
             } else if p == 39 {
                 // reset fg
                 fg = (CharData.defaultAttr >> 9) & 0x1ff
@@ -1614,7 +1621,6 @@ public class Terminal {
                 // fg color 256
                 if pars [i + 1] == 2 {
                     
-                    abort ()
                     i += 2
                     fg = matchColor (
                         pars [i] & 0xff,