MCPcopy Create free account
hub / github.com/5ocworkshop/bgraph / main

Function main

examples/simple.rs:33–89  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

31}
32
33fn main() -> Result<(), Box<dyn std::error::Error>> {
34 // Setup terminal
35 enable_raw_mode()?;
36 let mut stdout = io::stdout();
37 execute!(stdout, EnterAlternateScreen)?;
38 let backend = CrosstermBackend::new(stdout);
39 let mut terminal = Terminal::new(backend)?;
40
41 let mut time = 0.0_f32;
42 let mut paused = false;
43
44 loop {
45 terminal.draw(|f| {
46 let chunks = Layout::default()
47 .direction(Direction::Vertical)
48 .constraints([Constraint::Percentage(100)])
49 .split(f.area());
50
51 let sine = SineWave { frequency: 2.0 };
52 let graph = Graph::new(&sine)
53 .x_range(time, time + 4.0)
54 .y_range(-1.5, 1.5)
55 .style(Style::default().fg(Color::Cyan));
56
57 let block = Block::default()
58 .title(format!(
59 "Sine Wave {} | Press 'q' to quit, Space to pause",
60 if paused { "[PAUSED]" } else { "" }
61 ))
62 .borders(Borders::ALL);
63
64 f.render_widget(block, chunks[0]);
65 f.render_widget(graph, chunks[0].inner(ratatui::layout::Margin::new(1, 1)));
66 })?;
67
68 if event::poll(std::time::Duration::from_millis(16))? {
69 if let Event::Key(key) = event::read()? {
70 match key.code {
71 KeyCode::Char('q') => break,
72 KeyCode::Char(' ') => paused = !paused,
73 _ => {}
74 }
75 }
76 }
77
78 if !paused {
79 time += 0.016;
80 }
81 }
82
83 // Restore terminal
84 disable_raw_mode()?;
85 execute!(terminal.backend_mut(), LeaveAlternateScreen)?;
86 terminal.show_cursor()?;
87
88 Ok(())
89}
90

Callers

nothing calls this directly

Calls 3

styleMethod · 0.45
y_rangeMethod · 0.45
x_rangeMethod · 0.45

Tested by

no test coverage detected