clear to end of line properly in Timer, using termion

This commit is contained in:
Dustin Carlino 2019-03-11 14:01:06 -07:00
parent 92bf86454c
commit 899718bf48
7 changed files with 54 additions and 23 deletions

View File

@ -13,3 +13,4 @@ rand_xorshift = "0.1.1"
serde = "1.0.87" serde = "1.0.87"
serde_derive = "1.0" serde_derive = "1.0"
serde_json = "1.0" serde_json = "1.0"
termion = "1.5.1"

View File

@ -1,4 +1,4 @@
use crate::time::prettyprint_time; use crate::time::{clear_current_line, prettyprint_time};
use crate::{elapsed_seconds, prettyprint_usize, MultiMap, Timer, PROGRESS_FREQUENCY_SECONDS}; use crate::{elapsed_seconds, prettyprint_usize, MultiMap, Timer, PROGRESS_FREQUENCY_SECONDS};
use bincode; use bincode;
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
@ -225,16 +225,23 @@ impl Read for FileWithProgress {
let done = self.processed_bytes == self.total_bytes && bytes == 0; let done = self.processed_bytes == self.total_bytes && bytes == 0;
if elapsed_seconds(self.last_printed_at) >= PROGRESS_FREQUENCY_SECONDS || done { if elapsed_seconds(self.last_printed_at) >= PROGRESS_FREQUENCY_SECONDS || done {
self.last_printed_at = Instant::now(); self.last_printed_at = Instant::now();
clear_current_line();
if done {
// TODO Not seeing this case happen!
println!(
"Read {} ({})... {}",
self.path,
prettyprint_usize(self.total_bytes / 1024 / 1024),
prettyprint_time(elapsed_seconds(self.started_at))
);
} else {
print!( print!(
"\rReading {}: {}/{} MB... {}", "Reading {}: {}/{} MB... {}",
self.path, self.path,
prettyprint_usize(self.processed_bytes / 1024 / 1024), prettyprint_usize(self.processed_bytes / 1024 / 1024),
prettyprint_usize(self.total_bytes / 1024 / 1024), prettyprint_usize(self.total_bytes / 1024 / 1024),
prettyprint_time(elapsed_seconds(self.started_at)) prettyprint_time(elapsed_seconds(self.started_at))
); );
if done {
println!();
} else {
stdout().flush().unwrap(); stdout().flush().unwrap();
} }
} }

View File

@ -3,6 +3,7 @@ use procfs;
use std::collections::HashMap; use std::collections::HashMap;
use std::io::{stdout, Write}; use std::io::{stdout, Write};
use std::time::Instant; use std::time::Instant;
use termion;
pub fn elapsed_seconds(since: Instant) -> f64 { pub fn elapsed_seconds(since: Instant) -> f64 {
let dt = since.elapsed(); let dt = since.elapsed();
@ -47,14 +48,14 @@ impl Progress {
prettyprint_usize(self.total_items), prettyprint_usize(self.total_items),
prettyprint_time(elapsed) prettyprint_time(elapsed)
); );
// TODO blank till end of current line clear_current_line();
println!("\r{}", line); println!("{}", line);
return Some((elapsed, line)); return Some((elapsed, line));
} else if elapsed_seconds(self.last_printed_at) >= PROGRESS_FREQUENCY_SECONDS { } else if elapsed_seconds(self.last_printed_at) >= PROGRESS_FREQUENCY_SECONDS {
self.last_printed_at = Instant::now(); self.last_printed_at = Instant::now();
// TODO blank till end of current line clear_current_line();
print!( print!(
"\r{}: {}/{}... {}", "{}: {}/{}... {}",
self.label, self.label,
prettyprint_usize(self.processed_items), prettyprint_usize(self.processed_items),
prettyprint_usize(self.total_items), prettyprint_usize(self.total_items),
@ -110,6 +111,11 @@ impl Timer {
// Log immediately, but also repeat at the end, to avoid having to scroll up and find // Log immediately, but also repeat at the end, to avoid having to scroll up and find
// interesting debug stuff. // interesting debug stuff.
pub fn note(&mut self, line: String) { pub fn note(&mut self, line: String) {
// Interrupt the start_iter with a newline.
if let Some(StackEntry::Progress(_)) = self.stack.last() {
println!();
}
println!("{}", line); println!("{}", line);
self.notes.push(line); self.notes.push(line);
} }
@ -396,3 +402,12 @@ impl MeasureMemory {
fn process_used_memory_mb() -> usize { fn process_used_memory_mb() -> usize {
(procfs::Process::myself().unwrap().stat.vsize / 1024 / 1024) as usize (procfs::Process::myself().unwrap().stat.vsize / 1024 / 1024) as usize
} }
pub(crate) fn clear_current_line() {
let (terminal_width, _) = termion::terminal_size().unwrap();
print!(
"{}{}",
termion::clear::CurrentLine,
termion::cursor::Left(terminal_width)
);
}

View File

@ -20,3 +20,8 @@
- first: all the maps fully convert and display in some form; all tests pass or are disabled - first: all the maps fully convert and display in some form; all tests pass or are disabled
- slowly hone away at problems currently with errors printed (like bad pl shift angles) - slowly hone away at problems currently with errors printed (like bad pl shift angles)
- eventually: every intersection has at least a turn, minimum lengths enforced, etc - eventually: every intersection has at least a turn, minimum lengths enforced, etc
## Code to release
- tmp_gfx as a 2D-with-camera glium sample
- ezgui

View File

@ -1,10 +0,0 @@
# Profiling
apt-get install google-perftools libgoogle-perftools-dev
Follow Usage from https://crates.io/crates/cpuprofiler
Run editor or headless with --enable_profiler
google-pprof --no_strip_temp ../target/debug/editor profile
google-pprof --no_strip_temp ../target/release/headless profile
top30 --cum

View File

@ -10,3 +10,14 @@ Debug OpenGL calls:
apitrace trace --api gl ../target/debug/editor ../data/raw_maps/montlake.abst apitrace trace --api gl ../target/debug/editor ../data/raw_maps/montlake.abst
qapitrace editor.trace qapitrace editor.trace
apitrace dump editor.trace apitrace dump editor.trace
## Profiling
apt-get install google-perftools libgoogle-perftools-dev
Follow Usage from https://crates.io/crates/cpuprofiler
Run editor or headless with --enable_profiler
google-pprof --no_strip_temp ../target/debug/editor profile
google-pprof --no_strip_temp ../target/release/headless profile
top30 --cum

View File

@ -23,6 +23,7 @@
- https://www.seattle.gov/transportation/projects-and-programs/programs/neighborhood-street-fund / https://www.seattle.gov/neighborhoods/programs-and-services/your-voice-your-choice - https://www.seattle.gov/transportation/projects-and-programs/programs/neighborhood-street-fund / https://www.seattle.gov/neighborhoods/programs-and-services/your-voice-your-choice
- https://commuteseattle.com/ - https://commuteseattle.com/
- https://www.theurbanist.org/ - https://www.theurbanist.org/
- https://humantransit.org/2019/03/notes-on-simcity-at-30.html
## Similar projects ## Similar projects
@ -64,6 +65,7 @@ Seattlites with opinions and ideas:
### Games ### Games
SimCity, Cities: Skylines SimCity, Cities: Skylines
https://steamcommunity.com/sharedfiles/filedetails/?id=583429740
### Open source urban planning ### Open source urban planning