on master help sign in
feat(ui): file encoding in statusline (#1355)

* feat(ui): file encoding in statusline Display file encoding in statusline if the encoding isn't UTF-8. * Re-export encoding_rs from core From there it can be imported by other mods that rely on it.

commit4b0b1a5657b78693efe609647360de30264fcc92 authorMatouš Dzivjak <matousdzivjak@gmail.com> dateSat 25 Dec 2021, 16:10 (5 years ago) parentec878e4
6 files changed+29−19
Cargo.lock+1−1 helix-core/Cargo.toml+1 helix-core/src/lib.rs+2 helix-term/src/ui/editor.rs+8−1 helix-view/Cargo.toml−1 helix-view/src/document.rs+17−16
Cargo.lock+1−1
@@ -370,6 +370,7 @@ version = "0.5.0"
370370dependencies = [
371371 "arc-swap",
372372 "chrono",
373+ "encoding_rs",
373374 "etcetera",
374375 "helix-syntax",
375376 "log",
@@ -471,7 +472,6 @@ dependencies = [
471472 "chardetng",
472473 "clipboard-win",
473474 "crossterm",
474− "encoding_rs",
475475 "futures-util",
476476 "helix-core",
477477 "helix-lsp",
helix-core/Cargo.toml+1
@@ -35,6 +35,7 @@ toml = "0.5"
3535similar = "2.1"
3636
3737etcetera = "0.3"
38+encoding_rs = "0.8"
3839
3940chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] }
4041
helix-core/src/lib.rs+2
@@ -1,3 +1,5 @@
1+pub use encoding_rs as encoding;
2+
13pub mod auto_pairs;
24pub mod chars;
35pub mod comment;
helix-term/src/ui/editor.rs+8−1
@@ -7,7 +7,7 @@ use crate::{
77};
88
99use helix_core::{
10− coords_at_pos,
10+ coords_at_pos, encoding,
1111 graphemes::{ensure_grapheme_boundary_next, next_grapheme_boundary, prev_grapheme_boundary},
1212 movement::Direction,
1313 syntax::{self, HighlightEvent},
@@ -621,6 +621,13 @@ impl EditorView {
621621 base_style,
622622 ));
623623
624+ let enc = doc.encoding();
625+ if enc != encoding::UTF_8 {
626+ right_side_text
627+ .0
628+ .push(Span::styled(format!(" {} ", enc.name()), base_style));
629+ }
630+
624631 // Render to the statusline.
625632 surface.set_spans(
626633 viewport.x
helix-view/Cargo.toml−1
@@ -29,7 +29,6 @@ futures-util = { version = "0.3", features = ["std", "async-await"], default-fea
2929
3030slotmap = "1"
3131
32−encoding_rs = "0.8"
3332chardetng = "0.1"
3433
3534serde = { version = "1.0", features = ["derive"] }
helix-view/src/document.rs+17−16
@@ -9,6 +9,7 @@ use std::str::FromStr;
99use std::sync::Arc;
1010
1111use helix_core::{
12+ encoding,
1213 history::History,
1314 indent::{auto_detect_indent_style, IndentStyle},
1415 line_ending::auto_detect_line_ending,
@@ -74,7 +75,7 @@ pub struct Document {
7475 pub(crate) selections: HashMap<ViewId, Selection>,
7576
7677 path: Option<PathBuf>,
77− encoding: &'static encoding_rs::Encoding,
78+ encoding: &'static encoding::Encoding,
7879
7980 /// Current editing mode.
8081 pub mode: Mode,
@@ -143,8 +144,8 @@ impl fmt::Debug for Document {
143144/// be used to override encoding auto-detection.
144145pub fn from_reader<R: std::io::Read + ?Sized>(
145146 reader: &mut R,
146− encoding: Option<&'static encoding_rs::Encoding>,
147−) -> Result<(Rope, &'static encoding_rs::Encoding), Error> {
147+ encoding: Option<&'static encoding::Encoding>,
148+) -> Result<(Rope, &'static encoding::Encoding), Error> {
148149 // These two buffers are 8192 bytes in size each and are used as
149150 // intermediaries during the decoding process. Text read into `buf`
150151 // from `reader` is decoded into `buf_out` as UTF-8. Once either
@@ -212,11 +213,11 @@ pub fn from_reader<R: std::io::Read + ?Sized>(
212213 total_read += read;
213214 total_written += written;
214215 match result {
215− encoding_rs::CoderResult::InputEmpty => {
216+ encoding::CoderResult::InputEmpty => {
216217 debug_assert_eq!(slice.len(), total_read);
217218 break;
218219 }
219− encoding_rs::CoderResult::OutputFull => {
220+ encoding::CoderResult::OutputFull => {
220221 debug_assert!(slice.len() > total_read);
221222 builder.append(&buf_str[..total_written]);
222223 total_written = 0;
@@ -251,7 +252,7 @@ pub fn from_reader<R: std::io::Read + ?Sized>(
251252/// replacement characters may appear in the encoded text.
252253pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
253254 writer: &'a mut W,
254− encoding: &'static encoding_rs::Encoding,
255+ encoding: &'static encoding::Encoding,
255256 rope: &'a Rope,
256257) -> Result<(), Error> {
257258 // Text inside a `Rope` is stored as non-contiguous blocks of data called
@@ -286,12 +287,12 @@ pub async fn to_writer<'a, W: tokio::io::AsyncWriteExt + Unpin + ?Sized>(
286287 total_read += read;
287288 total_written += written;
288289 match result {
289− encoding_rs::CoderResult::InputEmpty => {
290+ encoding::CoderResult::InputEmpty => {
290291 debug_assert_eq!(chunk.len(), total_read);
291292 debug_assert!(buf.len() >= total_written);
292293 break;
293294 }
294− encoding_rs::CoderResult::OutputFull => {
295+ encoding::CoderResult::OutputFull => {
295296 debug_assert!(chunk.len() > total_read);
296297 writer.write_all(&buf[..total_written]).await?;
297298 total_written = 0;
@@ -322,8 +323,8 @@ use helix_lsp::lsp;
322323use url::Url;
323324
324325impl Document {
325− pub fn from(text: Rope, encoding: Option<&'static encoding_rs::Encoding>) -> Self {
326− let encoding = encoding.unwrap_or(encoding_rs::UTF_8);
326+ pub fn from(text: Rope, encoding: Option<&'static encoding::Encoding>) -> Self {
327+ let encoding = encoding.unwrap_or(encoding::UTF_8);
327328 let changes = ChangeSet::new(&text);
328329 let old_state = None;
329330
@@ -356,7 +357,7 @@ impl Document {
356357 /// overwritten with the `encoding` parameter.
357358 pub fn open(
358359 path: &Path,
359− encoding: Option<&'static encoding_rs::Encoding>,
360+ encoding: Option<&'static encoding::Encoding>,
360361 theme: Option<&Theme>,
361362 config_loader: Option<&syntax::Loader>,
362363 ) -> Result<Self, Error> {
@@ -366,7 +367,7 @@ impl Document {
366367 std::fs::File::open(path).context(format!("unable to open {:?}", path))?;
367368 from_reader(&mut file, encoding)?
368369 } else {
369− let encoding = encoding.unwrap_or(encoding_rs::UTF_8);
370+ let encoding = encoding.unwrap_or(encoding::UTF_8);
370371 (Rope::from(DEFAULT_LINE_ENDING.as_str()), encoding)
371372 };
372373
@@ -548,7 +549,7 @@ impl Document {
548549
549550 /// Sets the [`Document`]'s encoding with the encoding correspondent to `label`.
550551 pub fn set_encoding(&mut self, label: &str) -> Result<(), Error> {
551− match encoding_rs::Encoding::for_label(label.as_bytes()) {
552+ match encoding::Encoding::for_label(label.as_bytes()) {
552553 Some(encoding) => self.encoding = encoding,
553554 None => return Err(anyhow::anyhow!("unknown encoding")),
554555 }
@@ -556,7 +557,7 @@ impl Document {
556557 }
557558
558559 /// Returns the [`Document`]'s current encoding.
559− pub fn encoding(&self) -> &'static encoding_rs::Encoding {
560+ pub fn encoding(&self) -> &'static encoding::Encoding {
560561 self.encoding
561562 }
562563
@@ -1123,7 +1124,7 @@ mod test {
11231124
11241125 macro_rules! test_decode {
11251126 ($label:expr, $label_override:expr) => {
1126− let encoding = encoding_rs::Encoding::for_label($label_override.as_bytes()).unwrap();
1127+ let encoding = encoding::Encoding::for_label($label_override.as_bytes()).unwrap();
11271128 let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/encoding");
11281129 let path = base_path.join(format!("{}_in.txt", $label));
11291130 let ref_path = base_path.join(format!("{}_in_ref.txt", $label));
@@ -1142,7 +1143,7 @@ mod test {
11421143
11431144 macro_rules! test_encode {
11441145 ($label:expr, $label_override:expr) => {
1145− let encoding = encoding_rs::Encoding::for_label($label_override.as_bytes()).unwrap();
1146+ let encoding = encoding::Encoding::for_label($label_override.as_bytes()).unwrap();
11461147 let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/encoding");
11471148 let path = base_path.join(format!("{}_out.txt", $label));
11481149 let ref_path = base_path.join(format!("{}_out_ref.txt", $label));