Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch to Remove ANSI Colour Values Polluting Output #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ nbproject
*.sublime-project
*.sublime-workspace
/bin

SMCKit.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
76 changes: 55 additions & 21 deletions SMCKitTool/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,49 @@ let SMCKitToolVersion = "0.2.0-dev"
let maxTemperatureCelsius = 128.0

//------------------------------------------------------------------------------
// MARK: Enums
// MARK: Color Palette
//------------------------------------------------------------------------------

enum ANSIColor: String {
case Off = "\u{001B}[0;0m"
case Red = "\u{001B}[0;31m"
case Green = "\u{001B}[0;32m"
case Yellow = "\u{001B}[0;33m"
case Blue = "\u{001B}[0;34m"
struct Color {
var rawValue : String;
init() {
self.rawValue = "";
}
init(fromRawValue rv: String) {
self.rawValue = rv;
}
}

class ColorPalette { // Plain/Null Color Pallette
let nullColor = Color();
var Off : Color
var Red : Color
var Green : Color
var Yellow : Color
var Blue : Color

init() {
Off = nullColor
Red = nullColor
Green = nullColor
Yellow = nullColor
Blue = nullColor
}
}

class ANSIColor : ColorPalette { // ANSIColor Palette is a ColorPalette
override init() {
super.init();
Off = Color(fromRawValue: "\u{001B}[0;0m")
Red = Color(fromRawValue:"\u{001B}[0;31m")
Green = Color(fromRawValue:"\u{001B}[0;32m")
Yellow = Color(fromRawValue:"\u{001B}[0;33m")
Blue = Color(fromRawValue:"\u{001B}[0;34m")
}
}

var colorPalette : ColorPalette;

//------------------------------------------------------------------------------
// MARK: CLI
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -110,6 +142,8 @@ do {
exit(EX_USAGE)
}

colorPalette = (CLIColorOption.wasSet) ? ANSIColor() : ColorPalette()

// Give precedence to help flag
if CLIHelpOption.wasSet {
CLI.printUsage()
Expand All @@ -124,24 +158,24 @@ if CLIHelpOption.wasSet {
//------------------------------------------------------------------------------

func warningLevel(value: Double, maxValue: Double) -> (name: String,
color: ANSIColor) {
color: Color) {
let percentage = value / maxValue

switch percentage {
// TODO: Is this safe? Rather, is this the best way to go about this?
case -Double.infinity...0: return ("Cool", ANSIColor.Blue)
case 0...0.45: return ("Nominal", ANSIColor.Green)
case 0.45...0.75: return ("Danger", ANSIColor.Yellow)
default: return ("Crisis", ANSIColor.Red)
case -Double.infinity...0: return ("Cool", colorPalette.Blue)
case 0...0.45: return ("Nominal", colorPalette.Green)
case 0.45...0.75: return ("Danger", colorPalette.Yellow)
default: return ("Crisis", colorPalette.Red)
}
}

func colorBoolOutput(value: Bool) -> String {
let color: ANSIColor
if CLIColorOption.wasSet { color = value ? ANSIColor.Green : ANSIColor.Red }
else { color = ANSIColor.Off }
let color: Color
if CLIColorOption.wasSet { color = value ? colorPalette.Green : colorPalette.Red }
else { color = colorPalette.Off }

return "\(color.rawValue)\(value)\(ANSIColor.Off.rawValue)"
return "\(color.rawValue)\(value)\(colorPalette.Off.rawValue)"
}

func printTemperatureInformation(known: Bool = true) {
Expand Down Expand Up @@ -183,13 +217,13 @@ func printTemperatureInformation(known: Bool = true) {
print("NA")
return
}

let warning = warningLevel(value: temperature, maxValue: maxTemperatureCelsius)
let level = CLIWarnOption.wasSet ? "(\(warning.name))" : ""
let color = CLIColorOption.wasSet ? warning.color : ANSIColor.Off
let color = warning.color;

print("\(color.rawValue)\(temperature)°C \(level)" +
"\(ANSIColor.Off.rawValue)")
"\(colorPalette.Off.rawValue)")
}
}

Expand Down Expand Up @@ -219,9 +253,9 @@ func printFanInformation() {
let warning = warningLevel(value: Double(currentSpeed),
maxValue: Double(fan.maxSpeed))
let level = CLIWarnOption.wasSet ? "(\(warning.name))" : ""
let color = CLIColorOption.wasSet ? warning.color : ANSIColor.Off
let color = warning.color;
print("\tCurrent: \(color.rawValue)\(currentSpeed) RPM \(level)" +
"\(ANSIColor.Off.rawValue)")
"\(colorPalette.Off.rawValue)")
}
}

Expand Down