ipfrs-cli 0.2.0

Command-line interface for IPFRS distributed content-addressed storage
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Terminal User Interface (TUI) for IPFRS
//!
//! Provides an interactive dashboard for monitoring IPFRS node status,
//! network activity, storage statistics, and more.

use anyhow::Result;
use crossterm::{
    event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
    backend::{Backend, CrosstermBackend},
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Gauge, List, ListItem, Paragraph, Sparkline, Tabs, Wrap},
    Frame, Terminal,
};
use std::io;
use std::time::{Duration, Instant};

/// TUI application state
pub struct App {
    /// Current tab index
    current_tab: usize,
    /// Whether the app should quit
    should_quit: bool,
    /// Node statistics
    stats: NodeStats,
    /// Network activity history (for sparkline)
    network_history: Vec<u64>,
    /// Last update time
    last_update: Instant,
}

/// Node statistics
#[derive(Debug, Clone, Default)]
struct NodeStats {
    /// Peer count
    peer_count: usize,
    /// Total blocks stored
    block_count: u64,
    /// Total storage size in bytes
    storage_size: u64,
    /// Bandwidth in/out (bytes per second)
    bandwidth_in: u64,
    bandwidth_out: u64,
    /// Uptime in seconds
    uptime: u64,
    /// Number of pinned items
    pinned_count: usize,
    /// DHT routing table size
    dht_size: usize,
}

impl Default for App {
    fn default() -> Self {
        Self {
            current_tab: 0,
            should_quit: false,
            stats: NodeStats::default(),
            network_history: vec![0; 60],
            last_update: Instant::now(),
        }
    }
}

impl App {
    /// Create a new TUI app
    pub fn new() -> Self {
        Self::default()
    }

    /// Handle key events
    fn handle_key_event(&mut self, key: event::KeyEvent) {
        match (key.code, key.modifiers) {
            (KeyCode::Char('q'), _) | (KeyCode::Char('c'), KeyModifiers::CONTROL) => {
                self.should_quit = true;
            }
            (KeyCode::Right | KeyCode::Tab, _) => {
                self.current_tab = (self.current_tab + 1) % 4;
            }
            (KeyCode::Left, _) => {
                self.current_tab = if self.current_tab > 0 {
                    self.current_tab - 1
                } else {
                    3
                };
            }
            (KeyCode::Char('1'), _) => self.current_tab = 0,
            (KeyCode::Char('2'), _) => self.current_tab = 1,
            (KeyCode::Char('3'), _) => self.current_tab = 2,
            (KeyCode::Char('4'), _) => self.current_tab = 3,
            _ => {}
        }
    }

    /// Update statistics (mock data for now)
    fn update_stats(&mut self) {
        // In a real implementation, this would fetch data from the IPFRS node
        // For now, we'll use mock data that changes over time

        let elapsed = self.last_update.elapsed();
        if elapsed >= Duration::from_secs(1) {
            self.stats.uptime += elapsed.as_secs();
            self.last_update = Instant::now();

            // Simulate changing stats
            use std::time::SystemTime;
            let seed = SystemTime::now()
                .duration_since(SystemTime::UNIX_EPOCH)
                .expect("system time is after UNIX epoch")
                .as_secs();

            self.stats.peer_count = ((seed % 10) + 5) as usize;
            self.stats.bandwidth_in = (seed % 1000) * 1024;
            self.stats.bandwidth_out = (seed % 500) * 1024;

            // Update network history
            self.network_history.remove(0);
            self.network_history.push(self.stats.bandwidth_in / 1024);
        }
    }
}

/// Run the TUI application
pub async fn run_tui() -> Result<()> {
    // Setup terminal
    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    // Create app state
    let mut app = App::new();

    // Run the main loop
    let res = run_app(&mut terminal, &mut app).await;

    // Restore terminal
    disable_raw_mode()?;
    execute!(
        terminal.backend_mut(),
        LeaveAlternateScreen,
        DisableMouseCapture
    )?;
    terminal.show_cursor()?;

    res
}

/// Main application loop
async fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> Result<()>
where
    <B as Backend>::Error: Send + Sync + 'static,
{
    loop {
        // Update stats
        app.update_stats();

        // Draw UI
        terminal.draw(|f| ui(f, app))?;

        // Handle events with timeout
        if event::poll(Duration::from_millis(100))? {
            if let Event::Key(key) = event::read()? {
                app.handle_key_event(key);
            }
        }

        if app.should_quit {
            break;
        }
    }

    Ok(())
}

/// Draw the UI
fn ui(f: &mut Frame, app: &App) {
    let size = f.area();

    // Create main layout
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Min(0),
            Constraint::Length(3),
        ])
        .split(size);

    // Draw tabs
    draw_tabs(f, chunks[0], app);

    // Draw content based on current tab
    match app.current_tab {
        0 => draw_overview(f, chunks[1], app),
        1 => draw_network(f, chunks[1], app),
        2 => draw_storage(f, chunks[1], app),
        3 => draw_help(f, chunks[1]),
        _ => {}
    }

    // Draw footer
    draw_footer(f, chunks[2], app);
}

