From 801a8d1754d99ba48e1e023726f7840cc91ccd0f Mon Sep 17 00:00:00 2001 From: Mars Date: Thu, 24 Apr 2025 13:01:16 -0400 Subject: [PATCH] fixes and macos stuff --- meson.build | 2 + src/config/config.cpp | 1 - src/config/weather.cpp | 6 +- src/config/weather.h | 6 +- src/core/system_data.cpp | 7 +- src/main.cpp | 140 +++++++++++++++++---------------------- src/os/macos.cpp | 18 ++--- src/os/macos/bridge.mm | 6 +- src/os/os.h | 3 +- src/util/macros.h | 34 +++++----- 10 files changed, 108 insertions(+), 115 deletions(-) diff --git a/meson.build b/meson.build index 15187bd..d4983e6 100644 --- a/meson.build +++ b/meson.build @@ -9,6 +9,7 @@ project( 'default_library=static', 'buildtype=debugoptimized', 'b_vscrt=mt', + 'b_lto=true', 'warning_level=3', ], ) @@ -24,6 +25,7 @@ common_warning_flags = [ '-Wno-c++20-extensions', '-Wno-c++98-compat', '-Wno-c++98-compat-pedantic', + '-Wno-covered-switch-default', '-Wno-disabled-macro-expansion', '-Wno-missing-prototypes', '-Wno-padded', diff --git a/src/config/config.cpp b/src/config/config.cpp index cc203b4..ff069dc 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -1,4 +1,3 @@ -#include #include #include diff --git a/src/config/weather.cpp b/src/config/weather.cpp index 9ba93bb..452e546 100644 --- a/src/config/weather.cpp +++ b/src/config/weather.cpp @@ -39,7 +39,7 @@ namespace { DEBUG_LOG("Reading from cache file..."); try { - const String content((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); + const String content((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); WeatherOutput result; if (const glz::error_ctx errc = glz::read(result, content); errc.ec != glz::error_code::none) @@ -79,7 +79,9 @@ namespace { std::error_code errc; fs::rename(tempPath, *cachePath, errc); if (errc) { - fs::remove(tempPath, errc); + if (!fs::remove(tempPath, errc)) + DEBUG_LOG("Failed to remove temp file: {}", errc.message()); + return Err("Failed to replace cache file: " + errc.message()); } diff --git a/src/config/weather.h b/src/config/weather.h index 25063d9..df04f9f 100644 --- a/src/config/weather.h +++ b/src/config/weather.h @@ -8,7 +8,7 @@ struct Condition { String description; - struct glaze { + struct [[maybe_unused]] glaze { using T = Condition; static constexpr glz::detail::Object value = glz::object("description", &T::description); @@ -18,7 +18,7 @@ struct Condition { struct Main { f64 temp; - struct glaze { + struct [[maybe_unused]] glaze { using T = Main; static constexpr glz::detail::Object value = glz::object("temp", &T::temp); @@ -36,7 +36,7 @@ struct WeatherOutput { Vec weather; usize dt; - struct glaze { + struct [[maybe_unused]] glaze { using T = WeatherOutput; static constexpr glz::detail::Object value = diff --git a/src/core/system_data.cpp b/src/core/system_data.cpp index 94a24e6..38c62f7 100644 --- a/src/core/system_data.cpp +++ b/src/core/system_data.cpp @@ -34,6 +34,11 @@ fn SystemData::fetchSystemData(const Config& config) -> SystemData { .mem_info = os::GetMemInfo(), .desktop_environment = os::GetDesktopEnvironment(), .window_manager = os::GetWindowManager(), + .now_playing = {}, + .weather_info = {}, + .disk_used = {}, + .disk_total = {}, + .shell = {}, }; auto diskShell = std::async(std::launch::async, [] { @@ -61,4 +66,4 @@ fn SystemData::fetchSystemData(const Config& config) -> SystemData { data.now_playing = nowPlaying.get(); return data; -} \ No newline at end of file +} diff --git a/src/main.cpp b/src/main.cpp index e592a6b..711296d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,12 +11,11 @@ #include "core/system_data.h" #include "os/os.h" -constexpr inline bool SHOW_ICONS = true; - namespace ui { using ftxui::Color; - constexpr i32 MAX_PARAGRAPH_LENGTH = 30; + static constexpr inline bool SHOW_ICONS = true; + static constexpr i32 MAX_PARAGRAPH_LENGTH = 30; // Color themes struct Theme { @@ -24,34 +23,32 @@ namespace ui { Color::Palette16 label; Color::Palette16 value; Color::Palette16 border; - Color::Palette16 accent; }; - constexpr Theme DEFAULT_THEME = { + static constexpr Theme DEFAULT_THEME = { .icon = Color::Cyan, .label = Color::Yellow, .value = Color::White, .border = Color::GrayLight, - .accent = Color::Magenta, }; struct Icons { - std::string_view user; - std::string_view palette; - std::string_view calendar; - std::string_view host; - std::string_view kernel; - std::string_view os; - std::string_view memory; - std::string_view weather; - std::string_view music; - std::string_view disk; - std::string_view shell; - std::string_view desktop; - std::string_view window_manager; + [[maybe_unused]] StringView user; + [[maybe_unused]] StringView palette; + [[maybe_unused]] StringView calendar; + [[maybe_unused]] StringView host; + [[maybe_unused]] StringView kernel; + [[maybe_unused]] StringView os; + [[maybe_unused]] StringView memory; + [[maybe_unused]] StringView weather; + [[maybe_unused]] StringView music; + [[maybe_unused]] StringView disk; + [[maybe_unused]] StringView shell; + [[maybe_unused]] StringView desktop; + [[maybe_unused]] StringView window_manager; }; - constexpr Icons EMPTY_ICONS = { + static constexpr Icons EMPTY_ICONS = { .user = "", .palette = "", .calendar = "", @@ -67,8 +64,7 @@ namespace ui { .window_manager = "", }; - // Using your original icons - constexpr Icons NERD_ICONS = { + static constexpr Icons NERD_ICONS = { .user = "  ", .palette = "  ", .calendar = "  ", @@ -103,32 +99,27 @@ namespace { const bool nowPlayingEnabled = config.now_playing.enabled; const auto& [userIcon, paletteIcon, calendarIcon, hostIcon, kernelIcon, osIcon, memoryIcon, weatherIcon, musicIcon, diskIcon, shellIcon, deIcon, wmIcon] = - // ReSharper disable once CppDFAUnreachableCode - SHOW_ICONS ? ui::NERD_ICONS : ui::EMPTY_ICONS; + ui::SHOW_ICONS ? ui::NERD_ICONS : ui::EMPTY_ICONS; Elements content; content.push_back(text(String(userIcon) + "Hello " + name + "! ") | bold | color(Color::Cyan)); content.push_back(separator() | color(ui::DEFAULT_THEME.border)); - content.push_back(hbox( - { - text(String(paletteIcon)) | color(ui::DEFAULT_THEME.icon), - CreateColorCircles(), - } - )); + content.push_back(hbox({ + text(String(paletteIcon)) | color(ui::DEFAULT_THEME.icon), + CreateColorCircles(), + })); content.push_back(separator() | color(ui::DEFAULT_THEME.border)); // Helper function for aligned rows fn createRow = [&](const auto& icon, const auto& label, const auto& value) { - return hbox( - { - text(String(icon)) | color(ui::DEFAULT_THEME.icon), - text(String(static_cast(label))) | color(ui::DEFAULT_THEME.label), - filler(), - text(String(value)) | color(ui::DEFAULT_THEME.value), - text(" "), - } - ); + return hbox({ + text(String(icon)) | color(ui::DEFAULT_THEME.icon), + text(String(static_cast(label))) | color(ui::DEFAULT_THEME.label), + filler(), + text(String(value)) | color(ui::DEFAULT_THEME.value), + text(" "), + }); }; // System info rows @@ -139,39 +130,31 @@ namespace { const WeatherOutput& weatherInfo = data.weather_info.value(); if (weather.show_town_name) - content.push_back(hbox( - { - text(String(weatherIcon)) | color(ui::DEFAULT_THEME.icon), - text("Weather") | color(ui::DEFAULT_THEME.label), - filler(), + content.push_back(hbox({ + text(String(weatherIcon)) | color(ui::DEFAULT_THEME.icon), + text("Weather") | color(ui::DEFAULT_THEME.label), + filler(), - hbox( - { - text(std::format("{}°F ", std::lround(weatherInfo.main.temp))), - text("in "), - text(weatherInfo.name), - text(" "), - } - ) | - color(ui::DEFAULT_THEME.value), - } - )); + hbox({ + text(std::format("{}°F ", std::lround(weatherInfo.main.temp))), + text("in "), + text(weatherInfo.name), + text(" "), + }) | + color(ui::DEFAULT_THEME.value), + })); else - content.push_back(hbox( - { - text(String(weatherIcon)) | color(ui::DEFAULT_THEME.icon), - text("Weather") | color(ui::DEFAULT_THEME.label), - filler(), + content.push_back(hbox({ + text(String(weatherIcon)) | color(ui::DEFAULT_THEME.icon), + text("Weather") | color(ui::DEFAULT_THEME.label), + filler(), - hbox( - { - text(std::format("{}°F, {}", std::lround(weatherInfo.main.temp), weatherInfo.weather[0].description)), - text(" "), - } - ) | - color(ui::DEFAULT_THEME.value), - } - )); + hbox({ + text(std::format("{}°F, {}", std::lround(weatherInfo.main.temp), weatherInfo.weather[0].description)), + text(" "), + }) | + color(ui::DEFAULT_THEME.value), + })); } content.push_back(separator() | color(ui::DEFAULT_THEME.border)); @@ -213,16 +196,14 @@ namespace { const String& npText = *nowPlayingResult; content.push_back(separator() | color(ui::DEFAULT_THEME.border)); - content.push_back(hbox( - { - text(String(musicIcon)) | color(ui::DEFAULT_THEME.icon), - text("Playing") | color(ui::DEFAULT_THEME.label), - text(" "), - filler(), - paragraph(npText) | color(Color::Magenta) | size(WIDTH, LESS_THAN, ui::MAX_PARAGRAPH_LENGTH), - text(" "), - } - )); + content.push_back(hbox({ + text(String(musicIcon)) | color(ui::DEFAULT_THEME.icon), + text("Playing") | color(ui::DEFAULT_THEME.label), + text(" "), + filler(), + paragraph(npText) | color(Color::Magenta) | size(WIDTH, LESS_THAN, ui::MAX_PARAGRAPH_LENGTH), + text(" "), + })); } else { const NowPlayingError& error = nowPlayingResult.error(); @@ -230,6 +211,7 @@ namespace { switch (std::get(error)) { case NowPlayingCode::NoPlayers: DEBUG_LOG("No players found"); break; case NowPlayingCode::NoActivePlayer: DEBUG_LOG("No active player found"); break; + default: std::unreachable(); } #ifdef _WIN32 diff --git a/src/os/macos.cpp b/src/os/macos.cpp index 2de837b..e125f65 100644 --- a/src/os/macos.cpp +++ b/src/os/macos.cpp @@ -9,7 +9,7 @@ #include "os.h" #include "src/util/types.h" -fn GetMemInfo() -> Result { +fn os::GetMemInfo() -> Result { u64 mem = 0; usize size = sizeof(mem); @@ -19,15 +19,15 @@ fn GetMemInfo() -> Result { return mem; } -fn GetNowPlaying() -> Result { return GetCurrentPlayingInfo(); } +fn os::GetNowPlaying() -> Result { return GetCurrentPlayingInfo(); } -fn GetOSVersion() -> Result { return GetMacOSVersion(); } +fn os::GetOSVersion() -> Result { return GetMacOSVersion(); } -fn GetDesktopEnvironment() -> Option { return None; } +fn os::GetDesktopEnvironment() -> Option { return None; } -fn GetWindowManager() -> String { return "Yabai"; } +fn os::GetWindowManager() -> String { return "Yabai"; } -fn GetKernelVersion() -> String { +fn os::GetKernelVersion() -> String { std::array kernelVersion {}; usize kernelVersionLen = sizeof(kernelVersion); @@ -35,7 +35,7 @@ fn GetKernelVersion() -> String { return kernelVersion.data(); } -fn GetHost() -> String { +fn os::GetHost() -> String { std::array hwModel {}; size_t hwModelLen = sizeof(hwModel); @@ -195,7 +195,7 @@ fn GetHost() -> String { return String(modelNameByHwModel[hwModel.data()]); } -fn GetDiskUsage() -> std::pair { +fn os::GetDiskUsage() -> std::pair { struct statvfs vfs; if (statvfs("/", &vfs) != 0) @@ -204,6 +204,6 @@ fn GetDiskUsage() -> std::pair { return { (vfs.f_blocks - vfs.f_bfree) * vfs.f_frsize, vfs.f_blocks * vfs.f_frsize }; } -fn GetShell() -> String { return ""; } +fn os::GetShell() -> String { return ""; } #endif diff --git a/src/os/macos/bridge.mm b/src/os/macos/bridge.mm index e95f679..8e6e368 100644 --- a/src/os/macos/bridge.mm +++ b/src/os/macos/bridge.mm @@ -95,15 +95,15 @@ extern "C++" { return; } - NSDictionary* metadata = *metadataResult; + const NSDictionary* const metadata = *metadataResult; if (!metadata) { result = std::unexpected(NowPlayingError { NowPlayingCode::NoPlayers }); dispatch_semaphore_signal(semaphore); return; } - NSString* title = metadata[@"kMRMediaRemoteNowPlayingInfoTitle"]; - NSString* artist = metadata[@"kMRMediaRemoteNowPlayingInfoArtist"]; + const NSString* const title = metadata[@"kMRMediaRemoteNowPlayingInfoTitle"]; + const NSString* const artist = metadata[@"kMRMediaRemoteNowPlayingInfoArtist"]; if (!title && !artist) result = std::unexpected(NowPlayingError { "No metadata" }); diff --git a/src/os/os.h b/src/os/os.h index 4709b29..aa0bc75 100644 --- a/src/os/os.h +++ b/src/os/os.h @@ -54,4 +54,5 @@ namespace os { * @return std::pair Used space/total space */ fn GetDiskUsage() -> Pair; -} \ No newline at end of file +} + diff --git a/src/util/macros.h b/src/util/macros.h index 0c51cc2..16b9dc9 100644 --- a/src/util/macros.h +++ b/src/util/macros.h @@ -28,24 +28,26 @@ namespace term { return static_cast(static_cast(emphA) | static_cast(emphB)); } + // clang-format off enum class Color : u8 { - black = 30, - red = 31, - green = 32, - yellow = 33, - blue = 34, - magenta = 35, - cyan = 36, - white = 37, - bright_black = 90, - bright_red = 91, - bright_green = 92, - bright_yellow = 93, - bright_blue = 94, - bright_magenta = 95, - bright_cyan = 96, - bright_white = 97 + black [[maybe_unused]] = 30, + red [[maybe_unused]] = 31, + green [[maybe_unused]] = 32, + yellow [[maybe_unused]] = 33, + blue [[maybe_unused]] = 34, + magenta [[maybe_unused]] = 35, + cyan [[maybe_unused]] = 36, + white [[maybe_unused]] = 37, + bright_black [[maybe_unused]] = 90, + bright_red [[maybe_unused]] = 91, + bright_green [[maybe_unused]] = 92, + bright_yellow [[maybe_unused]] = 93, + bright_blue [[maybe_unused]] = 94, + bright_magenta [[maybe_unused]] = 95, + bright_cyan [[maybe_unused]] = 96, + bright_white [[maybe_unused]] = 97 }; + // clang-format on struct FgColor { Color col;