Fix: wrong width for Unicode characters

This commit is contained in:
DuckDuckWhale 2020-11-15 00:29:59 -08:00
parent 34efe371ef
commit 7480d79603
3 changed files with 16 additions and 4 deletions

7
Cargo.lock generated
View File

@ -105,6 +105,7 @@ name = "gitea-timings"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"serde", "serde",
"unicode-width",
"ureq", "ureq",
] ]
@ -485,6 +486,12 @@ dependencies = [
"tinyvec", "tinyvec",
] ]
[[package]]
name = "unicode-width"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[package]] [[package]]
name = "unicode-xid" name = "unicode-xid"
version = "0.2.1" version = "0.2.1"

View File

@ -6,4 +6,5 @@ edition = "2018"
[dependencies] [dependencies]
serde = "1.0.117" serde = "1.0.117"
unicode-width = "0.1.8"
ureq = { version = "1.5.2", features = ["json"] } ureq = { version = "1.5.2", features = ["json"] }

View File

@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::fs; use std::fs;
use serde::Deserialize; use serde::Deserialize;
use unicode_width::UnicodeWidthStr;
#[derive(Deserialize)] #[derive(Deserialize)]
struct TrackedTime { struct TrackedTime {
@ -50,10 +51,9 @@ fn main() {
times.sort_unstable_by(|a, b| a.0.cmp(&b.0).reverse().then(a.1.cmp(&b.1))); times.sort_unstable_by(|a, b| a.0.cmp(&b.0).reverse().then(a.1.cmp(&b.1)));
println!( println!(
"In the last 24 hours, you spent {} hours, {} minutes and {} seconds on {} issues{}", "You spent {} hours and {} minutes on {} issues in the last 24 hours{}",
seconds / 3600, seconds / 3600,
seconds / 60 % 60, (seconds + 30) / 60 % 60,
seconds % 60,
times.len(), times.len(),
if times.is_empty() { '.' } else { ':' } if times.is_empty() { '.' } else { ':' }
); );
@ -69,7 +69,11 @@ fn main() {
time % 60, time % 60,
title title
); );
println!("{}{}|", line, " ".repeat(separator.len() - line.len() - 1)); println!(
"{}{}|",
line,
" ".repeat(separator.len() - line.width() - 1)
);
} }
println!("{}", separator); println!("{}", separator);
} }