Skip to content

Commit

Permalink
more premade colors
Browse files Browse the repository at this point in the history
  • Loading branch information
zaneenders committed Jun 25, 2024
1 parent 210ae7e commit 07018d1
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 21 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# chroma-shell
# Chroma Shell

Add Chroma Shell to your swift package to give it a try
```swift
.package(
url: "https://github.com/zaneenders/chroma-shell.git",
revision: "main"),
```

.product(name: "Chroma", package: "chroma-shell"),
```

## Features
[x] Basic ascii color functions
2 changes: 1 addition & 1 deletion Sources/Color/Chroma.docc/Chroma.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# ``Chroma``

A package to add more ``Chroma`` to your Swift terminal life.
A package to add more Chroma to your Swift terminal life.


48 changes: 48 additions & 0 deletions Sources/Color/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,54 @@ public func foreground(_ color: Color, _ str: String) -> AnsiString {
return colorString + str + AnsiCode.reset.rawValue
}

public func background(_ color: Color, _ str: String) -> AnsiString {
// https://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
let colorString: String
switch color {
case let .basic(basic):
switch basic {
case .black:
colorString = "\u{001b}[40m"
case .red:
colorString = "\u{001b}[41m"
case .green:
colorString = "\u{001b}[42m"
case .yellow:
colorString = "\u{001b}[43m"
case .blue:
colorString = "\u{001b}[44m"
case .magenta:
colorString = "\u{001b}[45m"
case .cyan:
colorString = "\u{001b}[46m"
case .white:
colorString = "\u{001b}[47m"
}
case let .bright(bright):
switch bright {
case .black:
colorString = "\u{001b}[40;1m"
case .red:
colorString = "\u{001b}[41;1m"
case .green:
colorString = "\u{001b}[42;1m"
case .yellow:
colorString = "\u{001b}[43;1m"
case .blue:
colorString = "\u{001b}[44;1m"
case .magenta:
colorString = "\u{001b}[45;1m"
case .cyan:
colorString = "\u{001b}[46;1m"
case .white:
colorString = "\u{001b}[47;1m"
}
case let .int(i):
colorString = "\u{001b}[48;5;\(i % 256)m"
}
return colorString + str + AnsiCode.reset.rawValue
}

/// Ansi Codes
/// This is only used as a output translation.
/// [](https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797)
Expand Down
73 changes: 55 additions & 18 deletions Sources/Color/PreMade.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,72 @@
public func yellow(_ str: String) -> String {
foreground(.int(226), str)
public enum Position {
case foreground
case background
}

public func green(_ str: String) -> String {
foreground(.int(40), str)
public func yellow(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(226), position)
}

public func blue(_ str: String) -> String {
foreground(.int(27), str)
public func green(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(40), position)
}

public func pink(_ str: String) -> String {
foreground(.int(201), str)
public func blue(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(27), position)
}

public func red(_ str: String) -> String {
foreground(.int(196), str)
public func pink(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(201), position)
}

public func orange(_ str: String) -> String {
foreground(.int(202), str)
public func red(_ str: String, _ position: Position = .foreground) -> AnsiString
{
wrap(str, .int(196), position)
}

public func purple(_ str: String) -> String {
foreground(.int(129), str)
public func orange(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(202), position)
}

public func white(_ str: String) -> String {
foreground(.int(15), str)
public func purple(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(129), position)
}

public func teal(_ str: String) -> String {
foreground(.int(14), str)
public func white(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(231), position)
}

public func black(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(232), position)
}

public func teal(_ str: String, _ position: Position = .foreground)
-> AnsiString
{
wrap(str, .int(14), position)
}

func wrap(_ str: String, _ color: Color, _ position: Position) -> AnsiString {
switch position {
case .foreground:
return foreground(color, str)
case .background:
return background(color, str)
}
}

0 comments on commit 07018d1

Please sign in to comment.