From 7480d796037fcfec3ec49204cc9099e5208d965b Mon Sep 17 00:00:00 2001 From: DuckDuckWhale Date: Sun, 15 Nov 2020 00:29:59 -0800 Subject: [PATCH] Fix: wrong width for Unicode characters --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/main.rs | 12 ++++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c57a48d..fc61aed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,6 +105,7 @@ name = "gitea-timings" version = "0.1.0" dependencies = [ "serde", + "unicode-width", "ureq", ] @@ -485,6 +486,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" + [[package]] name = "unicode-xid" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index 19826bc..f3cb3b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2018" [dependencies] serde = "1.0.117" +unicode-width = "0.1.8" ureq = { version = "1.5.2", features = ["json"] } diff --git a/src/main.rs b/src/main.rs index c8ffc85..9c82668 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ use std::collections::HashMap; use std::fs; use serde::Deserialize; +use unicode_width::UnicodeWidthStr; #[derive(Deserialize)] 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))); 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 / 60 % 60, - seconds % 60, + (seconds + 30) / 60 % 60, times.len(), if times.is_empty() { '.' } else { ':' } ); @@ -69,7 +69,11 @@ fn main() { time % 60, title ); - println!("{}{}|", line, " ".repeat(separator.len() - line.len() - 1)); + println!( + "{}{}|", + line, + " ".repeat(separator.len() - line.width() - 1) + ); } println!("{}", separator); }