Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancelotbronner committed May 11, 2023
0 parents commit 60c5769
Show file tree
Hide file tree
Showing 25 changed files with 1,476 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
.swiftpm
xcuserdata/
Resources
20 changes: 20 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// swift-tools-version:5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "Termios",
products: [
.library(
name: "Termios",
targets: ["Termios"]
),
],
targets: [
.target(name: "CTermios"),
.target(name: "Termios", dependencies: ["CTermios"]),

.testTarget(name: "TermiosTests", dependencies: ["Termios"]),
]
)
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Terminal

This package aims to aid in the making of Command-Line Applications by providing an API to wrap
ANSI commands.

The package contains a few modules: `Termios`, `ControlSequence`, `Prompt` and `Terminal`. The last one re-exports all previous ones.

*Any help, comments or suggestions would be welcome!*

## Termios

A swift termios wrapper, with documentation from the man pages.

## ControlSequence

Wraps ANSI sequences, string styling shortcuts, terminal query sequences and parsing, etc.

## Prompt

Alternative to readline, provides a plugin architecture to customize your prompt

## Terminal

Provides convenience APIs for terminal-related things
18 changes: 18 additions & 0 deletions Sources/CTermios/ctermios.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// ctermios.c
//
//
// Created by Christophe Bronner on 2023-05-10.
//

#include <ctermios.h>

const cc_t CTERMIOS_VDISABLE = _POSIX_VDISABLE;

cc_t ctermios_getcc(const struct termios *const termios, int flag) {
return termios->c_cc[flag];
}

cc_t ctermios_setcc(struct termios *termios, int flag, cc_t value) {
termios->c_cc[flag] = value;
}
13 changes: 13 additions & 0 deletions Sources/CTermios/include/ctermios.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// File.swift
//
//
// Created by Christophe Bronner on 2023-05-10.
//

#include <termios.h>

const cc_t CTERMIOS_VDISABLE;

cc_t ctermios_getcc(const struct termios *const termios, int flag);
cc_t ctermios_setcc(struct termios *termios, int flag, cc_t value);
80 changes: 80 additions & 0 deletions Sources/Termios/Control+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//
// File.swift
//
//
// Created by Christophe Bronner on 2023-05-11.
//

import Darwin.POSIX.termios

extension Termios {

/// Specifies the byte size in bits for both transmission and reception.
///
/// This size does not include the parity bit, if any. If ``isTwoStopBitsUsed`` is set, two stop bits are used, otherwise one stop bit.
/// For example, at 110 baud, two stop bits are normally used.
@inlinable public var size: CharacterSize {
get { CharacterSize(rawValue: rawValue.csize) }
set { rawValue.csize = newValue.rawValue }
}

/// Two stop bits are used instead of one stop bit.
///
/// For example, at 110 baud, two stop bits are normally used.
@inlinable public var isTwoStopBitsUsed: Bool {
get { rawValue.cstopb }
set { rawValue.cstopb = newValue }
}

/// Wether parity generation and detection are enabled
///
/// If parity is enabled, ``parity-swift.property``controls wether even or odd parity is used.
@inlinable public var isParityEnabled: Bool {
get { rawValue.parenb }
set { rawValue.parenb = newValue }
}

/// Specifies wether parity is even or odd
@inlinable public var parity: ParityChecking {
get { ParityChecking(booleanLiteral: rawValue.parodd) }
set { rawValue.parodd = newValue.rawValue }
}

/// Wether to receive characters.
///
/// Not all hardware supports this bit. In fact, this flag is pretty silly and if it were not part of the termios specification it would be omitted.
@inlinable public var isReadEnabled: Bool {
get { rawValue.cread }
set { rawValue.cread = newValue }
}

/// Wether to hang up on the last close
///
/// The modem control lines for the port are lowered when the last process with the port open closes the port or the process terminates.
/// The modem connection is broken.
@inlinable public var willHangUpOnLastClose: Bool {
get { rawValue.hupcl }
set { rawValue.hupcl = newValue }
}

/// Wether to monitor or ignore modem status lines.
///
/// When enabled the connection does not depend on the state of the modem status lines.
/// When disabled the modem status lines are monitored.
@inlinable public var isModemStatusLineMonitoringEnabled: Bool {
get { rawValue.clocal }
set { rawValue.clocal = newValue }
}

/// Wether the output flow control is controlled by the state of Carrier Detect.
@inlinable public var isFlowControlledByCarrier: Bool {
get { rawValue.mdmbuf }
set { rawValue.mdmbuf = newValue }
}

/// The total number of bits used per transmission and reception
@inlinable public var bitsPerCharacter: Int {
size.bits + (isTwoStopBitsUsed ? 2 : 1) + (isParityEnabled ? 1 : 0)
}

}
72 changes: 72 additions & 0 deletions Sources/Termios/Control.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
//
// File.swift
//
//
// Created by Christophe Bronner on 2021-12-26.
//

import Darwin.POSIX.termios

extension termios {

/// See ``Termios/Termios/size``
@inlinable public var csize: Int32 {
get { get(mask: CSIZE, from: c_cflag) }
set { set(mask: CSIZE, to: newValue, in: &c_cflag) }
}

/// See ``isTwoStopBitsUsed``
@inlinable public var cstopb: Bool {
get { get(flag: CSTOPB, from: c_cflag) }
set { set(flag: CSTOPB, to: newValue, in: &c_cflag) }
}

/// See ``isParityEnabled``
@inlinable public var parenb: Bool {
get { get(flag: PARENB, from: c_cflag) }
set { set(flag: PARENB, to: newValue, in: &c_cflag) }
}

/// See ``parity-swift.property``
@inlinable public var parodd: Bool {
get { get(flag: PARODD, from: c_cflag) }
set { set(flag: PARODD, to: newValue, in: &c_cflag) }
}

/// See ``isReadEnabled``
@inlinable public var cread: Bool {
get { get(flag: CREAD, from: c_cflag) }
set { set(flag: CREAD, to: newValue, in: &c_cflag) }
}

/// See ``isHangUpOnLastCloseEnabled``
@inlinable public var hupcl: Bool {
get { get(flag: HUPCL, from: c_cflag) }
set { set(flag: HUPCL, to: newValue, in: &c_cflag) }
}

/// See ``isModemStatusLineMonitoringEnabled``
@inlinable public var clocal: Bool {
get { get(flag: CLOCAL, from: c_cflag) }
set { set(flag: CLOCAL, to: newValue, in: &c_cflag) }
}

/// Currently unused, same as ``crtscts``
@inlinable public var ccts_oflow: Bool {
get { get(flag: CCTS_OFLOW, from: c_cflag) }
set { set(flag: CCTS_OFLOW, to: newValue, in: &c_cflag) }
}

/// Currently unused, same as ``ccts_oflow``
@inlinable public var crtscts: Bool {
get { get(flag: CRTSCTS, from: c_cflag) }
set { set(flag: CRTSCTS, to: newValue, in: &c_cflag) }
}

/// See ``isFlowControlledByCarrier``
@inlinable public var mdmbuf: Bool {
get { get(flag: MDMBUF, from: c_cflag) }
set { set(flag: MDMBUF, to: newValue, in: &c_cflag) }
}

}
Loading

0 comments on commit 60c5769

Please sign in to comment.