• Stars
    star
    854
  • Rank 51,431 (Top 2 %)
  • Language
    Swift
  • License
    MIT License
  • Created about 5 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Xterm/VT100 Terminal emulator in Swift

Swift

SwiftTerm

SwiftTerm is a VT100/Xterm terminal emulator library for Swift applications that can be embedded into macOS, iOS applications, text-based, headless applications or other custom scenarios. It has been used in several commercially available SSH clients, including Secure Shellfish, La Terminal and CodeEdit

Check the API Documentation

This repository contains both a terminal emulator engine that is UI agnostic, as well as front-ends for this engine for iOS using UIKit, and macOS using AppKit. A curses-based terminal emulator (to emulate an xterm inside a console application) is available as part of the TermKit library.

Sample Code There are a couple of minimal sample apps for Mac and iOS showing how to use the library inside the TerminalApp directory.

  • The sample Mac app has much of the functionality of MacOS' Terminal.app, but without the configuration UI.
  • The sample iOS application uses an SSH library to connect to a remote system (as there is no native shell on iOS to run), and the sample happens to be hardcoded to my home machine, you can change that in the source code.

Companion App SwiftTermApp builds an actual iOS app that uses this library and is more complete than the testing apps in this module and provides a proper configuration UI.

This is a port of my original XtermSharp, which was itself based on xterm.js. At this point, I consider SwiftTerm to be a more advanced terminal emulator than both of those (modulo Selection/Accessibility) as it handles UTF, Unicode and grapheme clusters better than those and has a more complete coverage of terminal emulation. XtermSharp is generally attempting to keep up.

Features

  • Pretty decent terminal emulation, on or better than XtermSharp and xterm.js (and more comprehensive in many ways)
  • Unicode rendering (including Emoji, and combining characters and emoji)
  • Reusable and pluggable engine allows multiple user interfaces to be built on top of it.
  • Selection engine (with macOS support in the view)
  • Supports colors (ANSI, 256, TrueColor)
  • Supports mouse events
  • Supports terminal resizing operations (controlled by remote host, or locally)
  • Hyperlinks in terminal output
  • AppKit, UIKit front-ends; ncruses front-end provided separately
  • Local process and SSH connection support (some assembly required for the last one)
  • Proper CoreText rendering can munch through the hardened Unicode test suites.
  • Sixel graphics (Use img2sixel to test)
  • iTerm2-style graphic rendering (Use imgcat to test)
  • Fuzzed and abused
  • Seems pretty fast to me

SwiftTerm library

The SwiftTerm library itself contains the source code for both the engine and the front-ends. The front-ends are conditionally compiled based on the target platform.

The engine is in this directory, while code for macOS lives under Mac, and code for iOS, lives under iOS. Given that those two share a lot of common traits, the shared code is under Apple.

Using SwiftTerm

SwiftTerm uses the Swift Package Manager for its build, and you can add the library to your project by using the url for this project or a fork of it.

MacOS NSView

The macOS AppKit NSView implementation TerminalView is a reusable NSView control that can be connected to any source by implementing the TerminalViewDelegate.
I anticipate that a common scenario will be to host a local Unix command, so I have included LocalProcessTerminalView which is an implementation that connects the TerminalView to a Unix pseudo-terminal and runs a command there.

iOS UIView

There is an equivalent UIKit UIView implementation for TerminalView which like its NSView companion is an embeddable and reusable view that can be connected to your application by implementing the same TerminalViewDelegate. Unlike the NSView case running on a Mac, where a common scenario will be to run local commands, given that iOS does not offer access to processes, the most common scenario will be to wire up this terminal to a remote host. And the safest way of connecting to a remote system is with SSH.

Shared Code between MacOS and iOS

The iOS and UIKit code share a lot of the code, that code lives under the Apple directory.

Using SSH

