Skip to content

Commit

Permalink
fix(): nettop high cpu usage when no stdout stderr stdin attached. sh…
Browse files Browse the repository at this point in the history
…ow 0KB/s when little traffic
  • Loading branch information
cnfatal committed Nov 1, 2021
1 parent fce1e2f commit 8e658a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,23 @@
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>BE3EC8FE272FA6E6005AC578</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>BE3EC90F272FA6E7005AC578</key>
<dict>
<key>primary</key>
<true/>
</dict>
<key>BE3EC919272FA6E7005AC578</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>
2 changes: 2 additions & 0 deletions NetworkStatusBar/NetworkStatus.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ open class NetworkDetails{
process.executableURL = URL.init(fileURLWithPath: "/usr/bin/nettop")
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
process.standardInput = Pipe()
Task{
var data = Data()
for try await line in pipe.fileHandleForReading.bytes.lines{
Expand Down
6 changes: 4 additions & 2 deletions NetworkStatusBar/NetworkStatusBarApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class AppDelegate :NSObject, NSApplicationDelegate{
func onUpdate(update:NetworkStates){
DispatchQueue.main.async {
self.iostates.total = update.Total
self.iostates.items = update.Items
self.iostates.items = update.Items.filter({ item in
return item.total > 1024
})
}
}

Expand Down Expand Up @@ -59,7 +61,7 @@ class AppDelegate :NSObject, NSApplicationDelegate{
// ListTableCellView has 16dp at left and right,ListScroll has a 15dp width
// 16 * 2 + 15 = 47
// Out DetailsItem has min length 200
menuitem.view?.setFrameSize(NSSize(width: 247, height: 300))
menuitem.view?.setFrameSize(NSSize(width: 247, height: 100))
return menuitem
}(),
NSMenuItem(
Expand Down
13 changes: 12 additions & 1 deletion NetworkStatusBar/StatusBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,19 @@ struct StatusBarView_Previews: PreviewProvider {
}
}


let formatter = { () -> ByteCountFormatter in
var formatter = ByteCountFormatter.init()
formatter.allowsNonnumericFormatting = false
formatter.countStyle = .binary
return formatter
}()

func formatbytes(_ bytes:Int)->String {
if bytes < 1024 {
return "0KB/s"
}
return String(format: "%@/s", arguments: [
ByteCountFormatter.string(fromByteCount: Int64(bytes), countStyle: ByteCountFormatter.CountStyle.binary)
formatter.string(fromByteCount: Int64(bytes))
])
}

0 comments on commit 8e658a0

Please sign in to comment.