Skip to content

Commit

Permalink
Add is_tty field to Context
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robinkrahl authored and d-e-s-o committed Jan 11, 2021
1 parent b23296e commit 763219b
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Unreleased
----------
- Added the `fill` command that fills the SD card of a Nitrokey Storage device
with random data
- Added the `termion` dependency in version `1.5.5`
- Added SD card usage information to the output of the `status` command for
Storage devices

Expand Down
28 changes: 28 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ features = ["derive"]
version = "0.3.17"
default-features = false

[dependencies.termion]
version = "1.5.5"

[dependencies.toml]
version = "0.5.6"

Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -118,14 +120,20 @@ 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,
{
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),
Expand Down Expand Up @@ -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)
}
Expand Down
1 change: 1 addition & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 763219b

Please sign in to comment.