The core library currently does not provide a convenient way to connect to SSH, purely to avoid the additional dependency. But this git module references a module that pulls a precompiled SSH client (Frugghi's SwiftSH), along with a UIKitSsshTerminalView in the iOS sample that that connects the TerminalView for iOS to an SSH connection.

Working on SwiftTerm

If you are using Xcode, there are two toplevel projects, one for Mac and one for iOS in the TerminalApp directory, one called "iOSTerminal.xcodeproj" and one called "MacTerminal.xcodeproj".

This is needed because Xcode does not provide code completion for iOS if you have a Mac project in the project. So I had to split them up. Both projects reference the same SwiftTerm package.

When working with these projects, if you choose the terminal application it will run this one. To run the test suite, select the 'SwiftTerm' target instead, and you can use 'SwiftTermFuzz' to run the fuzzer.

You can use swift build to build the package, and swift test to run the test suite - but be warned that the test suite expects the directory esctest to be checked out to run. You can see how I run these on GitHub actions in the file .github/workflows/swift.yml if you want to do this locally.

If using Xcode, you can select the "SwiftTerm" project, and then use Command-U to run the test suite.

Pending Work

GitHub issues has a list of desired features and enhancements

Long Term Plans

In the longer term, I want to also add a tvOS UIView, a SwiftGtk front-end for Linux.

Screenshots

24 Bit Color

24 bit color

Midnight Commander

Screen Shot 2020-04-12 at 12 17 49 AM

Solid UTF-8 support, excellent rendering: Screen Shot 2020-04-22 at 11 25 30 PM

Screen Shot 2020-04-22 at 11 25 24 PM

Supports hyperlinks emitted by modern apps:

image

iOS support:

image

Sixel support:

image

image

Resources

Additional and useful documents:

Test suites:

  • VTTest - old, but still good
  • EscTest - fantastic: George Nachman, the author of iTerm, created this test suite, and it became a FreeDesktop standard. Since then, Thomas E. Dickey, the xterm maintainer and maintainer of many text apps has contributed to this effort.

Authors

  • Thanks go to the xterm.js developers that originally wrote a terminal emulator that was licensed under a license that allowed for maximum reuse.
  • Marcin Krzyzanowski who masterfully improved and curated the rendering engine on AppKit/CoreText to be the glorious renderer that it is today - and for his contributions to the rendering engine
  • Greg Munn that did a lot of work in XtermSharp to support the needs of Visual Studio for Mac
  • Anders Borum has contributed reliability fixes, the sixel parser and changes required to put SwiftTerm to use in production.
  • Miguel de Icaza -me- who have been looking for an excuse to write some Swift code.

More Repositories

1

TensorFlowSharp

TensorFlow API for .NET languages
C#
3,118
star
2

SwiftGodot

New Godot bindings for Swift
Swift
758
star
3

mono-wasm

JavaScript
523
star
4

TermKit

Terminal Kit - Console UI toolkit for Swift applications
Swift
444
star
5

MonoTouch.Dialog

Tools to simplify creating dialogs with the user using MonoTouch
C#
432
star
6

WasmerSharp

.NET Bindings for the Wasmer Runtime
C#
318
star
7

SwiftTermApp

Swift
284
star
8

PlaticaBot

MacOS, iOS and watchOS ChatGPT client using your own OpenAI key
Swift
185
star
9

TweetStation

MonoTouch based Twitter client
C#
180
star
10

XtermSharp

XTerm emulator as a .NET library
C#
156
star
11

redis-sharp

A C#/.NET binding for the Redis server.
C#
152
star
12

SwiftGodotKit

Embed Godot into Swift apps
C
146
star
13

SkiaKit

Swift Bindings to the Skia 2D graphics Library
Swift
130
star
14

TextBufferKit

Swift TextBuffer
Swift
103
star
15

CovidGraphs

Covid Graphs app
Swift
93
star
16

SwiftChatGPT

Simple ChatGPT API
Swift
72
star
17

GodotSwift

Swift bindings for the Godot Game Engine
C
56
star
18

mc

C# based curses file manager
C#
45
star
19

FuchsiaSharp

Bindings to Fuchsia
C#
36
star
20

OpenFlowSharp

Sample CoverFlow implementation for MonoTouch, based on OpenFlow
C#
33
star
21

muget

Command line front end to NuGet that is not overengineered
C#
31
star
22

monotouch-libs

Various MonoTouch bindings and libraries
C#
25
star
23

TldExtract

.NET Library to extracts the root domain, subdomain name, and top level domain from a host name using the Public Suffix List
C#
22
star
24

RealityActions

RealityActions
Swift
18
star
25

SwiftGodotBinary

SwiftGodot - binaries for ease of consumption
Swift
17
star
26

mono-wasm-libc

C
15
star
27

TurboSwift

Swift
14
star
28

mono-wasm-mono

C#
14
star
29

FluidInterfaces

Port of Nathan Gitter's Fluid Interfaces code
C#
14
star
30

MaterialSharp

Material design components for iOS applications written in C#
C#
13
star
31

NetCatNetwork

NetCat implementation using the new Apple Network framework, C# port of the C sample
C#
11
star
32

CovidExtractor

My Covid Extractor
Swift
9
star
33

SwiftGodotDocs

9
star
34

CocoaDriver

Xamarin.Mac-based Cocoa driver for Mono's System.Windows.Forms
C#
8
star
35

Darwin.CopyFile

Bindings for Darwin's CopyFile API
C#
8
star
36

MonoTouch.Bindings

Some bindings for third party libraries
6
star
37

migueldeicaza

Repository for my README
6
star
38

gtk

Fork of Gtk branch 2-24 with all Xamarin patches from github.com/mono/bockbuild applied
C
5
star
39

libgodot

Godot modified to be embeddable as a library, for use in SwiftGodotKit
C++
5
star
40

GodotSwiftLink

Binding module, temporary, while I move it to Godot.
C
4
star
41

PhotoPanner

C# version of Facebooks Photo Panner
C#
4
star
42

Mac-SceneKit-Globe-Test

Sample F# Mac app showing SceneKit in action.
F#
4
star
43

Mono.VFS

C# based Virtual File System API.
4
star
44

SimpleCollage

Simple picture collage with SkiaSharp
C#
4
star
45

til

4
star
46

KeyboardButton

Simple keyboard button in SwiftUI eventually for use in the Terminal Emulator
Swift
4
star
47

MapWrapper

SwiftUI wrapper around MapKit
Swift
4
star
48

SwiftApiExtractIssue

Shows a bug with swift-api-extract
Swift
3
star
49

RosettaMaker

Creates the Rosetta HTML page that shows the C#/Objective-C/C mappings
C#
3
star
50

KitCore.NET

.NET bindings to KitCore - the ultimate core application framework
3
star
51

NailedIt

My Google Glasses app to "Capture Moments" and "Nailed it"
Java
3
star
52

SplashView

C# Port of SplashView https://github.com/callumboddy/CBZSplashView
C#
3
star
53

FileProvider-ios-crashing

Swift
2
star
54

monotouch-samples

MonoTouch Sample Programs
2
star
55

SwiftSH.binaries

Binary for the SwiftSH framework
C
2
star
56

SkiaKit.binaries

Precompiled binaries for various platforms of the runtime used by SkiaKit (SkiaSharp)
2
star
57

CovidExtractorPM

SwiftPM version of CovidExtractor
Swift
2
star
58

paint-mono

Automatically exported from code.google.com/p/paint-mono
C#
2
star
59

SkiaKitPayloads

Binary payloads directory for the SkiaKit library
Shell
1
star
60

SwiftOnnxMnist

Mnist sample using OnnxRuntime in Swift
C++
1
star
61

iptoloc

IP to Location Tools
C#
1
star
62

TreemapWeb

C#
1
star
63

mlist

Gtk# Mlist widget - managed implementation of a list view (2005 era)
C#
1
star
64

mono-soc-2007

Automatically exported from code.google.com/p/mono-soc-2007
C#
1
star
65

blog-monomac

MonoMac Blog
1
star
66

SwiftDocCPlusPlusBug

Sample of how Swift Docc fails if you use the new Swift C++ interop
Swift
1
star
67

GodotSnippetEditor

A tool to edit Godot documentation code snippets
Swift
1
star
68

SwiftTermDocs

Placeholder to publish the API docs for SwiftTerm
1
star
69

esctest

Mirror from https://gitlab.freedesktop.org/terminal-wg/esctest.git
Python
1
star
70

CoreKitSharp

Bindings to the Ultimate Framework Platform
1
star