Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Masashi Aso committed Oct 25, 2020
0 parents commit 91e92e2
Show file tree
Hide file tree
Showing 17 changed files with 937 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
.swiftpm
29 changes: 29 additions & 0 deletions Demo/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import AnsiTerm
import Foundation

// like object oriented programming
let style = Style(foreground: .cyan, bold: true, blink: true)
let cyanText = ANSIString("cyan, bold, blink", style: style)
print(cyanText)

let first = "first".foreground(.blue)
let second = "second".style(Style(background: .fixed(52)))
let third = "third".style(Style(foreground: .red, bold: true))
let strings: ANSIStrings = [first, second, third]
print(strings)

sleep(2)
Cursor.move(up: 2)
Cursor.move(forward: 6)

// like functional programming
let over = "over,"
.foreground(.black)
.background(.purple)
.blink()
print(over)

sleep(2)
Cursor.move(forward: 10)
Console.clearLineBeforeCursor()
print() // print '\n'
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2020 Masashi Aso

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

32 changes: 32 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "swift-ansi-term",
products: [
.library(
name: "AnsiTerm",
targets: ["AnsiTerm"]),
.executable(
name: "demo",
targets: ["Demo"])
],
dependencies: [
],
targets: [
.target(
name: "AnsiTerm",
dependencies: []),

.target(
name: "Demo",
dependencies: ["AnsiTerm"],
path: "Demo"),

.testTarget(
name: "AnsiTermTests",
dependencies: ["AnsiTerm"]),
]
)
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Swift Ansi Term

## Usage

### Color asn Style

#### Like Object Oriented Programming

```swift
import AnsiTerm

let style = Style(foreground: .red, bold: true, blink: true)
let hello = "Hello, AnsiTerm!"
print(hello)
```

#### Like Functional Programming

```swift
import AnsiTerm

let yellowBoldText = "You can use modifier"
.foreground(.yellow)
.bold()
print(yellowBoldText)
```

### Cursor and Console

```swift
import AnsiTerm

print("Hello", terminator: "")
Cursor.move(backward: 5)
prnit("World")
Console.clearLine()
```

## Example

`Demo/main.swift` is minimam example. I'll add more example.

## Install

To use the `AnsiTerm` in a SwiftPM project, add the following line to the dependencies in your `Package.swift` file:

```swift
.package(url: "https://github.com/masasam-shi/swift-ansi-term", from: "0.0.1"),
```

and include "AnsyTirm" as a dependency for your executable target:

```swift
.product(name: "AnsiTerm", package: "swift-ansi-term"),
```
122 changes: 122 additions & 0 deletions Sources/AnsiTerm/ANSIString/ANSIString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//
// ANSIString.swift
//
//
// Created by Masashi Aso on 2020/10/24.
//

public struct ANSIString {
public var style: Style
public var string: String

public init(_ string: String, style: Style) {
self.string = string
self.style = style
}

public init(_ string: String, color: Color) {
self.init(string, style: Style(foreground: color))
}
}

extension ANSIString: CustomStringConvertible {
public var description: String {
style.prefix() + string + style.suffix()
}
}

// MARK: - Modifier
extension ANSIString: StyleModifier {
public typealias Modified = ANSIString

@inline(__always) @inlinable
public func bold(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.bold(isOn)
return new
}

@inline(__always) @inlinable
public func dim(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.dim(isOn)
return new
}

@inline(__always) @inlinable
public func italic(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.italic(isOn)
return new
}

@inline(__always) @inlinable
public func underline(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.underline(isOn)
return new
}

@inline(__always) @inlinable
public func blink(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.blink(isOn)
return new
}

@inline(__always) @inlinable
public func reverse(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.reverse(isOn)
return new
}

@inline(__always) @inlinable
public func hide(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.hide(isOn)
return new
}

@inline(__always) @inlinable
public func strikethrough(_ isOn: Bool = true) -> ANSIString {
var new = self
new.style = self.style.strikethrough(isOn)
return new
}

@inline(__always) @inlinable
public func foreground(_ color: Color?) -> ANSIString {
var new = self
new.style = self.style.foreground(color)
return new
}

@inline(__always) @inlinable
public func background(_ color: Color?) -> ANSIString {
var new = self
new.style = self.style.background(color)
return new
}
}

// MARK: StringProtocol
extension ANSIString: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
self = .init(value, style: Style())
}
}

// TODO: improve performance
// make `ANSICharacter` and `ANSISubstring`,
// implement ANSIString for StringProtocol.
//extension ANSIString: Sequence {
// public typealias Element = ANSIString
// public typealias Iterator = IndexingIterator<Array<ANSIString>>
//
// public func makeIterator() -> Iterator {
// string
// .map { ANSIString(String($0), style: style) }
// .makeIterator()
// }
//}
74 changes: 74 additions & 0 deletions Sources/AnsiTerm/ANSIString/ANSIStrings.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// ANSIStrings.swift
//
//
// Created by Masashi Aso on 2020/10/24.
//

public struct ANSIStrings {
private var strings: [ANSIString]

public init(_ ansiStrings: [ANSIString]) {
self.strings = ansiStrings
}

public init(_ ansiStrings: ANSIString...) {
self.strings = ansiStrings
}
}

extension ANSIStrings: CustomStringConvertible {
public var description: String {
if strings.count == 0 { return "" }
if strings.count == 1 { return strings.first!.description }

let prefix = strings.first!.style.prefix()
let zipped = zip(strings[..<strings.endIndex], strings[strings.startIndex.advanced(by: 1)...])
let inner = zipped.flatMap { $0.string + $0.style.infix(next: $1.style) }
let postfix = strings.last!.string + strings.last!.style.suffix()
return prefix + inner + postfix
}
}

// MARK: - Collection
extension ANSIStrings: Sequence {
public typealias Element = ANSIString
public typealias Iterator = IndexingIterator<Array<ANSIString>>

public func makeIterator() -> Iterator {
strings.makeIterator()
}
}

extension ANSIStrings: Collection {
public typealias Index = Int

public var count: Int { strings.count }

public var startIndex: Int { strings.startIndex }
public var endIndex: Int { strings.endIndex }

public func index(after i: Int) -> Int {
strings.index(after: i)
}

public subscript(position: Int) -> ANSIString {
strings[position]
}
}

extension ANSIStrings: BidirectionalCollection {
public typealias SubSequence = ANSIStrings

public func index(before i: Int) -> Int {
strings.index(before: i)
}
}

extension ANSIStrings: RandomAccessCollection {}

extension ANSIStrings: ExpressibleByArrayLiteral {
public init(arrayLiteral elements: Element...) {
self = .init(elements)
}
}
Loading

0 comments on commit 91e92e2

Please sign in to comment.