Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Statistics call changes + UI enhancements #1

Merged
merged 16 commits into from
Jan 18, 2022
Merged
44 changes: 14 additions & 30 deletions src/layout/mempool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use itertools::Itertools;

use crate::model::{EndorsementState, StateRef, UiState};

use super::create_pages_tabs;
use super::{create_header_bar, create_help_bar, create_pages_tabs};
pub struct MempoolScreen {}

impl MempoolScreen {
Expand All @@ -25,12 +25,17 @@ impl MempoolScreen {
) {
let data_state = data_state.read().unwrap();
let size = f.size();
let delta_toggle = ui_state.delta_toggle;

// TODO: placeholder for mempool page
let chunks = Layout::default()
let page_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Min(5), Constraint::Length(3)])
.constraints([
Constraint::Min(5),
Constraint::Length(3),
Constraint::Length(4),
])
.split(size);

let (header_chunk, summary_chunk, endorsements_chunk) = Layout::default()
Expand All @@ -40,38 +45,14 @@ impl MempoolScreen {
Constraint::Length(4),
Constraint::Min(1),
])
.split(chunks[0])
.split(page_chunks[0])
.into_iter()
.collect_tuple()
.unwrap(); // safe as we specify 3 elements in constraints and collecting into tuple of size 3

// ======================== HEADER ========================
// wrap the header chunk in border
let block = Block::default().borders(Borders::ALL).title("Current Head");
f.render_widget(block, header_chunk);

let header = &data_state.current_head_header;

let header_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Min(1), Constraint::Min(1), Constraint::Min(1)])
.split(header_chunk);

let block_hash = Paragraph::new(Spans::from(format!("Block hash: {}", header.hash)))
.block(Block::default())
.alignment(Alignment::Left);
f.render_widget(block_hash, header_chunks[0]);

let block_level = Paragraph::new(format!("Level: {}", header.level))
.block(Block::default())
.alignment(Alignment::Left);
f.render_widget(block_level, header_chunks[1]);

let block_protocol = Paragraph::new(format!("Protocol: {}", header.protocol))
.block(Block::default())
.alignment(Alignment::Left);
f.render_widget(block_protocol, header_chunks[2]);
create_header_bar(header_chunk, header, f);

// ======================== SUMARY ========================
let summary_elements_constraits = std::iter::repeat(Constraint::Percentage(16))
Expand Down Expand Up @@ -193,6 +174,9 @@ impl MempoolScreen {

// ======================== PAGES TABS ========================
let tabs = create_pages_tabs(ui_state);
f.render_widget(tabs, chunks[1]);
f.render_widget(tabs, page_chunks[1]);

// ======================== HELP BAR ========================
create_help_bar(page_chunks[2], f, delta_toggle);
}
}
108 changes: 106 additions & 2 deletions src/layout/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod syncing;
use itertools::Itertools;
pub use syncing::*;

pub mod mempool;
Expand All @@ -9,12 +10,15 @@ pub use statistics::*;

use strum::IntoEnumIterator;
use tui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style},
text::{Span, Spans},
widgets::{Block, Borders, Tabs},
widgets::{Block, Borders, Paragraph, Tabs},
Frame,
};

use crate::model::{ActivePage, UiState};
use crate::model::{ActivePage, CurrentHeadHeader, UiState};

pub fn create_pages_tabs(ui_state: &UiState) -> Tabs {
let titles = ActivePage::iter()
Expand All @@ -37,3 +41,103 @@ pub fn create_pages_tabs(ui_state: &UiState) -> Tabs {
.highlight_style(Style::default().fg(Color::Black).bg(Color::Gray))
.select(page_in_focus)
}

pub fn create_help_bar<B: Backend>(help_chunk: Rect, f: &mut Frame<B>, delta_toggle: bool) {
let help_strings = vec![
("F1 - F3", "PageSwitch"),
("q", "Quit"),
("TAB", "Rotate widgets"),
(
"d",
if delta_toggle {
"Concrete values"
} else {
"Delta values"
},
),
("j", "Switch sort left"),
("k", "Switch sort right"),
("←", "Table left"),
("→", "Table right"),
("↑", "Table up"),
("↓", "Table down"),
];

let help_chunks = Layout::default()
.direction(Direction::Horizontal)
.margin(1)
.constraints([
Constraint::Length(24),
Constraint::Length(24),
Constraint::Length(24),
Constraint::Length(24),
Constraint::Length(24),
])
.split(help_chunk);

for (index, (row_1, row_2)) in help_strings.iter().tuple_windows().step_by(2).enumerate() {
let p = Paragraph::new(vec![
Spans::from(vec![
Span::styled(
format!(" {} ", row_1.0),
Style::default().bg(Color::White).fg(Color::Black),
),
Span::styled(format!(" {}\n", row_1.1), Style::default()),
]),
Spans::from(vec![
Span::styled(
format!(" {} ", row_2.0),
Style::default().bg(Color::White).fg(Color::Black),
),
Span::styled(format!(" {}\n", row_2.1), Style::default()),
]),
]);
f.render_widget(p, help_chunks[index]);
}
}

pub fn create_header_bar<B: Backend>(
header_chunk: Rect,
header: &CurrentHeadHeader,
f: &mut Frame<B>,
) {
// wrap the header info in borders
let block = Block::default().borders(Borders::ALL).title("Current Head");
f.render_widget(block, header_chunk);

let header_chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints([Constraint::Min(1), Constraint::Min(1), Constraint::Min(1)])
.split(header_chunk);

let block_hash = Paragraph::new(Spans::from(vec![
Span::styled("Block hash: ", Style::default().fg(Color::Gray)),
Span::styled(
format!("{} ", header.hash),
Style::default().fg(Color::Reset),
),
]));

f.render_widget(block_hash, header_chunks[0]);

let block_level = Paragraph::new(Spans::from(vec![
Span::styled("Level: ", Style::default().fg(Color::Gray)),
Span::styled(
format!("{} ", header.level),
Style::default().fg(Color::Reset),
),
]));

f.render_widget(block_level, header_chunks[1]);

let block_protocol = Paragraph::new(Spans::from(vec![
Span::styled("Protocol: ", Style::default().fg(Color::Gray)),
Span::styled(
format!("{} ", header.protocol),
Style::default().fg(Color::Reset),
),
]));

f.render_widget(block_protocol, header_chunks[2]);
}
Loading