aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-09-10 13:34:46 +0200
committerDaniel Mueller <deso@posteo.net>2021-01-10 21:05:44 -0800
commit763219bc4331331351b4180d4775432e9e11f8b2 (patch)
tree1162cd1f662a2e2d2a877746fb125941b80d9fea /src
parentb23296e1cc47cf5350835d8a8cca6eb6f5d7d270 (diff)
downloadnitrocli-763219bc4331331351b4180d4775432e9e11f8b2.tar.gz
nitrocli-763219bc4331331351b4180d4775432e9e11f8b2.tar.bz2
Add is_tty field to Context
This patch adds the is_tty field to the Context struct that indicates whether stdout is a TTY. This allows us to use TTY features like moving the cursor in our output.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs13
-rw-r--r--src/tests/mod.rs1
2 files changed, 12 insertions, 2 deletions
diff --git a/src/main.rs b/src/main.rs
index baad15c..1a2a3d3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -98,6 +98,8 @@ pub struct Context<'io> {
pub stdout: &'io mut dyn io::Write,
/// The `Write` object used as standard error throughout the program.
pub stderr: &'io mut dyn io::Write,
+ /// Whether `stdout` is a TTY.
+ pub is_tty: bool,
/// The admin PIN, if provided through an environment variable.
pub admin_pin: Option<ffi::OsString>,
/// The user PIN, if provided through an environment variable.
@@ -118,7 +120,12 @@ pub struct Context<'io> {
}
impl<'io> Context<'io> {
- fn from_env<O, E>(stdout: &'io mut O, stderr: &'io mut E, config: config::Config) -> Context<'io>
+ fn from_env<O, E>(
+ stdout: &'io mut O,
+ stderr: &'io mut E,
+ is_tty: bool,
+ config: config::Config,
+ ) -> Context<'io>
where
O: io::Write,
E: io::Write,
@@ -126,6 +133,7 @@ impl<'io> Context<'io> {
Context {
stdout,
stderr,
+ is_tty,
admin_pin: env::var_os(NITROCLI_ADMIN_PIN),
user_pin: env::var_os(NITROCLI_USER_PIN),
new_admin_pin: env::var_os(NITROCLI_NEW_ADMIN_PIN),
@@ -154,8 +162,9 @@ fn main() {
let rc = match config::Config::load() {
Ok(config) => {
+ let is_tty = termion::is_tty(&stdout);
let args = env::args().collect::<Vec<_>>();
- let ctx = &mut Context::from_env(&mut stdout, &mut stderr, config);
+ let ctx = &mut Context::from_env(&mut stdout, &mut stderr, is_tty, config);
run(ctx, args)
}
diff --git a/src/tests/mod.rs b/src/tests/mod.rs
index e0a5b9a..1871f3c 100644
--- a/src/tests/mod.rs
+++ b/src/tests/mod.rs
@@ -99,6 +99,7 @@ impl Nitrocli {
let ctx = &mut crate::Context {
stdout: &mut stdout,
stderr: &mut stderr,
+ is_tty: false,
admin_pin: self.admin_pin.clone(),
user_pin: self.user_pin.clone(),
new_admin_pin: self.new_admin_pin.clone(),