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

Swift 3.0 Migration #37

Merged
merged 2 commits into from
Oct 5, 2017
Merged
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
3 changes: 3 additions & 0 deletions dshb.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
TargetAttributes = {
4C8727FC19C7B41C002D093F = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 0810;
};
};
};
Expand Down Expand Up @@ -312,6 +313,7 @@
isa = XCBuildConfiguration;
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
};
name = Debug;
};
Expand All @@ -320,6 +322,7 @@
buildSettings = {
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
SWIFT_VERSION = 3.0;
};
name = Release;
};
Expand Down
16 changes: 8 additions & 8 deletions dshb/Widget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import Darwin.ncurses

// Color pair refs
enum WidgetUIColor: Int32 {
case Background = 1
case Title = 2
case WarningLevelCool = 3
case WarningLevelNominal = 4
case WarningLevelDanger = 5
case WarningLevelCrisis = 6
case background = 1
case title = 2
case warningLevelCool = 3
case warningLevelNominal = 4
case warningLevelDanger = 5
case warningLevelCrisis = 6
}

/// Number of pixels between widgets
Expand All @@ -57,12 +57,12 @@ protocol WidgetType {
init(window: Window)

mutating func draw()
mutating func resize(window: Window) -> Int32
mutating func resize(_ window: Window) -> Int32
}

