From 91a72709f8c72aef8d12658fd20410177e424f6a Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 19 Apr 2018 21:54:39 +0200 Subject: [PATCH] Add wrapping functionality to StdoutViewer --- lib/entry/libimagentryview/Cargo.toml | 1 + lib/entry/libimagentryview/src/builtin/stdout.rs | 13 ++++++++++++- lib/entry/libimagentryview/src/lib.rs | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/entry/libimagentryview/Cargo.toml b/lib/entry/libimagentryview/Cargo.toml index 3d00b01e..4c702043 100644 --- a/lib/entry/libimagentryview/Cargo.toml +++ b/lib/entry/libimagentryview/Cargo.toml @@ -23,6 +23,7 @@ maintenance = { status = "actively-developed" } log = "0.4.0" toml = "0.4" error-chain = "0.11" +textwrap = "0.9" libimagrt = { version = "0.8.0", path = "../../../lib/core/libimagrt" } libimagstore = { version = "0.8.0", path = "../../../lib/core/libimagstore" } diff --git a/lib/entry/libimagentryview/src/builtin/stdout.rs b/lib/entry/libimagentryview/src/builtin/stdout.rs index 1aa7b97e..5a270745 100644 --- a/lib/entry/libimagentryview/src/builtin/stdout.rs +++ b/lib/entry/libimagentryview/src/builtin/stdout.rs @@ -27,6 +27,7 @@ use error::Result; pub struct StdoutViewer { view_header: bool, view_content: bool, + wrap_content: Option, } impl StdoutViewer { @@ -35,9 +36,14 @@ impl StdoutViewer { StdoutViewer { view_header: view_header, view_content: view_content, + wrap_content: None, } } + pub fn wrap_at(&mut self, wrap: usize) { + self.wrap_content = Some(wrap) + } + } impl Viewer for StdoutViewer { @@ -48,7 +54,12 @@ impl Viewer for StdoutViewer { } if self.view_content { - println!("{}", e.get_content()); + match self.wrap_content { + Some(limit) => ::textwrap::wrap(e.get_content(), limit).iter().for_each(|line| { + println!("{}", line) + }), + None => println!("{}", e.get_content()), + } } Ok(()) diff --git a/lib/entry/libimagentryview/src/lib.rs b/lib/entry/libimagentryview/src/lib.rs index b543c6f8..170f415c 100644 --- a/lib/entry/libimagentryview/src/lib.rs +++ b/lib/entry/libimagentryview/src/lib.rs @@ -37,6 +37,7 @@ extern crate toml; #[macro_use] extern crate error_chain; +extern crate textwrap; extern crate libimagstore; extern crate libimagrt;