Skip to content

Commit

Permalink
Improve line termination handling and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
David Sid Olofsson committed Oct 25, 2023
1 parent 3bd91d8 commit b338e89
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/cmd/io_commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ fn insert<'a>(
let mut tail = buffer.split_off(index);
let start = buffer.len();
for line in data {
buffer.push(Line::new(line).map_err(InternalError::InvalidLineText)?);
buffer.push(
Line::new(format!("{}\n", line)).map_err(InternalError::InvalidLineText)?
);
}
let end = buffer.len();
buffer.append(&mut tail);
Expand All @@ -43,7 +45,9 @@ fn replace_buffer<'a>(
) -> Result<usize> {
buffer.clear();
for line in data {
buffer.push(Line::new(line).map_err(InternalError::InvalidLineText)?);
buffer.push(
Line::new(format!("{}\n", line)).map_err(InternalError::InvalidLineText)?
);
}
Ok(buffer.len())
}
Expand Down Expand Up @@ -94,7 +98,7 @@ pub fn read_from_file(
state.io.read_file(file, command == 'E')?
},
};
let data = unformated_data.split_inclusive('\n');
let data = unformated_data.lines();
let datalen = match index {
Some(i) => insert(state.history.current_mut(full_command.into()), data, i),
None => replace_buffer(state.history.current_mut(full_command.into()), data),
Expand Down
22 changes: 22 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub trait IO {
/// Run a read command, collecting stdout to add into buffer
///
/// Stdin and Stderr should be passed through to UI
///
/// The returned string will be split into lines and added into the buffer.
/// All line endings are converted into '\n' when adding into the buffer.
fn run_read_command(&mut self,
// UI handle. Created by setting up the UI for passing through
// std-in/-err to child process.
Expand All @@ -49,6 +52,10 @@ pub trait IO {
///
/// Stdout and Stderr should be passed through to UI
/// Returns number of bytes written
///
/// The LinesIter contains string slices over '\n' terminated lines. If you
/// with to use "\r\n" line endings in the command input this should be
/// handled in the IO implementation.
fn run_write_command(&mut self,
// UI handle. Created by setting up the UI for passing through std-in/-err
// to child process.
Expand All @@ -63,6 +70,13 @@ pub trait IO {
/// via stdout.
///
/// Stderr should be passed through to UI
///
/// The LinesIter contains string slices over '\n' terminated lines. If you
/// with to use "\r\n" line endings in the command input this should be
/// handled in the IO implementation.
///
/// The returned string will be split into lines and added into the buffer.
/// All line endings are converted into '\n' when adding into the buffer.
fn run_transform_command(&mut self,
// UI handle. Created by setting up the UI for passing through
// std-in/-out/-err to child process.
Expand All @@ -74,7 +88,12 @@ pub trait IO {
) -> Result<String>;

/// Normal file write
///
/// Returns number of bytes written
///
/// The LinesIter contains string slices over '\n' terminated lines. If you
/// with to write "\r\n" line endings into the file this should be handled in
/// the IO implementation.
fn write_file(&mut self,
// Path to file as give by user. Not checked beyond shell escape parsing
path: &str,
Expand All @@ -85,6 +104,9 @@ pub trait IO {
) -> Result<usize>;

/// Normal file read
///
/// The returned string will be split into lines and added into the buffer.
/// All line endings are converted into '\n' when adding into the buffer.
fn read_file(&mut self,
// Path to file as given by user. Not checked beyond shell escape parsing
path: &str,
Expand Down

0 comments on commit b338e89

Please sign in to comment.