-
Notifications
You must be signed in to change notification settings - Fork 10
Add fill command to overwrite SD card #125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2ba357c
Add fill command to overwrite SD card
robinkrahl b58bf85
Add is_tty field to Context
robinkrahl b853402
Show progress bar in fill output
robinkrahl 5f972f8
fixup! Add fill command to overwrite SD card
robinkrahl 2f5e81e
fixup! Add is_tty field to Context
robinkrahl fa57018
fixup! Show progress bar in fill output
robinkrahl 9f226ec
fixup! Add is_tty field to Context
robinkrahl 5c464a0
fixup! Show progress bar in fill output
robinkrahl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
// commands.rs | ||
|
||
// Copyright (C) 2020 The Nitrocli Developers | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use crate::Context; | ||
|
||
/// A progress bar that can be printed to an interactive output. | ||
pub struct ProgressBar { | ||
/// Whether to redraw the entire progress bar in the next call to `draw`. | ||
redraw: bool, | ||
/// The current progress of the progress bar (0 <= progress <= 100). | ||
progress: u8, | ||
/// Toogled on every call to `draw` to print a pulsing indicator. | ||
toggle: bool, | ||
/// Whether this progress bar finished. | ||
finished: bool, | ||
} | ||
|
||
impl ProgressBar { | ||
/// Creates a new empty progress bar. | ||
pub fn new() -> ProgressBar { | ||
ProgressBar { | ||
redraw: true, | ||
progress: 0, | ||
toggle: false, | ||
finished: false, | ||
} | ||
} | ||
|
||
/// Whether this progress bar is finished. | ||
pub fn is_finished(&self) -> bool { | ||
self.finished | ||
} | ||
|
||
/// Updates the progress bar with the given progress (0 <= progress <= 100). | ||
pub fn update(&mut self, progress: u8) -> anyhow::Result<()> { | ||
anyhow::ensure!(!self.finished, "Tried to update finished progress bar"); | ||
anyhow::ensure!( | ||
progress <= 100, | ||
"Progress bar value out of range: {}", | ||
progress | ||
); | ||
if progress != self.progress { | ||
self.redraw = true; | ||
self.progress = progress; | ||
} | ||
self.toggle = !self.toggle; | ||
Ok(()) | ||
} | ||
|
||
/// Finish this progress bar. | ||
/// | ||
/// A finished progress bar may no longer be updated. | ||
pub fn finish(&mut self) { | ||
self.finished = true; | ||
self.redraw = true; | ||
self.progress = 100; | ||
} | ||
|
||
/// Print the progress bar to the stdout set in the given context. | ||
/// | ||
/// On every call of this method (as long as the progress bar is not finished), a pulsing | ||
/// indicator is printed to show that the process is still running. If there was progress since | ||
/// the last call to `draw`, or if this is the first call, this function will also print the | ||
/// progress bar itself. | ||
pub fn draw(&self, ctx: &mut Context<'_>) -> anyhow::Result<()> { | ||
use anyhow::Context as _; | ||
use termion::cursor::DetectCursorPos as _; | ||
use termion::raw::IntoRawMode as _; | ||
|
||
if !ctx.is_tty { | ||
return Ok(()); | ||
} | ||
|
||
let pos = ctx | ||
.stdout | ||
.into_raw_mode() | ||
.context("Failed to activate raw mode")? | ||
.cursor_pos() | ||
.context("Failed to query cursor position")?; | ||
let progress_char = if self.toggle && !self.finished { | ||
"." | ||
} else { | ||
" " | ||
}; | ||
if self.redraw { | ||
use progressing::Baring as _;; | ||
|
||
let mut progress_bar = progressing::mapping::Bar::with_range(0, 100); | ||
progress_bar.set(self.progress); | ||
|
||
print!(ctx, "{}", termion::clear::CurrentLine)?; | ||
print!(ctx, "{}", termion::cursor::Goto(1, pos.1))?; | ||
print!(ctx, " {} {}", progress_char, progress_bar)?; | ||
if self.finished { | ||
println!(ctx)?; | ||
} | ||
} else { | ||
print!(ctx, "{}{}", termion::cursor::Goto(2, pos.1), progress_char)?; | ||
} | ||
|
||
ctx.stdout.flush()?; | ||
Ok(()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// fill.rs | ||
|
||
// Copyright (C) 2020 The Nitrocli Developers | ||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
use super::*; | ||
|
||
// Ignore this test as it takes about one hour to execute | ||
#[ignore] | ||
#[test_device(storage)] | ||
fn fill(model: nitrokey::Model) -> anyhow::Result<()> { | ||
let res = Nitrocli::new().model(model).handle(&["fill"]); | ||
assert!(res.is_ok()); | ||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.