diff --git a/src/main.cpp b/src/main.cpp index d9049d3..81098cc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -123,13 +123,11 @@ namespace { using namespace ftxui; fn CreateColorCircles() -> Element { - Elements circles; + Elements circles(16); - for (int i = 0; i < 16; ++i) - circles.push_back(hbox({ - text("◯") | bold | color(Color::Palette256(i)), - text(" "), - })); + std::generate_n(circles.begin(), 16, [colorIndex = 0]() mutable { + return hbox({ text("◯") | bold | color(Color::Palette256(colorIndex++)), text(" ") }); + }); return hbox(circles); } diff --git a/src/os/macos.cpp b/src/os/macos.cpp index 45ad334..45e6f68 100644 --- a/src/os/macos.cpp +++ b/src/os/macos.cpp @@ -7,26 +7,26 @@ #include "macos/bridge.h" #include "os.h" -fn GetMemInfo() -> std::expected { +fn GetMemInfo() -> expected { u64 mem = 0; usize size = sizeof(mem); if (sysctlbyname("hw.memsize", &mem, &size, nullptr, 0) == -1) - return std::unexpected(std::string("sysctlbyname failed: ") + strerror(errno)); + return std::unexpected(string("sysctlbyname failed: ") + strerror(errno)); return mem; } -fn GetNowPlaying() -> string { +fn GetNowPlaying() -> expected { if (const char* title = GetCurrentPlayingTitle(); const char* artist = GetCurrentPlayingArtist()) return "Now Playing: " + string(artist) + " - " + string(title); return "No song playing"; } -fn GetOSVersion() -> string { return GetMacOSVersion(); } +fn GetOSVersion() -> expected { return GetMacOSVersion(); } -fn GetDesktopEnvironment() -> string { return "Aqua"; } +fn GetDesktopEnvironment() -> optional { return std::nullopt; } fn GetWindowManager() -> string { return "Yabai"; } diff --git a/src/os/macos/bridge.h b/src/os/macos/bridge.h index d08b788..8d371d6 100644 --- a/src/os/macos/bridge.h +++ b/src/os/macos/bridge.h @@ -7,18 +7,19 @@ #ifdef __OBJC__ #import +#include @interface Bridge : NSObject + (NSDictionary*)currentPlayingMetadata; -+ (NSString*)macOSVersion; ++ (std::expected)macOSVersion; @end #else -extern "C" { +extern "C++" { fn GetCurrentPlayingTitle() -> const char*; fn GetCurrentPlayingArtist() -> const char*; - fn GetMacOSVersion() -> const char*; + fn GetMacOSVersion() -> std::expected; } #endif diff --git a/src/os/macos/bridge.mm b/src/os/macos/bridge.mm index 70a26ed..fe6caf9 100644 --- a/src/os/macos/bridge.mm +++ b/src/os/macos/bridge.mm @@ -1,6 +1,7 @@ #ifdef __APPLE__ #import +#include #import #import "bridge.h" @@ -57,36 +58,34 @@ using MRMediaRemoteGetNowPlayingInfoFunction = return nowPlayingInfo; } -+ (NSString*)macOSVersion { - NSProcessInfo* processInfo = [NSProcessInfo processInfo]; ++ (std::expected)macOSVersion { + NSProcessInfo* processInfo = [NSProcessInfo processInfo]; + NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion]; - NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion]; - - NSString* version = nullptr; - if (osVersion.patchVersion == 0) { - version = [NSString stringWithFormat:@"%ld.%ld", osVersion.majorVersion, osVersion.minorVersion]; - } else { - version = [NSString + // Build version number string + NSString* versionNumber = nullptr; + if (osVersion.patchVersion == 0) + versionNumber = [NSString stringWithFormat:@"%ld.%ld", osVersion.majorVersion, osVersion.minorVersion]; + else + versionNumber = [NSString stringWithFormat:@"%ld.%ld.%ld", osVersion.majorVersion, osVersion.minorVersion, osVersion.patchVersion]; - } - // Dictionary to map macOS versions to their respective names - NSDictionary* versionNames = + // Map major version to name + NSDictionary* versionNames = @{ @11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma", @15 : @"Sequoia" }; + NSNumber* majorVersion = @(osVersion.majorVersion); + NSString* versionName = versionNames[majorVersion]; - NSNumber* majorVersionNumber = @(osVersion.majorVersion); - NSString* versionName = versionNames[majorVersionNumber]; + if (!versionName) + return std::unexpected("Unsupported macOS version"); - if (versionName == nil) - versionName = @"Unknown"; - - NSString* fullVersion = [NSString stringWithFormat:@"macOS %@ %@", version, versionName]; - - return fullVersion; + NSString* fullVersion = [NSString stringWithFormat:@"macOS %@ %@", versionNumber, versionName]; + return strdup([fullVersion UTF8String]); } @end -extern "C" { +extern "C++" { + // NOLINTBEGIN(misc-use-internal-linkage) fn GetCurrentPlayingTitle() -> const char* { NSDictionary* metadata = [Bridge currentPlayingMetadata]; @@ -115,14 +114,8 @@ extern "C" { return nullptr; } - fn GetMacOSVersion() -> const char* { - NSString* version = [Bridge macOSVersion]; - - if (version) - return strdup([version UTF8String]); - - return nullptr; - } + fn GetMacOSVersion() -> std::expected { return [Bridge macOSVersion]; } + // NOLINTEND(misc-use-internal-linkage) } #endif