Skip to content

Commit

Permalink
Add showAwakeTime
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jan 30, 2025
1 parent f8a724f commit 860ecb6
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 18 deletions.
1 change: 1 addition & 0 deletions src/TrueWidget/swift/UserSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ final class UserSettings: ObservableObject {
@AppStorage("showOperatingSystem") var showOperatingSystem: Bool = true
@AppStorage("operatingSystemFontSize") var operatingSystemFontSize: Double = 14.0
@AppStorage("showUptime") var showUptime: Bool = false
@AppStorage("showAwakeTime") var showAwakeTime: Bool = false
@AppStorage("showHostName") var showHostName: Bool = false
@AppStorage("showRootVolumeName") var showRootVolumeName: Bool = false
@AppStorage("showUserName") var showUserName: Bool = false
Expand Down
4 changes: 4 additions & 0 deletions src/TrueWidget/swift/Views/Main/MainOperatingSystemView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ struct MainOperatingSystemView: View {
}

VStack(alignment: .trailing, spacing: 0) {
if userSettings.showAwakeTime {
Text("awake \(operatingSystem.awakeTime)")
}

if userSettings.showHostName {
Text(operatingSystem.hostName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ struct SettingsOperatingSystemView: View {
}
.switchToggleStyle()

Toggle(isOn: $userSettings.showAwakeTime) {
Text("Show awake time")
}
.switchToggleStyle()

Toggle(isOn: $userSettings.showHostName) {
Text("Show host name")
}
Expand Down
54 changes: 36 additions & 18 deletions src/TrueWidget/swift/WidgetSource/OperatingSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ extension WidgetSource {

@Published public var version = ""
@Published public var uptime = ""
@Published public var awakeTime = ""
@Published public var hostName = ""
@Published public var rootVolumeName = ""
@Published public var userName = ""
Expand Down Expand Up @@ -63,25 +64,18 @@ extension WidgetSource {

@MainActor
private func update() {
if userSettings.showUptime {
if let uptimeSeconds = getSecondsFromBoot() {
let days = uptimeSeconds / (24 * 3600)
let hours = (uptimeSeconds % (24 * 3600)) / 3600
let minutes = (uptimeSeconds % 3600) / 60

var daysString = ""
if days > 1 {
daysString = "\(days) days, "
} else if days == 1 {
daysString = "1 day, "
}
if userSettings.showUptime || userSettings.showAwakeTime {
let uptimeSeconds = getSecondsFromBoot()
let awakeTimeSeconds = Int(ProcessInfo.processInfo.systemUptime)

uptime = formatUptime(seconds: uptimeSeconds)
awakeTime = formatUptime(seconds: awakeTimeSeconds)

uptime = String(
format: "%@%02d:%02d",
daysString,
hours,
minutes
)
if let uptimeSeconds = uptimeSeconds {
if uptimeSeconds > 0 {
let ratio = Double(awakeTimeSeconds) / Double(uptimeSeconds) * 100.0
awakeTime += " (\(String(format: "%.02f", ratio))%)"
}
}
}

Expand Down Expand Up @@ -134,5 +128,29 @@ extension WidgetSource {
let uptime = Date().timeIntervalSince(bootDate)
return Int(uptime)
}

private func formatUptime(seconds: Int?) -> String {
if let seconds = seconds {
let days = seconds / (24 * 3600)
let hours = (seconds % (24 * 3600)) / 3600
let minutes = (seconds % 3600) / 60

var daysString = ""
if days > 1 {
daysString = "\(days) days, "
} else if days == 1 {
daysString = "1 day, "
}

return String(
format: "%@%02d:%02d",
daysString,
hours,
minutes
)
} else {
return "---"
}
}
}
}

0 comments on commit 860ecb6

Please sign in to comment.