/// Draw the tab bar
fn draw_tabs(f: &mut Frame, area: Rect, app: &App) {
    let titles = vec!["Overview", "Network", "Storage", "Help"];
    let tabs = Tabs::new(titles)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(" IPFRS Dashboard "),
        )
        .select(app.current_tab)
        .style(Style::default().fg(Color::White))
        .highlight_style(
            Style::default()
                .fg(Color::Yellow)
                .add_modifier(Modifier::BOLD),
        );
    f.render_widget(tabs, area);
}

/// Draw the overview tab
fn draw_overview(f: &mut Frame, area: Rect, app: &App) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3),
            Constraint::Length(3),
            Constraint::Length(3),
            Constraint::Length(3),
            Constraint::Min(0),
        ])
        .split(area);

    // Peer gauge
    let peer_ratio = app.stats.peer_count as f64 / 50.0;
    let peer_gauge = Gauge::default()
        .block(Block::default().borders(Borders::ALL).title(" Peers "))
        .gauge_style(Style::default().fg(Color::Green))
        .percent((peer_ratio * 100.0).min(100.0) as u16)
        .label(format!("{} / 50", app.stats.peer_count));
    f.render_widget(peer_gauge, chunks[0]);

    // Storage gauge
    let storage_ratio = app.stats.storage_size as f64 / (10_u64 * 1024 * 1024 * 1024) as f64;
    let storage_gauge = Gauge::default()
        .block(Block::default().borders(Borders::ALL).title(" Storage "))
        .gauge_style(Style::default().fg(Color::Blue))
        .percent((storage_ratio * 100.0).min(100.0) as u16)
        .label(format_bytes(app.stats.storage_size));
    f.render_widget(storage_gauge, chunks[1]);

    // Bandwidth in
    let bw_in = Paragraph::new(format!(
        "Incoming: {} /s",
        format_bytes(app.stats.bandwidth_in)
    ))
    .block(Block::default().borders(Borders::ALL).title(" Bandwidth "));
    f.render_widget(bw_in, chunks[2]);

    // Bandwidth out
    let bw_out = Paragraph::new(format!(
        "Outgoing: {} /s",
        format_bytes(app.stats.bandwidth_out)
    ));
    f.render_widget(bw_out, chunks[3]);

    // Summary
    let uptime_hours = app.stats.uptime / 3600;
    let uptime_mins = (app.stats.uptime % 3600) / 60;
    let summary = [
        format!("Uptime: {}h {}m", uptime_hours, uptime_mins),
        format!("Blocks: {}", app.stats.block_count),
        format!("Pinned: {}", app.stats.pinned_count),
        format!("DHT Size: {}", app.stats.dht_size),
    ];
    let summary_widget = Paragraph::new(summary.join("\n"))
        .block(Block::default().borders(Borders::ALL).title(" Node Info "))
        .wrap(Wrap { trim: true });
    f.render_widget(summary_widget, chunks[4]);
}

/// Draw the network tab
fn draw_network(f: &mut Frame, area: Rect, app: &App) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(10), Constraint::Min(0)])
        .split(area);

    // Network activity sparkline
    let sparkline = Sparkline::default()
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(" Network Activity (KB/s) "),
        )
        .data(&app.network_history)
        .style(Style::default().fg(Color::Cyan));
    f.render_widget(sparkline, chunks[0]);

    // Connected peers list (placeholder)
    let peers: Vec<ListItem> = vec![
        ListItem::new("QmPeer1... - /ip4/192.168.1.100/tcp/4001"),
        ListItem::new("QmPeer2... - /ip4/10.0.0.50/udp/4001/quic-v1"),
        ListItem::new("QmPeer3... - /ip6/::1/tcp/4001"),
    ];
    let peer_list = List::new(peers).block(
        Block::default()
            .borders(Borders::ALL)
            .title(format!(" Connected Peers ({}) ", app.stats.peer_count)),
    );
    f.render_widget(peer_list, chunks[1]);
}

