Skip to content

Commit

Permalink
Add encoding field and enum
Browse files Browse the repository at this point in the history
Signed-off-by: Leni Aniva <v@leni.sh>
  • Loading branch information
lenianiva committed Aug 17, 2023
1 parent 0adc8a0 commit f5a333d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
6 changes: 6 additions & 0 deletions src/encoding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[derive(Debug)]
#[allow(non_snake_case)]
pub enum Encoding {
ASCII,
UTF8,
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,13 @@
//! }
//! ```

pub mod encoding;
pub mod error;
pub mod process;
pub mod reader;
pub mod session;

pub use encoding::Encoding;
pub use reader::ReadUntil;
pub use session::{spawn, spawn_bash, spawn_python, spawn_stream};

Expand Down
22 changes: 13 additions & 9 deletions src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Unblocking reader which supports waiting for strings/regexes and EOF to be present

use crate::error::Error;
use crate::encoding::Encoding;
pub use regex::Regex;
use std::io::prelude::*;
use std::io::{self, BufReader};
Expand Down Expand Up @@ -108,6 +109,7 @@ pub struct NBReader {
buffer: String,
eof: bool,
timeout: Option<time::Duration>,
encoding: Encoding,
}

impl NBReader {
Expand Down Expand Up @@ -154,6 +156,7 @@ impl NBReader {
buffer: String::with_capacity(1024),
eof: false,
timeout: timeout.map(time::Duration::from_millis),
encoding: Encoding::UTF8,
}
}

Expand All @@ -162,21 +165,22 @@ impl NBReader {
if self.eof {
return Ok(());
}
// FIXME: Temporary flag to demonstrate utf-8 capabilities
let unicode = true;
// NOTE: When UTF-8 mode is on, there is no handling to salvage a
// stream of chars if a broken unicode char is not completed.
let mut char_buf: Vec<u8> = Vec::new();

while let Ok(from_channel) = self.reader.try_recv() {
match from_channel {
Ok(PipedChar::Char(c)) => {
if unicode {
char_buf.push(c);
if let Ok(s) = std::str::from_utf8(&char_buf) {
self.buffer.push(s.chars().next().unwrap());
char_buf.clear();
match &self.encoding {
Encoding::ASCII => self.buffer.push(c as char),
Encoding::UTF8 => {
char_buf.push(c);
if let Ok(s) = std::str::from_utf8(&char_buf) {
self.buffer.push(s.chars().next().unwrap());
char_buf.clear();
}
}
} else {
self.buffer.push(c as char)
}
},
Ok(PipedChar::EOF) => self.eof = true,
Expand Down

0 comments on commit f5a333d

Please sign in to comment.