start updating macos stuff

This commit is contained in:
Mars 2025-02-18 23:57:45 -05:00
parent a02ffbed47
commit 46074a94a2
Signed by: pupbrained
GPG key ID: 874E22DF2F9DFCB5
4 changed files with 35 additions and 43 deletions

View file

@ -123,13 +123,11 @@ namespace {
using namespace ftxui; using namespace ftxui;
fn CreateColorCircles() -> Element { fn CreateColorCircles() -> Element {
Elements circles; Elements circles(16);
for (int i = 0; i < 16; ++i) std::generate_n(circles.begin(), 16, [colorIndex = 0]() mutable {
circles.push_back(hbox({ return hbox({ text("") | bold | color(Color::Palette256(colorIndex++)), text(" ") });
text("") | bold | color(Color::Palette256(i)), });
text(" "),
}));
return hbox(circles); return hbox(circles);
} }

View file

@ -7,26 +7,26 @@
#include "macos/bridge.h" #include "macos/bridge.h"
#include "os.h" #include "os.h"
fn GetMemInfo() -> std::expected<u64, string> { fn GetMemInfo() -> expected<u64, string> {
u64 mem = 0; u64 mem = 0;
usize size = sizeof(mem); usize size = sizeof(mem);
if (sysctlbyname("hw.memsize", &mem, &size, nullptr, 0) == -1) 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; return mem;
} }
fn GetNowPlaying() -> string { fn GetNowPlaying() -> expected<string, NowPlayingError> {
if (const char* title = GetCurrentPlayingTitle(); const char* artist = GetCurrentPlayingArtist()) if (const char* title = GetCurrentPlayingTitle(); const char* artist = GetCurrentPlayingArtist())
return "Now Playing: " + string(artist) + " - " + string(title); return "Now Playing: " + string(artist) + " - " + string(title);
return "No song playing"; return "No song playing";
} }
fn GetOSVersion() -> string { return GetMacOSVersion(); } fn GetOSVersion() -> expected<string, string> { return GetMacOSVersion(); }
fn GetDesktopEnvironment() -> string { return "Aqua"; } fn GetDesktopEnvironment() -> optional<string> { return std::nullopt; }
fn GetWindowManager() -> string { return "Yabai"; } fn GetWindowManager() -> string { return "Yabai"; }

View file

@ -7,18 +7,19 @@
#ifdef __OBJC__ #ifdef __OBJC__
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#include <expected>
@interface Bridge : NSObject @interface Bridge : NSObject
+ (NSDictionary*)currentPlayingMetadata; + (NSDictionary*)currentPlayingMetadata;
+ (NSString*)macOSVersion; + (std::expected<const char*, const char*>)macOSVersion;
@end @end
#else #else
extern "C" { extern "C++" {
fn GetCurrentPlayingTitle() -> const char*; fn GetCurrentPlayingTitle() -> const char*;
fn GetCurrentPlayingArtist() -> const char*; fn GetCurrentPlayingArtist() -> const char*;
fn GetMacOSVersion() -> const char*; fn GetMacOSVersion() -> std::expected<const char*, const char*>;
} }
#endif #endif

View file

@ -1,6 +1,7 @@
#ifdef __APPLE__ #ifdef __APPLE__
#import <dispatch/dispatch.h> #import <dispatch/dispatch.h>
#include <expected>
#import <objc/runtime.h> #import <objc/runtime.h>
#import "bridge.h" #import "bridge.h"
@ -57,36 +58,34 @@ using MRMediaRemoteGetNowPlayingInfoFunction =
return nowPlayingInfo; return nowPlayingInfo;
} }
+ (NSString*)macOSVersion { + (std::expected<const char*, const char*>)macOSVersion {
NSProcessInfo* processInfo = [NSProcessInfo processInfo]; NSProcessInfo* processInfo = [NSProcessInfo processInfo];
NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion];
NSOperatingSystemVersion osVersion = [processInfo operatingSystemVersion]; // Build version number string
NSString* versionNumber = nullptr;
NSString* version = nullptr; if (osVersion.patchVersion == 0)
if (osVersion.patchVersion == 0) { versionNumber = [NSString stringWithFormat:@"%ld.%ld", osVersion.majorVersion, osVersion.minorVersion];
version = [NSString stringWithFormat:@"%ld.%ld", osVersion.majorVersion, osVersion.minorVersion]; else
} else { versionNumber = [NSString
version = [NSString
stringWithFormat:@"%ld.%ld.%ld", osVersion.majorVersion, osVersion.minorVersion, osVersion.patchVersion]; stringWithFormat:@"%ld.%ld.%ld", osVersion.majorVersion, osVersion.minorVersion, osVersion.patchVersion];
}
// Dictionary to map macOS versions to their respective names // Map major version to name
NSDictionary<NSNumber*, NSString*>* versionNames = NSDictionary* versionNames =
@{ @11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma", @15 : @"Sequoia" }; @{ @11 : @"Big Sur", @12 : @"Monterey", @13 : @"Ventura", @14 : @"Sonoma", @15 : @"Sequoia" };
NSNumber* majorVersion = @(osVersion.majorVersion);
NSString* versionName = versionNames[majorVersion];
NSNumber* majorVersionNumber = @(osVersion.majorVersion); if (!versionName)
NSString* versionName = versionNames[majorVersionNumber]; return std::unexpected("Unsupported macOS version");
if (versionName == nil) NSString* fullVersion = [NSString stringWithFormat:@"macOS %@ %@", versionNumber, versionName];
versionName = @"Unknown"; return strdup([fullVersion UTF8String]);
NSString* fullVersion = [NSString stringWithFormat:@"macOS %@ %@", version, versionName];
return fullVersion;
} }
@end @end
extern "C" { extern "C++" {
// NOLINTBEGIN(misc-use-internal-linkage)
fn GetCurrentPlayingTitle() -> const char* { fn GetCurrentPlayingTitle() -> const char* {
NSDictionary* metadata = [Bridge currentPlayingMetadata]; NSDictionary* metadata = [Bridge currentPlayingMetadata];
@ -115,14 +114,8 @@ extern "C" {
return nullptr; return nullptr;
} }
fn GetMacOSVersion() -> const char* { fn GetMacOSVersion() -> std::expected<const char*, const char*> { return [Bridge macOSVersion]; }
NSString* version = [Bridge macOSVersion]; // NOLINTEND(misc-use-internal-linkage)
if (version)
return strdup([version UTF8String]);
return nullptr;
}
} }
#endif #endif