/// Draw the storage tab
fn draw_storage(f: &mut Frame, area: Rect, app: &App) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(5),
            Constraint::Length(5),
            Constraint::Min(0),
        ])
        .split(area);

    // Storage breakdown
    let storage_info = [
        format!("Total Size: {}", format_bytes(app.stats.storage_size)),
        format!("Block Count: {}", app.stats.block_count),
        format!("Pinned Items: {}", app.stats.pinned_count),
    ];
    let storage_widget = Paragraph::new(storage_info.join("\n"))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(" Storage Info "),
        )
        .wrap(Wrap { trim: true });
    f.render_widget(storage_widget, chunks[0]);

    // Recent blocks (placeholder)
    let blocks: Vec<ListItem> = vec![
        ListItem::new("QmHash1... - 1.2 MB - 2 mins ago"),
        ListItem::new("QmHash2... - 534 KB - 5 mins ago"),
        ListItem::new("QmHash3... - 2.1 GB - 10 mins ago"),
    ];
    let block_list = List::new(blocks).block(
        Block::default()
            .borders(Borders::ALL)
            .title(" Recent Blocks "),
    );
    f.render_widget(block_list, chunks[1]);

    // Cache stats
    let cache_info = [
        "Cache Hit Rate: 87.3%",
        "Cache Size: 100 MB / 256 MB",
        "Evictions: 1,234",
    ];
    let cache_widget = Paragraph::new(cache_info.join("\n"))
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(" Cache Stats "),
        )
        .wrap(Wrap { trim: true });
    f.render_widget(cache_widget, chunks[2]);
}

/// Draw the help tab
fn draw_help(f: &mut Frame, area: Rect) {
    let help_text = vec![
        Line::from(vec![
            Span::styled(
                "q",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(" - Quit"),
        ]),
        Line::from(vec![
            Span::styled(
                "Tab / ←/→",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(" - Switch tabs"),
        ]),
        Line::from(vec![
            Span::styled(
                "1-4",
                Style::default()
                    .fg(Color::Yellow)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(" - Jump to tab"),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled("Overview", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw(" - Node statistics and gauges"),
        ]),
        Line::from(vec![
            Span::styled("Network", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw(" - Peer connections and activity"),
        ]),
        Line::from(vec![
            Span::styled("Storage", Style::default().add_modifier(Modifier::BOLD)),
            Span::raw(" - Block storage and cache stats"),
        ]),
        Line::from(""),
        Line::from("Press Ctrl+C or q to exit the dashboard."),
    ];

    let help = Paragraph::new(help_text)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(" Help & Keyboard Shortcuts "),
        )
        .alignment(Alignment::Left)
        .wrap(Wrap { trim: true });
    f.render_widget(help, area);
}

/// Draw the footer
fn draw_footer(f: &mut Frame, area: Rect, app: &App) {
    let footer_text = format!(
        " IPFRS v0.2.0 | Peers: {} | Blocks: {} | Press 'q' to quit ",
        app.stats.peer_count, app.stats.block_count
    );
    let footer = Paragraph::new(footer_text)
        .style(Style::default().fg(Color::White).bg(Color::DarkGray))
        .alignment(Alignment::Center);
    f.render_widget(footer, area);
}

/// Format bytes to human-readable string
fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];

    if bytes == 0 {
        return "0 B".to_string();
    }

    let mut size = bytes as f64;
    let mut unit_index = 0;

    while size >= 1024.0 && unit_index < UNITS.len() - 1 {
        size /= 1024.0;
        unit_index += 1;
    }

    if unit_index == 0 {
        format!("{} {}", bytes, UNITS[unit_index])
    } else {
        format!("{:.2} {}", size, UNITS[unit_index])
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(0), "0 B");
        assert_eq!(format_bytes(500), "500 B");
        assert_eq!(format_bytes(1024), "1.00 KB");
        assert_eq!(format_bytes(1536), "1.50 KB");
        assert_eq!(format_bytes(1048576), "1.00 MB");
        assert_eq!(format_bytes(1073741824), "1.00 GB");
    }

    #[test]
    fn test_app_creation() {
        let app = App::new();
        assert_eq!(app.current_tab, 0);
        assert!(!app.should_quit);
        assert_eq!(app.stats.peer_count, 0);
    }

    #[test]
    fn test_tab_navigation() {
        let mut app = App::new();

        // Test right navigation
        app.handle_key_event(event::KeyEvent::from(KeyCode::Right));
        assert_eq!(app.current_tab, 1);

        app.handle_key_event(event::KeyEvent::from(KeyCode::Right));
        assert_eq!(app.current_tab, 2);

        // Test wraparound
        app.current_tab = 3;
        app.handle_key_event(event::KeyEvent::from(KeyCode::Right));
        assert_eq!(app.current_tab, 0);

        // Test left navigation
        app.handle_key_event(event::KeyEvent::from(KeyCode::Left));
        assert_eq!(app.current_tab, 3);
    }

    #[test]
    fn test_quit_key() {
        let mut app = App::new();

        app.handle_key_event(event::KeyEvent::from(KeyCode::Char('q')));
        assert!(app.should_quit);
    }

    #[test]
    fn test_direct_tab_selection() {
        let mut app = App::new();

        app.handle_key_event(event::KeyEvent::from(KeyCode::Char('3')));
        assert_eq!(app.current_tab, 2);

        app.handle_key_event(event::KeyEvent::from(KeyCode::Char('1')));
        assert_eq!(app.current_tab, 0);
    }
}