extension WidgetType {

mutating func resize(window: Window) -> Int32 {
mutating func resize(_ window: Window) -> Int32 {
title.resize(window)

var windowVar = window
Expand Down
4 changes: 2 additions & 2 deletions dshb/WidgetBattery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ struct WidgetBattery: WidgetType {
stats[0].Danger.range = 0..<0
stats[0].Crisis.range = 0..<0.2

stats[1].Nominal.color = WidgetUIColor.WarningLevelCrisis
stats[1].Crisis.color = WidgetUIColor.WarningLevelNominal
stats[1].Nominal.color = WidgetUIColor.warningLevelCrisis
stats[1].Crisis.color = WidgetUIColor.warningLevelNominal
}

mutating func draw() {
Expand Down
6 changes: 3 additions & 3 deletions dshb/WidgetCPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct WidgetCPU: WidgetType {
var title: WidgetUITitle
var stats = [WidgetUIStat]()

private static var system = System()
fileprivate static var system = System()

init(window: Window = Window()) {
title = WidgetUITitle(name: "CPU", window: window)
Expand All @@ -41,8 +41,8 @@ struct WidgetCPU: WidgetType {
max: 100.0))
}

stats[2].Nominal.color = WidgetUIColor.WarningLevelCrisis
stats[2].Crisis.color = WidgetUIColor.WarningLevelNominal
stats[2].Nominal.color = WidgetUIColor.warningLevelCrisis
stats[2].Crisis.color = WidgetUIColor.warningLevelNominal
}

mutating func draw() {
Expand Down
6 changes: 3 additions & 3 deletions dshb/WidgetMemory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ struct WidgetMemory: WidgetType {
var title: WidgetUITitle
var stats = [WidgetUIStat]()

private static let maxValueGB = System.physicalMemory(.Gigabyte)
private static let maxValueMB = System.physicalMemory(.Megabyte)
fileprivate static let maxValueGB = System.physicalMemory(.gigabyte)
fileprivate static let maxValueMB = System.physicalMemory(.megabyte)

init(window: Window = Window()) {
title = WidgetUITitle(name: name, window: window)
Expand All @@ -60,7 +60,7 @@ struct WidgetMemory: WidgetType {
unitCheck(values.compressed, index: 4)
}

private mutating func unitCheck(val: Double, index: Int) {
fileprivate mutating func unitCheck(_ val: Double, index: Int) {
if val < 1.0 {
stats[index].unit = .Megabyte
let value = val * 1000.0
Expand Down
32 changes: 16 additions & 16 deletions dshb/WidgetProcess.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ struct WidgetProcess: WidgetType {
mutating func draw() {
var list = processList()

list.sortInPlace { $0.kp_proc.p_pid > $1.kp_proc.p_pid }
list.sort { $0.kp_proc.p_pid > $1.kp_proc.p_pid }

for index in 0..<Int(LINES) - Int(title.window.point.y) - 1 {
if index >= list.count { break }

var kinfo = list[index]
let command = withUnsafePointer(&kinfo.kp_proc.p_comm) {
String.fromCString(UnsafePointer($0))!
let command = withUnsafePointer(to: &kinfo.kp_proc.p_comm) {
String(cString: UnsafeRawPointer($0).assumingMemoryBound(to: CChar.self))
}

let username: String
let uid = kinfo.kp_eproc.e_ucred.cr_uid

if let index = usernames.indexForKey(uid) {
if let index = usernames.index(forKey: uid) {
username = usernames[index].1
}
else { username = getUsername(uid) }
Expand Down Expand Up @@ -87,14 +87,14 @@ struct WidgetUIProcess {
addstr(name)
}

mutating func resize(window: Window) {
mutating func resize(_ window: Window) {
self.window = window
draw()
}
}


private func processString(tokens: [String], length: Int) -> String {
private func processString(_ tokens: [String], length: Int) -> String {
let pidSpace = 6
let userSpace = 16

Expand All @@ -104,8 +104,8 @@ private func processString(tokens: [String], length: Int) -> String {
var pidSpaceString = String()
var userSpaceString = String()

for _ in 0..<pidSpace - pidCount { pidSpaceString.append(UnicodeScalar(" ")) }
for _ in 0..<userSpace - userCount { userSpaceString.append(UnicodeScalar(" ")) }
for _ in 0..<pidSpace - pidCount { pidSpaceString.append(" ") }
for _ in 0..<userSpace - userCount { userSpaceString.append(" ") }


return pidSpaceString + tokens[0] + " " + tokens[1] + userSpaceString + tokens[2]
Expand All @@ -121,35 +121,35 @@ private func processList() -> [kinfo_proc] {
assert(result == KERN_SUCCESS)

// Get process list
let procCount = size / strideof(kinfo_proc)
var procList = [kinfo_proc](count: procCount, repeatedValue: kinfo_proc())
let procCount = size / MemoryLayout<kinfo_proc>.stride
var procList = [kinfo_proc](repeating: kinfo_proc(), count: procCount)
result = sysctl(&mib, u_int(mib.count), &procList, &size, nil, 0)
assert(result == KERN_SUCCESS)

return procList
}


private func getUsername(uid: uid_t) -> String {
private func getUsername(_ uid: uid_t) -> String {
let username: String
var userInfo = passwd()
var result = UnsafeMutablePointer<passwd>.alloc(1)
var result: UnsafeMutablePointer<passwd>? = UnsafeMutablePointer<passwd>.allocate(capacity: 1)

// TODO: Can we cache this?
// TODO: Returns -1 on not error
let bufferSize = sysconf(_SC_GETPW_R_SIZE_MAX)
let buffer = UnsafeMutablePointer<Int8>.alloc(bufferSize)
let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: bufferSize)

// TODO: Check result for nil pointer - indictes not found
// TODO: Add note about not using getpwuid()
// TODO: Add note about not using getpwuid()
if getpwuid_r(uid, &userInfo, buffer, bufferSize, &result) == 0 {
username = String.fromCString(userInfo.pw_name)!
username = String(cString: userInfo.pw_name)
}
else {
username = String()
}

buffer.dealloc(bufferSize)
buffer.deallocate(capacity: bufferSize)
// TODO: Why does this fail?
//result.dealloc(1)

Expand Down
4 changes: 2 additions & 2 deletions dshb/WidgetTemperature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct WidgetTemperature: WidgetType {
var stats = [WidgetUIStat]()

let maxValue = 128.0
private let sensors: [TemperatureSensor]
fileprivate let sensors: [TemperatureSensor]

init(window: Window = Window()) {
title = WidgetUITitle(name: name, window: window)
Expand All @@ -41,7 +41,7 @@ struct WidgetTemperature: WidgetType {
do {
// TODO: Add battery temperature from SystemKit? SMC will usually
// have a key for it too (though not always certain which one)
let allKnownSensors = try SMCKit.allKnownTemperatureSensors().sort
let allKnownSensors = try SMCKit.allKnownTemperatureSensors().sorted
{ $0.name < $1.name }

let allUnknownSensors: [TemperatureSensor]
Expand Down
34 changes: 17 additions & 17 deletions dshb/WidgetUIStat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

import Foundation

typealias WarningLevel = (range: HalfOpenInterval<Double>, color: WidgetUIColor)
typealias WarningLevel = (range: Range<Double>, color: WidgetUIColor)


enum Unit: String {
Expand Down Expand Up @@ -55,15 +55,15 @@ struct WidgetUIStat {
var maxValue: Double
var window: Window

var Cool: WarningLevel = (-Double.infinity..<0, WidgetUIColor.WarningLevelCool)
var Nominal: WarningLevel = (0..<0.45, WidgetUIColor.WarningLevelNominal)
var Danger: WarningLevel = (0.45..<0.75, WidgetUIColor.WarningLevelDanger)
var Crisis: WarningLevel = (0.75..<Double.infinity, WidgetUIColor.WarningLevelCrisis)
var Cool: WarningLevel = (-Double.infinity..<0, WidgetUIColor.warningLevelCool)
var Nominal: WarningLevel = (0..<0.45, WidgetUIColor.warningLevelNominal)
var Danger: WarningLevel = (0.45..<0.75, WidgetUIColor.warningLevelDanger)
var Crisis: WarningLevel = (0.75..<Double.infinity, WidgetUIColor.warningLevelCrisis)

private let nameCount: Int
private var unitCount = 0
private var lastStr = String()
private var lastPercentage = 0.0
fileprivate let nameCount: Int
fileprivate var unitCount = 0
fileprivate var lastStr = String()
fileprivate var lastPercentage = 0.0

init(name: String, unit: Unit, max: Double, window: Window = Window()) {
self.name = name
Expand All @@ -75,31 +75,31 @@ struct WidgetUIStat {
unitCount = unit.rawValue.characters.count
}

mutating func draw(str: String, percentage: Double) {
mutating func draw(_ str: String, percentage: Double) {
lastStr = str
lastPercentage = percentage

let spaceCount = window.length - nameCount - str.characters.count - unitCount
let space = String(count: max(spaceCount, 2), repeatedValue: Character(" "))
let space = String(repeating: " ", count: max(spaceCount, 2))


var shortenedName = name
if spaceCount <= 0 {
shortenedName = (name as NSString).substringToIndex(nameCount + spaceCount - 2)
shortenedName = (name as NSString).substring(to: nameCount + spaceCount - 2)
shortenedName.append(Character("…"))
}


let charactersToColorCount: Int
if percentage.isSignMinus {
if percentage.sign == .minus {
charactersToColorCount = window.length
} else {
charactersToColorCount = Int(Double(window.length) * percentage)
}

let fullStr = (shortenedName + space + str + unit.rawValue) as NSString
let coloredStr = fullStr.substringToIndex(charactersToColorCount)
let uncoloredStr = fullStr.substringFromIndex(charactersToColorCount)
let coloredStr = fullStr.substring(to: charactersToColorCount)
let uncoloredStr = fullStr.substring(from: charactersToColorCount)


move(window.point.y, window.point.x)
Expand All @@ -112,11 +112,11 @@ struct WidgetUIStat {
}

addstr(coloredStr)
attrset(COLOR_PAIR(WidgetUIColor.Background.rawValue))
attrset(COLOR_PAIR(WidgetUIColor.background.rawValue))
addstr(uncoloredStr)
}

mutating func resize(window: Window) {
mutating func resize(_ window: Window) {
self.window = window
draw(lastStr, percentage: lastPercentage)
}
Expand Down
10 changes: 5 additions & 5 deletions dshb/WidgetUITitle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct WidgetUITitle {
let nameCount: Int
var window : Window

private var padding = String()
fileprivate var padding = String()

init(name: String, window: Window) {
self.name = name
Expand All @@ -44,21 +44,21 @@ struct WidgetUITitle {

func draw() {
move(window.point.y, window.point.x)
attrset(COLOR_PAIR(WidgetUIColor.Title.rawValue))
attrset(COLOR_PAIR(WidgetUIColor.title.rawValue))
addstr(name + padding)
}

mutating func resize(window: Window) {
mutating func resize(_ window: Window) {
self.window = window
generatePadding()
draw()
}

private mutating func generatePadding() {
fileprivate mutating func generatePadding() {
var paddingSize = window.length - nameCount

if paddingSize < 0 { paddingSize = 0 }

padding = String(count: paddingSize, repeatedValue: Character(" "))
padding = String(repeating: " ", count: paddingSize)
}
}
Loading