shell: fit size of ascii box around pty to its contents

This commit is contained in:
Michael Sippel 2021-09-21 02:19:42 +02:00
parent 5ec840e87c
commit 911b69aeea
Signed by: senvas
GPG key ID: F96CF119C34B64A6

View file

@ -69,12 +69,29 @@ impl AsciiBox {
ascii_box ascii_box
} }
pub fn resize(&mut self, new_extent: Vector2<i16>) {
if self.extent != new_extent {
let old_extent = self.extent;
self.extent = new_extent;
self.notify_each(GridWindowIterator::from(Point2::new(0, 0) .. Point2::new(2+std::cmp::max(old_extent.x, new_extent.x), 2+std::cmp::max(old_extent.y, new_extent.y))));
}
}
pub fn fit_content(&mut self) {
if let Some(c) = self.content.as_ref() {
let p = c.range().end;
self.resize(Vector2::new(p.x, p.y));
} else {
self.resize(Vector2::new(0, 0));
}
}
} }
impl Observer<dyn TerminalView> for AsciiBox { impl Observer<dyn TerminalView> for AsciiBox {
fn reset(&mut self, new_content: Option<Arc<dyn TerminalView>>) { fn reset(&mut self, new_content: Option<Arc<dyn TerminalView>>) {
self.content = new_content; self.content = new_content;
self.notify_each(GridWindowIterator::from(Point2::new(0, 0) ..= Point2::new(self.extent.x, self.extent.y))); self.notify_each(GridWindowIterator::from(Point2::new(0, 0) .. Point2::new(self.extent.x+2, self.extent.y+2)));
} }
fn notify(&mut self, pt: &Point2<i16>) { fn notify(&mut self, pt: &Point2<i16>) {
@ -90,31 +107,31 @@ impl IndexView<Point2<i16>> for AsciiBox {
type Item = TerminalAtom; type Item = TerminalAtom;
fn get(&self, pt: &Point2<i16>) -> Option<TerminalAtom> { fn get(&self, pt: &Point2<i16>) -> Option<TerminalAtom> {
if pt.x == 0 || pt.x == self.extent.x { if pt.x == 0 || pt.x == self.extent.x+1 {
// vertical line // vertical line
if pt.y == 0 && pt.x == 0 { if pt.y == 0 && pt.x == 0 {
Some(TerminalAtom::from('╭')) Some(TerminalAtom::from('╭'))
} else if pt.y == 0 && pt.x == self.extent.x { } else if pt.y == 0 && pt.x == self.extent.x+1 {
Some(TerminalAtom::from('╮')) Some(TerminalAtom::from('╮'))
} else if pt.y > 0 && pt.y < self.extent.y { } else if pt.y > 0 && pt.y < self.extent.y+1 {
Some(TerminalAtom::from('│')) Some(TerminalAtom::from('│'))
} else if pt.y == self.extent.y && pt.x == 0 { } else if pt.y == self.extent.y+1 && pt.x == 0 {
Some(TerminalAtom::from('╰')) Some(TerminalAtom::from('╰'))
} else if pt.y == self.extent.y && pt.x == self.extent.x { } else if pt.y == self.extent.y+1 && pt.x == self.extent.x+1 {
Some(TerminalAtom::from('╯')) Some(TerminalAtom::from('╯'))
} else { } else {
None None
} }
} else if pt.y == 0 || pt.y == self.extent.y { } else if pt.y == 0 || pt.y == self.extent.y+1 {
// horizontal line // horizontal line
if pt.x > 0 && pt.x < self.extent.x { if pt.x > 0 && pt.x < self.extent.x+1 {
Some(TerminalAtom::from('─')) Some(TerminalAtom::from('─'))
} else { } else {
None None
} }
} else if } else if
pt.x < self.extent.x && pt.x < self.extent.x+1 &&
pt.y < self.extent.y pt.y < self.extent.y+1
{ {
self.content.get(&(pt - Vector2::new(1, 1))) self.content.get(&(pt - Vector2::new(1, 1)))
} else { } else {
@ -124,7 +141,7 @@ impl IndexView<Point2<i16>> for AsciiBox {
fn area(&self) -> Option<Vec<Point2<i16>>> { fn area(&self) -> Option<Vec<Point2<i16>>> {
Some(GridWindowIterator::from( Some(GridWindowIterator::from(
Point2::new(0, 0) ..= Point2::new(self.extent.x, self.extent.y) Point2::new(0, 0) .. Point2::new(self.extent.x+2, self.extent.y+2)
).collect()) ).collect())
} }
} }
@ -180,11 +197,15 @@ async fn main() {
}); });
let mut pty : Option<pty::PTY> = None; let mut pty : Option<pty::PTY> = None;
let mut ptybox : Option<Arc<RwLock<AsciiBox>>> = None;
loop { loop {
term_port.update(); term_port.update();
if let Some(p) = pty.as_mut() { if let Some(p) = pty.as_mut() {
if p.get_status() { if p.get_status() {
if let Some(ptybox) = ptybox.take() {
ptybox.write().unwrap().fit_content();
}
pty = None; pty = None;
process_launcher = ProcessLauncher::new(); process_launcher = ProcessLauncher::new();
@ -247,12 +268,14 @@ async fn main() {
let box_port = ViewPort::new(); let box_port = ViewPort::new();
let test_box = AsciiBox::new( let test_box = AsciiBox::new(
Vector2::new(81, 26), Vector2::new(80, 25),
output_port.outer() output_port.outer()
.map_item(|_,a| a.add_style_back(TerminalStyle::fg_color((230, 230, 230)))), .map_item(|_,a| a.add_style_back(TerminalStyle::fg_color((230, 230, 230)))),
box_port.inner() box_port.inner()
); );
ptybox = Some(test_box.clone());
table_buf.remove(Point2::new(0, y-1)); table_buf.remove(Point2::new(0, y-1));
let mut p = box_port.outer().map_item(|_idx, x| x.add_style_back(TerminalStyle::fg_color((90, 120, 100)))) let mut p = box_port.outer().map_item(|_idx, x| x.add_style_back(TerminalStyle::fg_color((90, 120, 100))))