Skip to content

Commit 5d5bfed

Browse files
committed
try fixing tracking of ccusage availability
1 parent 71a9cf8 commit 5d5bfed

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

src-tauri/src/lib.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ struct BlocksResponse {
4646
struct SessionData {
4747
active_block: Option<BlockData>,
4848
last_updated: Option<Instant>,
49+
ccusage_available: bool,
4950
}
5051

5152
static SESSION_CACHE: Mutex<SessionData> = Mutex::new(SessionData {
5253
active_block: None,
5354
last_updated: None,
55+
ccusage_available: false,
5456
});
5557

5658
// Removed AppSettings as we now always show cost
@@ -79,7 +81,7 @@ fn format_model_name(model_name: &str) -> String {
7981
}
8082
}
8183

82-
async fn fetch_session_data() -> Option<BlockData> {
84+
async fn fetch_session_data() -> (Option<BlockData>, bool) {
8385
// Try multiple approaches to find and run ccusage
8486
let shell_commands = vec![
8587
// Most likely to succeed: Try with explicit PATH that includes common npm locations
@@ -109,7 +111,9 @@ async fn fetch_session_data() -> Option<BlockData> {
109111
// Try to parse the response
110112
match serde_json::from_str::<BlocksResponse>(&stdout) {
111113
Ok(response) => {
112-
return response.blocks.into_iter().find(|block| block.is_active);
114+
// ccusage is working! Return the active block (if any) and true
115+
let active_block = response.blocks.into_iter().find(|block| block.is_active);
116+
return (active_block, true);
113117
}
114118
Err(e) => {
115119
eprintln!("Failed to parse ccusage response: {}", e);
@@ -131,7 +135,7 @@ async fn fetch_session_data() -> Option<BlockData> {
131135
}
132136

133137
eprintln!("All attempts to fetch session data failed");
134-
None
138+
(None, false)
135139
}
136140

137141
// Removed fetch_blocks_data and fetch_week_data functions as they are no longer needed
@@ -220,7 +224,7 @@ async fn refresh_session_data(app_handle: &tauri::AppHandle) {
220224
IS_REFRESHING.store(true, Ordering::Relaxed);
221225

222226
// Fetch active session data
223-
let active_block = fetch_session_data().await;
227+
let (active_block, ccusage_available) = fetch_session_data().await;
224228

225229
// Update tray title with cost if there's an active session
226230
let title = if let Some(ref block) = active_block {
@@ -234,6 +238,7 @@ async fn refresh_session_data(app_handle: &tauri::AppHandle) {
234238
let mut cache = SESSION_CACHE.lock().unwrap();
235239
cache.active_block = active_block;
236240
cache.last_updated = Some(Instant::now());
241+
cache.ccusage_available = ccusage_available;
237242
}
238243

239244
// Update tray title
@@ -261,9 +266,9 @@ async fn build_menu(app: &tauri::AppHandle) -> Result<tauri::menu::Menu<tauri::W
261266
menu_builder = menu_builder.item(&ccusage_header).separator();
262267

263268
// Get data from cache
264-
let (active_block, has_attempted_fetch) = {
269+
let (active_block, has_attempted_fetch, ccusage_available) = {
265270
let cache = SESSION_CACHE.lock().unwrap();
266-
(cache.active_block.clone(), cache.last_updated.is_some())
271+
(cache.active_block.clone(), cache.last_updated.is_some(), cache.ccusage_available)
267272
};
268273

269274
// Current session section
@@ -323,20 +328,25 @@ async fn build_menu(app: &tauri::AppHandle) -> Result<tauri::menu::Menu<tauri::W
323328

324329
menu_builder = menu_builder.separator();
325330
} else if has_attempted_fetch {
326-
// We've tried to fetch but got no data - likely ccusage is not available
331+
// We've tried to fetch
327332
let no_session = MenuItemBuilder::with_id("no_session", "No active session")
328333
.build(app)?;
329334
menu_builder = menu_builder.item(&no_session);
330335

331-
// Add helpful error message
332-
let error_msg = MenuItemBuilder::with_id("error_msg", "ccusage may not be installed")
333-
.enabled(false)
334-
.build(app)?;
335-
menu_builder = menu_builder.item(&error_msg);
336+
// Only show error if ccusage is actually not available
337+
if !ccusage_available {
338+
// Add helpful error message
339+
let error_msg = MenuItemBuilder::with_id("error_msg", "ccusage may not be installed")
340+
.enabled(false)
341+
.build(app)?;
342+
menu_builder = menu_builder.item(&error_msg);
343+
344+
let install_msg = MenuItemBuilder::with_id("install_msg", "Install: npm install -g ccusage")
345+
.build(app)?;
346+
menu_builder = menu_builder.item(&install_msg);
347+
}
336348

337-
let install_msg = MenuItemBuilder::with_id("install_msg", "Install: npm install -g ccusage")
338-
.build(app)?;
339-
menu_builder = menu_builder.item(&install_msg).separator();
349+
menu_builder = menu_builder.separator();
340350
} else {
341351
// Still loading
342352
let loading = MenuItemBuilder::with_id("loading", "Loading...")

0 commit comments

Comments
 (0)