aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-05-03 23:49:41 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-05-03 23:51:02 +0200
commit13cdb0218c4ba9a0b531f41e49964704932d2510 (patch)
treec7ebd0350f390f774c61928dbd418c032dd0a0dc
parent4d102e069e4a0b6064a79915d368773338dbbb8e (diff)
downloadmail-13cdb0218c4ba9a0b531f41e49964704932d2510.tar.gz
truncate long strings
-rw-r--r--Cargo.lock41
-rw-r--r--Cargo.toml4
-rw-r--r--src/window.rs14
3 files changed, 37 insertions, 22 deletions
diff --git a/Cargo.lock b/Cargo.lock
index a10ee2d..730c560 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,70 +4,75 @@
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[package]]
name = "libc"
version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "mail"
version = "0.1.0"
dependencies = [
- "notmuch 0.6.0",
- "termion 1.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
+ "notmuch",
+ "termion",
+ "unicode-segmentation",
]
[[package]]
name = "notmuch"
version = "0.6.0"
dependencies = [
- "libc 0.2.94 (registry+https://github.com/rust-lang/crates.io-index)",
- "supercow 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc",
+ "supercow",
]
[[package]]
name = "numtoa"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
[[package]]
name = "redox_syscall"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "85dd92e586f7355c633911e11f77f3d12f04b1b1bd76a198bd34ae3af8341ef2"
dependencies = [
- "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "bitflags",
]
[[package]]
name = "redox_termios"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
dependencies = [
- "redox_syscall 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
+ "redox_syscall",
]
[[package]]
name = "supercow"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "171758edb47aa306a78dfa4ab9aeb5167405bd4e3dc2b64e88f6a84bbe98bd63"
[[package]]
name = "termion"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
dependencies = [
- "libc 0.2.94 (registry+https://github.com/rust-lang/crates.io-index)",
- "numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "redox_syscall 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
- "redox_termios 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc",
+ "numtoa",
+ "redox_syscall",
+ "redox_termios",
]
-[metadata]
-"checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
-"checksum libc 0.2.94 (registry+https://github.com/rust-lang/crates.io-index)" = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
-"checksum numtoa 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
-"checksum redox_syscall 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "85dd92e586f7355c633911e11f77f3d12f04b1b1bd76a198bd34ae3af8341ef2"
-"checksum redox_termios 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
-"checksum supercow 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "171758edb47aa306a78dfa4ab9aeb5167405bd4e3dc2b64e88f6a84bbe98bd63"
-"checksum termion 1.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
+[[package]]
+name = "unicode-segmentation"
+version = "1.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb0d2e7be6ae3a5fa87eed5fb451aff96f2573d2694942e40543ae0bbe19c796"
diff --git a/Cargo.toml b/Cargo.toml
index a06693e..05c475c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -7,9 +7,9 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
-termion = "1" # terminal control (cursor, style)
-
notmuch = { path = "notmuch" }
+termion = "1"
+unicode-segmentation = "1"
[profile.release]
lto = "thin"
diff --git a/src/window.rs b/src/window.rs
index 3714c4c..a2f2b68 100644
--- a/src/window.rs
+++ b/src/window.rs
@@ -2,6 +2,7 @@
use std::io::Write;
use termion::{color, cursor};
+use unicode_segmentation::UnicodeSegmentation;
#[derive(Clone, Copy)]
pub struct Area {
@@ -16,6 +17,15 @@ pub enum Line {
Highlight(String),
}
+pub fn truncate_dots(s: &str, to: usize) -> String {
+ let graphemes = s.graphemes(true).collect::<Vec<_>>();
+ if graphemes.len() >= to {
+ format!("{}...", graphemes.iter().take(to - 3).fold(String::new(), |acc, s| format!("{}{}", acc, s)))
+ } else {
+ graphemes.join("")
+ }
+}
+
pub struct Window {
pub lines: Vec<Line>,
scroll: usize,
@@ -37,12 +47,12 @@ impl Window {
Some(line) => {
write!(out, "{}", cursor::Goto(area.x, y))?;
match line {
- Line::Normal(s) => write!(out, "{}", s)?,
+ Line::Normal(s) => write!(out, "{}", truncate_dots(s, area.w as usize))?,
Line::Highlight(s) => write!(
out,
"{}{}{}",
color::Fg(color::Red),
- s,
+ truncate_dots(s, area.w as usize),
color::Fg(color::Reset),
)?,
}