Skip to content

Commit

Permalink
Implement "has_leading_newline"
Browse files Browse the repository at this point in the history
This change adds a new TermOption, "has_leading_newline".

When this option is set to false (default is true),
TermLock::ensure_height() does not add a leading newline
(=the separator between the original line contents and the new
contents) to the output anymore.

When it is set to true, there is no change in ensure_height()'s
behavior (the leading newline is present).
  • Loading branch information
c4eater committed May 4, 2022
1 parent 05908fc commit 08c9ab2
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct TermOptions {
mouse_enabled: bool,
raw_mouse: bool,
hold: bool, // to start term or not on creation
has_leading_newline: bool,
}

impl Default for TermOptions {
Expand All @@ -85,6 +86,7 @@ impl Default for TermOptions {
mouse_enabled: false,
raw_mouse: false,
hold: false,
has_leading_newline: true,
}
}
}
Expand Down Expand Up @@ -120,6 +122,10 @@ impl TermOptions {
self.hold = hold;
self
}
pub fn has_leading_newline(mut self, has_leading_newline: bool) -> Self {
self.has_leading_newline = has_leading_newline;
self
}
}

impl<UserEvent: Send + 'static> Term<UserEvent> {
Expand Down Expand Up @@ -563,6 +569,7 @@ struct TermLock {
bottom_intact: bool,
clear_on_exit: bool,
mouse_enabled: bool,
has_leading_newline: bool,
alternate_screen: bool,
cursor_row: usize,
screen_height: usize,
Expand All @@ -578,6 +585,7 @@ impl Default for TermLock {
max_height: TermHeight::Percent(100),
min_height: TermHeight::Fixed(3),
bottom_intact: false,
has_leading_newline: true,
alternate_screen: false,
cursor_row: 0,
screen_height: 0,
Expand All @@ -598,6 +606,7 @@ impl TermLock {
term.min_height = options.min_height;
term.clear_on_exit = options.clear_on_exit;
term.mouse_enabled = options.mouse_enabled;
term.has_leading_newline = options.has_leading_newline;
term
}

Expand Down Expand Up @@ -744,8 +753,7 @@ impl TermLock {
} else {
// only use part of the screen

// go to a new line so that existing line won't be messed up
if cursor_col > 0 {
if self.has_leading_newline && cursor_col > 0 {
output.write("\n");
cursor_row += 1;
}
Expand Down

0 comments on commit 08c9ab2

Please sign in to comment.