Fix more width issues

This commit is contained in:
Dheepak Krishnamurthy 2021-05-06 09:42:44 -06:00
parent a74d101b39
commit e20ec465c6
3 changed files with 14 additions and 15 deletions

View file

@ -17,6 +17,7 @@ use rustyline_derive::Helper;
use unicode_segmentation::Graphemes;
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;
pub fn get_start_word_under_cursor(line: &str, cursor_pos: usize) -> usize {
let mut chars = line[..cursor_pos].chars();
@ -143,10 +144,7 @@ impl CompletionList {
}
pub fn max_width(&self) -> Option<usize> {
self.candidates()
.iter()
.map(|p| p.display.graphemes(true).count() + 4)
.max()
self.candidates().iter().map(|p| p.display.width() + 4).max()
}
pub fn get(&self, i: usize) -> Option<String> {

View file

@ -498,14 +498,14 @@ where
"{symbol}{elt:>width$}",
symbol = symbol,
elt = elt,
width = *w as usize - symbol.to_string().graphemes(true).count()
width = (*w as usize).saturating_sub(symbol.to_string().width())
)
} else {
format!(
"{symbol}{elt:<width$}",
symbol = symbol,
elt = elt,
width = *w as usize - symbol.to_string().graphemes(true).count()
width = (*w as usize).saturating_sub(symbol.to_string().width())
)
}
} else {

View file

@ -5,6 +5,7 @@ use std::error::Error;
use std::process::Command;
use task_hookrs::task::Task;
use task_hookrs::uda::UDAValue;
use unicode_width::UnicodeWidthStr;
pub fn vague_format_date_time(from_dt: NaiveDateTime, to_dt: NaiveDateTime) -> String {
let mut seconds = (to_dt - from_dt).num_seconds();
@ -277,14 +278,14 @@ impl TaskReportTable {
None => format!(""),
};
let mut d = task.description().to_string();
let mut end = self.description_width;
let mut available_width = self.description_width;
if self.description_width >= c.len() {
end = self.description_width - c.len();
available_width = self.description_width - c.len();
}
while end < d.len() && !d.is_char_boundary(end) {
end += 1;
while available_width < d.width() && !d.is_char_boundary(available_width) {
available_width += 1;
}
d.truncate(end);
d.truncate(available_width);
if d != *task.description() {
d = format!("{}", d);
}
@ -292,11 +293,11 @@ impl TaskReportTable {
}
"description.truncated" => {
let mut d = task.description().to_string();
let mut end = self.description_width;
while end < d.len() && !d.is_char_boundary(end) {
end += 1;
let mut available_width = self.description_width;
while available_width < d.len() && !d.is_char_boundary(available_width) {
available_width += 1;
}
d.truncate(end);
d.truncate(available_width);
if d != *task.description() {
d = format!("{}", d);
}