Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions bins/bounty-cli/src/tui/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ struct App {
entries: Vec<LeaderboardEntry>,
scroll_offset: usize,
error: Option<String>,
visibility_changed: bool,
initial_visibility_notified: bool,
}

fn parse_entries(data: &Value) -> Vec<LeaderboardEntry> {
Expand Down Expand Up @@ -137,6 +139,8 @@ pub async fn run(rpc_url: &str) -> Result<()> {
entries: vec![],
scroll_offset: 0,
error: None,
visibility_changed: false,
initial_visibility_notified: false,
};

let mut last_fetch = Instant::now() - Duration::from_secs(10);
Expand All @@ -163,19 +167,27 @@ pub async fn run(rpc_url: &str) -> Result<()> {
KeyCode::Char('q') | KeyCode::Esc => break,
KeyCode::Up | KeyCode::Char('k') => {
app.scroll_offset = app.scroll_offset.saturating_sub(1);
app.visibility_changed = true;
}
KeyCode::Down | KeyCode::Char('j') => {
if app.scroll_offset + 1 < app.entries.len() {
app.scroll_offset += 1;
app.visibility_changed = true;
}
}
_ => {}
}
}
}
}

if app.visibility_changed && !app.initial_visibility_notified {
app.initial_visibility_notified = true;
// Call onVisibilityChange handler here if needed
} else if app.visibility_changed {
// Call onVisibilityChange handler here if needed
app.visibility_changed = false;
}
}

super::restore_terminal(&mut terminal)?;
Ok(())
}
super::restore_terminal(&mut terminal)
39 changes: 39 additions & 0 deletions bins/bounty-cli/src/tui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crossterm::{
};
use ratatui::prelude::*;
use std::io;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

pub fn setup_terminal() -> Result<Terminal<CrosstermBackend<io::Stdout>>> {
enable_raw_mode()?;
Expand All @@ -25,3 +27,40 @@ pub fn restore_terminal(terminal: &mut Terminal<CrosstermBackend<io::Stdout>>) -
terminal.show_cursor()?;
Ok(())
}

pub struct AuxiliaryBar {
visible: bool,
on_visibility_change: Option<Box<dyn Fn(bool) + Send + Sync>>,
initial_run: Arc<AtomicBool>,
}

impl AuxiliaryBar {
pub fn new() -> Self {
Self {
visible: false,
on_visibility_change: None,
initial_run: Arc::new(AtomicBool::new(true)),
}
}

pub fn set_on_visibility_change(&mut self, callback: impl Fn(bool) + Send + Sync + 'static) {
self.on_visibility_change = Some(Box::new(callback));
}

pub fn set_visible(&mut self, visible: bool) {
if self.visible != visible {
self.visible = visible;
if let Some(callback) = &self.on_visibility_change {
if !*self.initial_run.load(Ordering::SeqCst) {
callback(visible);
} else {
self.initial_run.store(false, Ordering::SeqCst);
}
}
}
}

pub fn visible(&self) -> bool {
self.visible
}
}
16 changes: 13 additions & 3 deletions bins/bounty-cli/src/tui/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ fn stat_block<'a>(label: &'a str, value: u64, color: Color) -> Paragraph<'a> {
)
}

fn ui(frame: &mut Frame, stats: &StatsData, error: &Option<String>) {
fn ui(frame: &mut Frame, stats: &StatsData, error: &Option<String>, initial_render: bool) {
let outer = Layout::default()
.direction(Direction::Vertical)
.constraints([
Expand Down Expand Up @@ -117,13 +117,19 @@ fn ui(frame: &mut Frame, stats: &StatsData, error: &Option<String>) {
.style(Style::default().fg(Color::DarkGray))
.block(Block::default().borders(Borders::ALL));
frame.render_widget(help, outer[2]);

if !initial_render {
// Call onVisibilityChange handler here if needed
}
}

pub async fn run(rpc_url: &str) -> Result<()> {
let mut terminal = super::setup_terminal()?;
let mut stats = StatsData::default();
let mut error: Option<String> = None;
let mut last_fetch = Instant::now() - Duration::from_secs(10);
let mut initial_render = true;
let mut visible = true;

loop {
if last_fetch.elapsed() >= Duration::from_secs(5) {
Expand All @@ -137,7 +143,8 @@ pub async fn run(rpc_url: &str) -> Result<()> {
last_fetch = Instant::now();
}

terminal.draw(|f| ui(f, &stats, &error))?;
terminal.draw(|f| ui(f, &stats, &error, initial_render))?;
initial_render = false;

if event::poll(Duration::from_millis(100))? {
if let Event::Key(key) = event::read()? {
Expand All @@ -148,8 +155,11 @@ pub async fn run(rpc_url: &str) -> Result<()> {
}
}
}
if visible {
// Call onVisibilityChange handler here if needed
}
}

super::restore_terminal(&mut terminal)?;
Ok(())
}
}