skill_web/utils/
mod.rs

1//! Utility functions and helpers
2
3/// Format a duration in milliseconds to a human-readable string
4pub fn format_duration(ms: u64) -> String {
5    if ms < 1000 {
6        format!("{}ms", ms)
7    } else if ms < 60000 {
8        format!("{:.1}s", ms as f64 / 1000.0)
9    } else {
10        let minutes = ms / 60000;
11        let seconds = (ms % 60000) / 1000;
12        format!("{}m {}s", minutes, seconds)
13    }
14}
15
16/// Format a timestamp to a relative time string
17pub fn format_relative_time(timestamp: &str) -> String {
18    // Placeholder - will be implemented with proper time parsing
19    timestamp.to_string()
20}
21
22/// Truncate a string to a maximum length with ellipsis
23pub fn truncate(s: &str, max_len: usize) -> String {
24    if s.len() <= max_len {
25        s.to_string()
26    } else {
27        format!("{}...", &s[..max_len.saturating_sub(3)])
28    }
29}
30
31/// Copy text to clipboard
32pub async fn copy_to_clipboard(text: &str) -> Result<(), String> {
33    let window = web_sys::window().ok_or("No window")?;
34    let navigator = window.navigator();
35    let clipboard = navigator.clipboard();
36
37    wasm_bindgen_futures::JsFuture::from(clipboard.write_text(text))
38        .await
39        .map_err(|_| "Failed to copy to clipboard")?;
40
41    Ok(())
42}