How dnpp73/terminal works
Main Components
• Terminal Emulator Library: This is the core of the project. Split it into two conceptual parts:
– Public API: Located in Sources/Terminal/Public (e.g., classes like TerminalView, TerminalClearEvent, TerminalSize, etc.) that clients of the library use.
– Internal Mechanics: Found under Sources/Terminal/Internal (e.g., Character+Control, HtermWebView, debugLog, UIView+EdgeConstraints, etc.). This part handles the internal logic like processing keystrokes, managing web view interactions, and bridging to hterm resources.
• Resources: The Resources subdirectory within Sources/Terminal includes essential assets (hterm.css, hterm.html, hterm_all.js, and hterm_bridge.js) that enable the terminal emulation in the WKWebView.
• Example Application: The Example directory contains an Xcode project which provides sample view controllers (e.g., TerminalViewController, SSHTerminalViewController) and additional supporting UI components (e.g., InputAccessoryView and KeyboardNotification) that demonstrate how to use the Terminal library.
• Build & Configuration Scripts: Files like the swiftlint.yml, Cartfile, and build scripts (bin/build_hterm.sh, for instance) show how to maintain coding standards and automate build tasks.
Out the Relationships and Interactions
• The Example App serves as the front end to showcase the Terminal library. In it, the user interacts with the Terminal emulator via UIKit components; these interact with the TerminalViewController.
• TerminalViewController (and related controllers like SSHTerminalViewController) instantiate and use the TerminalView (provided by the library) to display terminal output.
• The TerminalView internally uses a WKWebView to load the hterm-based interface (using assets from the Resources folder). There is a JavaScript bridge (hterm_bridge.js) to communicate between Swift/Objective-C code and the web view’s rendering engine.
• Additional utility modules (such as InputAccessoryView, KeyboardNotification, and Util components) support the handling of user input (hardware keyboard, Interface Builder UI) and event processing.
• Dependencies and integrations (like Carthage and Swift Package Manager compatibility) are indicated in the README, and these play a key role during the build and dependency resolution phases.
Architectural Patterns and Design Principles
• Separation of Concerns: The project divides responsibilities between a reusable Terminal library and a demonstration Example app, allowing for clear separation between core terminal functionality and sample application UI concerns.
• Modularity: Using distinct directories for Public and Internal code in Sources/Terminal makes it easier to manage the API boundary, while utility functions and view controllers are separated into their own directories.
• Bridge Pattern: The integration of WKWebView with hterm.js via JavaScript bridging demonstrates a clear separation of platform-specific UI (iOS) and the cross-platform terminal emulation logic.
• MVC (or similar iOS design philosophy): The controllers in the Example app (ViewControllers) manage the user interface along with the terminal logic from the library.