Skip to content

A Swift library to access features of ANSI terminal.

License

Notifications You must be signed in to change notification settings

escrafford/ANSITerminal

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ANSITerminal

ANSITerminal is an open source Swift library to access ANSI Terminal and (almost) all of its features. It's including text coloring, text styling, cursor and screen handling, and direct keyboard input. ANSITerminal is based on standard ANSI features which is commonly supported on Unix terminals especially such as xterm and VT-100 compatible. This library currently only supports Linux and macOS, simply because Swift isn't officially available on Windows yet.

UPDATE:
Latest version is v.0.0.3 at May, 26th 2019. There are a simple demo and also a simple game made using this library. Have fun! :)

Usage

Using ANSITerminal library is as easy as putting import ANSITerminal in your Swift program then you will be able to use any functions or extensions provided by this library. The main advantage of using this library instead of something like ncurses is it doesn't take over the whole screen. You may use this library to complement your common console program.

Using Swift Package Manager

ANSITerminal only supports library distribution through Swift Package Manager. To use ANSITerminal, simply add a dependency into your project's package.swift file. Here's package.swift example from ansiDemo project.

// swift-tools-version:5.0

import PackageDescription

let package = Package(
    name: "ansiDemo",
    dependencies: [
        .package(url: "https://github.com/pakLebah/ANSITerminal", from: "0.0.3"),
    ],
    targets: [
        .target(
            name: "ansiDemo",
            dependencies: ["ANSITerminal"],
            path: "Sources"),
    ]
)

You just need to add a package dependency pointed to ANSITerminal's GitHub repo and "ANSITerminal" item into target dependency. Then you should be able to put import ANSITerminal in your Swift program and enjoy ANSI terminal features. That's all.

If you find a missing ANSI feature that you need, you could simply call swift package edit ANSITerminal. SPM will create a local copy of ANSITerminal for you, so you could add any missing features by yourself. Everytime you build the project, Swift will refers to the local copy instead of the original source. Once you done with it, don't forget to send a pull request to me. I'll be happy to accept any useful contributions to ANSITerminal from anyone.

Features

Text Coloring

ANSI color is available as property to String type through extension. To color a text, simply follow the text with the color's name. For example, 'text'.blue will produce text (blue text)* on the screen. To set text background color, simply follow the text with the color's name but with on prefix. For example, 'text'.onCyan will produce text (black text over cyan background) on the screen. As coloring is available as property to String, you may combine them. For example, 'text'.blue.onCyan will produce text (blue text over cyan background) on the screen. In case you prefer more expressive property name, you may want to use as prefix for text color. So, instead of 'text'.blue you could use 'text'.asBlue. It's so easy!

But the color name property is only available for the 16 system colors. Most ANSI terminals also support 256 colors. To use 256 colors palette, use foreColor(_:) type extension method to set text color and backColor(_:) to set text background color. You may also want to use colors(_: _:) that combines text color and background color respectively. Those type methods work just like the type properties mentioned before. Since 256 is too many to define correct and consistent name for each of them, you have to use the color index for 256 color palette. For example, 'text'.foreColor(196) will produce text (red text) on the screen. As in 16 colors properties, there are also more expressive method names for 256 colors methods, using with prefix. So, instead of 'text'.foreColor(196) you could also use 'text'.withForeColor(196).

The String extension mechanism sets color only for the text