Skip to content

Commit

Permalink
Merge pull request #15 from exelban/dev
Browse files Browse the repository at this point in the history
v1.2.7
  • Loading branch information
exelban authored Jul 13, 2019
2 parents 57ed301 + e87764e commit e1b4d28
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 38 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog
All notable changes to this project will be documented in this file.

### [v1.2.7]
- added hyperthreading mode in Bar Chart for CPU
- fixed few bugs

### [v1.2.6]
- fixed CPU usage

Expand Down Expand Up @@ -51,6 +55,7 @@ All notable changes to this project will be documented in this file.
### [v1.0.0]
- first release

[v1.2.7]: https://github.com/exelban/stats/releases/tag/v1.2.7
[v1.2.6]: https://github.com/exelban/stats/releases/tag/v1.2.6
[v1.2.5]: https://github.com/exelban/stats/releases/tag/v1.2.5
[v1.2.4]: https://github.com/exelban/stats/releases/tag/v1.2.4
Expand Down
17 changes: 17 additions & 0 deletions Stats/Modules/CPU/CPU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CPU: Module {
var available: Observable<Bool>
var color: Observable<Bool>
var label: Observable<Bool>
var hyperthreading: Observable<Bool>
var reader: Reader = CPUReader()

let defaults = UserDefaults.standard
Expand All @@ -26,6 +27,7 @@ class CPU: Module {
init() {
self.available = Observable(true)
self.active = Observable(defaults.object(forKey: name) != nil ? defaults.bool(forKey: name) : true)
self.hyperthreading = Observable(defaults.object(forKey: "\(name)_hyperthreading") != nil ? defaults.bool(forKey: "\(name)_hyperthreading") : true)
self.widgetType = defaults.object(forKey: "\(name)_widget") != nil ? defaults.float(forKey: "\(name)_widget") : Widgets.Mini
self.color = Observable(defaults.object(forKey: "\(name)_color") != nil ? defaults.bool(forKey: "\(name)_color") : false)
self.label = Observable(defaults.object(forKey: "\(name)_label") != nil ? defaults.bool(forKey: "\(name)_label") : true)
Expand All @@ -34,6 +36,7 @@ class CPU: Module {

if self.widgetType == Widgets.BarChart {
(self.reader as! CPUReader).perCoreMode = true
(self.reader as! CPUReader).hyperthreading = self.hyperthreading.value
}
}

Expand Down Expand Up @@ -64,6 +67,10 @@ class CPU: Module {
barChart.state = self.widgetType == Widgets.BarChart ? NSControl.StateValue.on : NSControl.StateValue.off
barChart.target = self

let hyperthreading = NSMenuItem(title: "Hyperthreading", action: #selector(toggleHyperthreading), keyEquivalent: "")
hyperthreading.state = self.hyperthreading.value ? NSControl.StateValue.on : NSControl.StateValue.off
hyperthreading.target = self

let color = NSMenuItem(title: "Color", action: #selector(toggleColor), keyEquivalent: "")
color.state = self.color.value ? NSControl.StateValue.on : NSControl.StateValue.off
color.target = self
Expand All @@ -79,6 +86,9 @@ class CPU: Module {

submenu.addItem(NSMenuItem.separator())

if self.widgetType == Widgets.BarChart {
submenu.addItem(hyperthreading)
}
if self.widgetType == Widgets.BarChart || self.widgetType == Widgets.ChartWithValue || self.widgetType == Widgets.Chart {
submenu.addItem(label)
}
Expand Down Expand Up @@ -145,6 +155,13 @@ class CPU: Module {
self.active << true
}

@objc func toggleHyperthreading(_ sender: NSMenuItem) {
sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on
self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(name)_hyperthreading")
self.hyperthreading << (sender.state == NSControl.StateValue.on)
(self.reader as! CPUReader).hyperthreading = sender.state == NSControl.StateValue.on
}

@objc func toggleColor(_ sender: NSMenuItem) {
sender.state = sender.state == NSControl.StateValue.on ? NSControl.StateValue.off : NSControl.StateValue.on
self.defaults.set(sender.state == NSControl.StateValue.on, forKey: "\(name)_color")
Expand Down
10 changes: 8 additions & 2 deletions Stats/Modules/CPU/CPUReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CPUReader: Reader {
let CPUUsageLock: NSLock = NSLock()

var perCoreMode: Bool = false
var hyperthreading: Bool = true

init() {
let mibKeys: [Int32] = [ CTL_HW, HW_NCPU ]
Expand Down Expand Up @@ -59,7 +60,12 @@ class CPUReader: Reader {
var totalOnAllCores: Int32 = 0
var usagePerCore: [Double] = []

for i in 0 ..< Int32(numCPUs) {
var incrementNumber = 1
if !self.hyperthreading && self.perCoreMode {
incrementNumber = 2
}

for i in stride(from: 0, to: Int32(numCPUs), by: incrementNumber){
var inUse: Int32
var total: Int32
if let prevCpuInfo = prevCpuInfo {
Expand All @@ -80,7 +86,7 @@ class CPUReader: Reader {

inUseOnAllCores = inUseOnAllCores + inUse
totalOnAllCores = totalOnAllCores + total
usagePerCore.insert((Double(inUse) / Double(total)), at: Int(i))
usagePerCore.append(Double(inUse) / Double(total))
}

if self.perCoreMode {
Expand Down
8 changes: 7 additions & 1 deletion Stats/Modules/Network/Network.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class Network: Module {
arrowsWithText.state = self.widgetType == Widgets.NetworkArrowsWithText ? NSControl.StateValue.on : NSControl.StateValue.off
arrowsWithText.target = self

let chart = NSMenuItem(title: "Chart", action: #selector(toggleWidget), keyEquivalent: "")
chart.state = self.widgetType == Widgets.NetworkChart ? NSControl.StateValue.on : NSControl.StateValue.off
chart.target = self

submenu.addItem(dots)
submenu.addItem(arrows)
submenu.addItem(text)
Expand Down Expand Up @@ -113,6 +117,8 @@ class Network: Module {
widgetCode = Widgets.NetworkDotsWithText
case "Arrows with text":
widgetCode = Widgets.NetworkArrowsWithText
case "Chart":
widgetCode = Widgets.NetworkChart
default:
break
}
Expand All @@ -122,7 +128,7 @@ class Network: Module {
}

for item in self.submenu.items {
if item.title == "Dots" || item.title == "Arrows" || item.title == "Text" || item.title == "Dots with text" || item.title == "Arrows with text" {
if item.title == "Dots" || item.title == "Arrows" || item.title == "Text" || item.title == "Dots with text" || item.title == "Arrows with text" || item.title == "Chart" {
item.state = NSControl.StateValue.off
}
}
Expand Down
7 changes: 1 addition & 6 deletions Stats/Modules/Network/NetworkReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,7 @@ class NetworkReader: Reader {
guard let download = Int64(arr[2]), let upload = Int64(arr[5]) else {
return
}

guard let value: Double = Double("\(download).\(upload)") else {
return
}

self.value << [value]
self.value << [Double(download), Double(upload)]
}
}

Expand Down
2 changes: 1 addition & 1 deletion Stats/Supporting Files/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.2.6</string>
<string>1.2.7</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>LSApplicationCategoryType</key>
Expand Down
3 changes: 3 additions & 0 deletions Stats/Widgets/BarChart.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ class BarChart: NSView, Widget {
if self.partitions.count == 1 {
width = 18
}
if self.partitions.count == 2 {
width = 28
}
if self.labelEnabled {
width += labelPadding
}
Expand Down
60 changes: 32 additions & 28 deletions Stats/Widgets/NetworkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class NetworkDotsView: NSView, Widget {
}

func setValue(data: [Double]) {
let value: Double = data.first!
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])

let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
if self.download != download {
self.download = download
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
}
}

Expand Down Expand Up @@ -118,10 +118,11 @@ class NetworkTextView: NSView, Widget {
}

func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
downloadValue.stringValue = Units(bytes: values[0]).getReadableUnit()
uploadValue.stringValue = Units(bytes: values[1]).getReadableUnit()
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])

downloadValue.stringValue = Units(bytes: download).getReadableUnit()
uploadValue.stringValue = Units(bytes: upload).getReadableUnit()
}

func toggleColor(state: Bool) {
Expand Down Expand Up @@ -237,13 +238,14 @@ class NetworkArrowsView: NSView, Widget {
}

func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])

if self.download != download {
self.download = download
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
}
}

Expand Down Expand Up @@ -320,14 +322,15 @@ class NetworkDotsTextView: NSView, Widget {
}

func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])

if self.download != download {
self.download = download
downloadValue.stringValue = Units(bytes: self.download).getReadableUnit()
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit()
}
}
Expand Down Expand Up @@ -448,14 +451,15 @@ class NetworkArrowsTextView: NSView, Widget {
}

func setValue(data: [Double]) {
let value: Double = data.first!
let values = value.splitAtDecimal()
if self.download != values[0] {
self.download = values[0]
let download: Int64 = Int64(data[0])
let upload: Int64 = Int64(data[1])

if self.download != download {
self.download = download
downloadValue.stringValue = Units(bytes: self.download).getReadableUnit()
}
if self.upload != values[1] {
self.upload = values[1]
if self.upload != upload {
self.upload = upload
uploadValue.stringValue = Units(bytes: self.upload).getReadableUnit()
}
}
Expand Down
1 change: 1 addition & 0 deletions Stats/Widgets/Widget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct Widgets {
static let NetworkText: WidgetType = 2.2
static let NetworkDotsWithText: WidgetType = 2.3
static let NetworkArrowsWithText: WidgetType = 2.4
static let NetworkChart: WidgetType = 2.5

static let BarChart: WidgetType = 3.0
}

0 comments on commit e1b4d28

Please sign in to comment.