Limit framerate

This commit is contained in:
Thomas Forgione 2023-10-25 22:27:39 +02:00
parent 174477c756
commit dad76ebd37
2 changed files with 13 additions and 2 deletions

View File

@ -1,5 +1,6 @@
use std::io::{self, stdin, stdout, Write}; use std::io::{self, stdin, stdout, Write};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::time::{Duration, Instant};
use std::{env, thread}; use std::{env, thread};
use termion::event::{Event, Key, MouseButton, MouseEvent}; use termion::event::{Event, Key, MouseButton, MouseEvent};
@ -27,6 +28,9 @@ struct Multiview<W: Write> {
/// Whether we need to refresh the UI. /// Whether we need to refresh the UI.
pub refresh_ui: bool, pub refresh_ui: bool,
/// Last time when the rendering was performed.
pub last_render: Instant,
} }
impl<W: Write> Multiview<W> { impl<W: Write> Multiview<W> {
@ -37,6 +41,7 @@ impl<W: Write> Multiview<W> {
tiles, tiles,
selected: (0, 0), selected: (0, 0),
refresh_ui: true, refresh_ui: true,
last_render: Instant::now(),
}; };
write!( write!(
@ -85,6 +90,13 @@ impl<W: Write> Multiview<W> {
/// Renders all the tiles of the multiview. /// Renders all the tiles of the multiview.
pub fn render(&mut self) -> io::Result<()> { pub fn render(&mut self) -> io::Result<()> {
let now = Instant::now();
if now.duration_since(self.last_render) < Duration::from_millis(20) {
return Ok(());
}
self.last_render = now;
let mut buffer = vec![]; let mut buffer = vec![];
for i in 0..self.tiles.len() { for i in 0..self.tiles.len() {
for j in 0..self.tiles[0].len() { for j in 0..self.tiles[0].len() {

View File

@ -249,7 +249,6 @@ impl Tile {
/// Push content into the stdout of the tile. /// Push content into the stdout of the tile.
pub fn push_stdout(&mut self, content: String) { pub fn push_stdout(&mut self, content: String) {
eprintln!("{:?}", content); eprintln!("{:?}", content);
for c in content.chars() { for c in content.chars() {
if c == '\x1b' { if c == '\x1b' {
self.counting = false; self.counting = false;
@ -363,7 +362,7 @@ impl Tile {
.stdout .stdout
.iter() .iter()
.skip(scroll as usize) .skip(scroll as usize)
.take((h + scroll) as usize); .take(h as usize + 1);
let mut line = iter.next().unwrap(); let mut line = iter.next().unwrap();
let mut char_iter = line.chars(); let mut char_iter = line.chars();