GithubHelp home page GithubHelp logo

pmacro / swiftcursesterm Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rderik/swiftcursesterm

0.0 0.0 0.0 10 KB

A Swift library for building text bases interface for your command-line tools - based on C ncurses

C 0.80% Swift 99.20%

swiftcursesterm's Introduction

SwiftCursesTerm

You can use the SwiftcursesTerm library to create text based interfaces for your command-line tools. The library uses C ncurses(3) library. Currently the library is a work in progress, so the API might change to prived a more Swifty library.

If you are interested in seeing a full implementation, the following post shows how to build a text-based clock using this library:

https://rderik.com/blog/building-a-text-based-application-using-swift-and-ncurses/

And the code for that application can be found in the following GitHub repository:

https://github.com/rderik/clock

Using it on your command-line tools

You need to add it as a dependencty to your package manifesto Package.swift. For example:

// swift-tools-version:5.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "clock",
    dependencies: [
        // Dependencies declare other packages that this package depends on.
        // .package(url: /* package url */, from: "1.0.0"),
        .package(name: "SwiftCursesTerm", url: "https://github.com/rderik/SwiftCursesTerm.git", from: "0.1.1"),
    ],
    targets: [
        // Targets are the basic building blocks of a package. A target can define a module or a test suite.
        // Targets can depend on other targets in this package, and on products in packages which this package depends on.
        .target(
            name: "clock",
            dependencies: ["SwiftCursesTerm"]),
        .testTarget(
            name: "clockTests",
            dependencies: ["clock"]),
    ]
)

Check the latest release version at https://github.com/rderik/SwiftCursesTerm/releases/latest.

Now you can import SwiftCursesTerm and use all of its functionality.

#Example usage

Text format:

import Foundation
import SwiftCursesTerm

var term = SwiftCursesTerm()
term.addStr(content: "Hello, world!", refresh: true)
let green = term.defineColorPair(foreground: CursesColor.white, background: CursesColor.green)
term.setAttributes([TextAttribute.bold, TextAttribute.underline], colorPair: green)
term.addStr(content: "Hello, in Green!", refresh: true)
getch()
term.shutdown()
exit(EXIT_SUCCESS)

The SwiftCursesTerm object frees memory when it is deinitilized, but if you want to make sure it frees the memory call the function shutdown() and it'll close the ncurses session and free any window you created.

Windows

import Foundation
import SwiftCursesTerm

var term = SwiftCursesTerm()
term.refresh()
var win1 = term.newWindow(height: 20, width: 20, line: 0, column: 0)
var win2 = term.newWindow(height: 20, width: 20, line: 0, column: 21)
term.addStr(window: win1, content: "Hello, world!", refresh: true)
let green = term.defineColorPair(foreground: CursesColor.white, background: CursesColor.green)
term.setWindowColorPair(window: win2, colorPair: green)
term.setAttributes(window: win2, [TextAttribute.dim, TextAttribute.underline], colorPair: green)
term.addStrTo(window: win2, content: "Hello, in Green!", line: 10, column: 0, refresh: true)
getch()
term.shutdown()
exit(EXIT_SUCCESS)

Notice that you can call addStr to add a String on the current cursor position or call addStrTo to move to a position and then add the string.

I encourage you to read the SwiftCursesTerm.swift file to view all the available options.

Contributing

If you want to contribute:

  • Fork the project
  • Create a branch to hold your changes
  • Create a pull request

Author

Derik Ramirez - https://rderik.com

swiftcursesterm's People

Contributors

rderik avatar sriske2 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.