mirror of
https://github.com/soywod/himalaya.git
synced 2024-07-05 17:15:12 +00:00
fix unicode issue (#71)
This commit is contained in:
parent
14d8a3fecd
commit
994141f852
|
@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Unicode chars breaks the view [#71]
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Telescope support [#61]
|
- Telescope support [#61]
|
||||||
|
@ -118,3 +122,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
||||||
[#40]: https://github.com/soywod/himalaya/issues/40
|
[#40]: https://github.com/soywod/himalaya/issues/40
|
||||||
[#41]: https://github.com/soywod/himalaya/issues/41
|
[#41]: https://github.com/soywod/himalaya/issues/41
|
||||||
[#61]: https://github.com/soywod/himalaya/issues/61
|
[#61]: https://github.com/soywod/himalaya/issues/61
|
||||||
|
[#71]: https://github.com/soywod/himalaya/issues/71
|
||||||
|
|
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -318,6 +318,7 @@ dependencies = [
|
||||||
"terminal_size",
|
"terminal_size",
|
||||||
"toml",
|
"toml",
|
||||||
"tree_magic",
|
"tree_magic",
|
||||||
|
"unicode-width",
|
||||||
"uuid",
|
"uuid",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -18,4 +18,5 @@ serde_json = "1.0.61"
|
||||||
terminal_size = "0.1.15"
|
terminal_size = "0.1.15"
|
||||||
toml = "0.5.8"
|
toml = "0.5.8"
|
||||||
tree_magic = "0.2.3"
|
tree_magic = "0.2.3"
|
||||||
|
unicode-width = "0.1.7"
|
||||||
uuid = { version = "0.8", features = ["v4"] }
|
uuid = { version = "0.8", features = ["v4"] }
|
||||||
|
|
24
src/table.rs
24
src/table.rs
|
@ -1,5 +1,6 @@
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use terminal_size::terminal_size;
|
use terminal_size::terminal_size;
|
||||||
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Style(u8, u8, u8);
|
pub struct Style(u8, u8, u8);
|
||||||
|
@ -46,8 +47,8 @@ impl Cell {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn printable_value_len(&self) -> usize {
|
pub fn unicode_width(&self) -> usize {
|
||||||
self.value.chars().collect::<Vec<_>>().len()
|
UnicodeWidthStr::width(self.value.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(&self, col_size: usize) -> String {
|
pub fn render(&self, col_size: usize) -> String {
|
||||||
|
@ -58,18 +59,15 @@ impl Cell {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.concat();
|
.concat();
|
||||||
let style_end = "\x1b[0m";
|
let style_end = "\x1b[0m";
|
||||||
|
let unicode_width = self.unicode_width();
|
||||||
|
|
||||||
if col_size > 0 && self.printable_value_len() > col_size {
|
if col_size > 0 && unicode_width > col_size {
|
||||||
let value: String = self.value.chars().collect::<Vec<_>>()[0..=col_size - 2]
|
String::from(style_begin + &self.value[0..=col_size - 2] + "… " + style_end)
|
||||||
.into_iter()
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
String::from(style_begin + &value + "… " + style_end)
|
|
||||||
} else {
|
} else {
|
||||||
let padding = if col_size == 0 {
|
let padding = if col_size == 0 {
|
||||||
"".to_string()
|
"".to_string()
|
||||||
} else {
|
} else {
|
||||||
" ".repeat(col_size - self.printable_value_len() + 1)
|
" ".repeat(col_size - unicode_width + 1)
|
||||||
};
|
};
|
||||||
|
|
||||||
String::from(style_begin + &self.value + &padding + style_end)
|
String::from(style_begin + &self.value + &padding + style_end)
|
||||||
|
@ -102,7 +100,7 @@ pub trait DisplayTable<'a, T: DisplayRow + 'a> {
|
||||||
let head = Self::header_row();
|
let head = Self::header_row();
|
||||||
|
|
||||||
head.iter().for_each(|cell| {
|
head.iter().for_each(|cell| {
|
||||||
col_sizes.push(cell.printable_value_len());
|
col_sizes.push(cell.unicode_width());
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut table = self
|
let mut table = self
|
||||||
|
@ -110,9 +108,9 @@ pub trait DisplayTable<'a, T: DisplayRow + 'a> {
|
||||||
.iter()
|
.iter()
|
||||||
.map(|item| {
|
.map(|item| {
|
||||||
let row = item.to_row();
|
let row = item.to_row();
|
||||||
row.iter().enumerate().for_each(|(i, cell)| {
|
row.iter()
|
||||||
col_sizes[i] = col_sizes[i].max(cell.printable_value_len())
|
.enumerate()
|
||||||
});
|
.for_each(|(i, cell)| col_sizes[i] = col_sizes[i].max(cell.unicode_width()));
|
||||||
row
|
row
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
Loading…
Reference in a new issue