ui.rs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. use ratatui::{
  2. prelude::{Constraint, Layout, Rect},
  3. style::{Color, Modifier, Style},
  4. symbols,
  5. text::{self, Span, Text},
  6. widgets::{Block, Borders, List, ListItem, Sparkline, Tabs},
  7. Frame,
  8. };
  9. use crate::app::App;
  10. /// Renders the user interface widgets.
  11. pub fn render(app: &mut App, frame: &mut Frame) {
  12. // This is where you add new widgets.
  13. // See the following resources:
  14. // - https://docs.rs/ratatui/latest/ratatui/widgets/index.html
  15. // - https://github.com/ratatui-org/ratatui/tree/master/examples
  16. // frame.render_widget(
  17. // Paragraph::new(format!(
  18. // "This is a tui template.\n\
  19. // Press `Esc`, `Ctrl-C` or `q` to stop running.\n\
  20. // Press left and right to increment and decrement the counter respectively.\n\
  21. // Counter: {}",
  22. // app.counter
  23. // ))
  24. // .block(
  25. // Block::default()
  26. // .title("Template")
  27. // .title_alignment(Alignment::Center)
  28. // .borders(Borders::ALL)
  29. // .border_type(BorderType::Rounded),
  30. // )
  31. // .style(Style::default().fg(Color::Cyan).bg(Color::Black))
  32. // .alignment(Alignment::Center),
  33. // frame.size(),
  34. // )
  35. let chunks = Layout::default()
  36. .constraints([Constraint::Length(3), Constraint::Min(0)])
  37. .split(frame.size());
  38. let titles = app
  39. .tabs
  40. .titles
  41. .iter()
  42. .map(|t| text::Line::from(Span::styled(*t, Style::default().fg(Color::Green))))
  43. .collect();
  44. let tabs = Tabs::new(titles)
  45. .block(Block::default().borders(Borders::ALL).title(app.title))
  46. .highlight_style(Style::default().fg(Color::Yellow))
  47. .select(app.tabs.index);
  48. frame.render_widget(tabs, chunks[0]);
  49. match app.tabs.index {
  50. 0 => draw_first_tab(frame, app, chunks[1]),
  51. _ => {}
  52. }
  53. }
  54. fn draw_first_tab(f: &mut Frame, app: &mut App, area: Rect) {
  55. let chunks = Layout::default()
  56. .constraints([
  57. Constraint::Min(1),
  58. Constraint::Min(3),
  59. Constraint::Length(7),
  60. ])
  61. .split(area);
  62. draw_memory_logging_speed_gauges(f, app, chunks[0]);
  63. // draw_charts(f, app, chunks[1]);
  64. draw_footer(f, app, chunks[2]);
  65. }
  66. /// 绘制内存日志产生数量的图表
  67. fn draw_memory_logging_speed_gauges(f: &mut Frame, app: &mut App, area: Rect) {
  68. let chunks = Layout::default()
  69. .constraints([Constraint::Length(3)])
  70. .margin(1)
  71. .split(area);
  72. let block = Block::default().borders(Borders::ALL).title("Speed:");
  73. f.render_widget(block, area);
  74. let sparkline = Sparkline::default()
  75. .block(Block::default().title("Memory Log Speed:"))
  76. .style(Style::default().fg(Color::Green))
  77. .data(&app.memory_log_sparkline.points)
  78. .bar_set(if app.enhanced_graphics {
  79. symbols::bar::NINE_LEVELS
  80. } else {
  81. symbols::bar::THREE_LEVELS
  82. });
  83. f.render_widget(sparkline, chunks[0]);
  84. }
  85. fn draw_footer(f: &mut Frame, app: &mut App, area: Rect) {
  86. let _block = Block::default().borders(Borders::ALL).title(Span::styled(
  87. "Logs",
  88. Style::default()
  89. .fg(Color::Magenta)
  90. .add_modifier(Modifier::BOLD),
  91. ));
  92. let info_style = Style::default().fg(Color::Blue);
  93. let warning_style = Style::default().fg(Color::Yellow);
  94. let error_style = Style::default().fg(Color::Magenta);
  95. let critical_style = Style::default().fg(Color::Red);
  96. let binding = app.logs().clone();
  97. let log_list = binding
  98. .iter()
  99. .map(|log_str| {
  100. let _style = match log_str {
  101. log if log.contains("INFO") => info_style,
  102. log if log.contains("WARNING") => warning_style,
  103. log if log.contains("ERROR") => error_style,
  104. log if log.contains("CRITICAL") => critical_style,
  105. _ => Style::default().fg(Color::White),
  106. };
  107. // println!("log_str: {}", log_str);
  108. ListItem::new(Text::from(log_str.clone()))
  109. })
  110. .collect::<Vec<ListItem>>();
  111. let items_num = 5;
  112. let list_to_show = log_list.split_at(if log_list.len() > items_num {
  113. log_list.len() - items_num
  114. } else {
  115. 0
  116. });
  117. let logs =
  118. List::new(list_to_show.1).block(Block::default().borders(Borders::ALL).title("List"));
  119. f.render_stateful_widget(logs, area, &mut app.stateful_logs.state);
  120. }