keyfork-prompt: make clippy happy
This commit is contained in:
parent
f88a4d21f2
commit
e3e7f0bf44
|
@ -1,16 +1,19 @@
|
|||
use std::{io::{stdin, stdout}, str::FromStr};
|
||||
use std::{
|
||||
io::{stdin, stdout},
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
use keyfork_prompt::*;
|
||||
use keyfork_mnemonic_util::Mnemonic;
|
||||
use keyfork_prompt::{qrencode, Message, PromptManager};
|
||||
|
||||
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let mut mgr = PromptManager::new(stdin(), stdout())?;
|
||||
mgr.prompt_passphrase("Passphrase: ")?;
|
||||
let string = mgr.prompt_wordlist("Mnemonic: ", &Default::default())?;
|
||||
let mnemonic = Mnemonic::from_str(&string).unwrap();
|
||||
let entropy = mnemonic.entropy();
|
||||
mgr.prompt_message(Message::Text(format!("Your entropy is: {entropy:X?}")))?;
|
||||
mgr.prompt_message(&Message::Text(format!("Your entropy is: {entropy:X?}")))?;
|
||||
let qrcode = qrencode::qrencode(&string)?;
|
||||
mgr.prompt_message(Message::Data(qrcode))?;
|
||||
mgr.prompt_message(&Message::Data(qrcode))?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ use crossterm::{
|
|||
|
||||
mod alternate_screen;
|
||||
mod raw_mode;
|
||||
use alternate_screen::*;
|
||||
use raw_mode::*;
|
||||
use alternate_screen::AlternateScreen;
|
||||
use raw_mode::RawMode;
|
||||
|
||||
#[cfg(feature = "qrencode")]
|
||||
pub mod qrencode;
|
||||
|
@ -82,6 +82,7 @@ where
|
|||
|
||||
// TODO: create a wrapper for bracketed paste similar to RawMode
|
||||
#[cfg(feature = "mnemonic")]
|
||||
#[allow(clippy::too_many_lines)]
|
||||
pub fn prompt_wordlist(&mut self, prompt: &str, wordlist: &Wordlist) -> Result<String> {
|
||||
let mut terminal = AlternateScreen::new(&mut self.write)?;
|
||||
let mut terminal = RawMode::new(&mut terminal)?;
|
||||
|
@ -137,7 +138,7 @@ where
|
|||
}
|
||||
}
|
||||
KeyCode::Char(' ') => {
|
||||
if !input.chars().rev().next().is_some_and(char::is_whitespace) {
|
||||
if !input.chars().next_back().is_some_and(char::is_whitespace) {
|
||||
input.push(' ');
|
||||
}
|
||||
}
|
||||
|
@ -151,21 +152,27 @@ where
|
|||
|
||||
let usable_space = cols as usize - prefix_length - 1;
|
||||
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
terminal
|
||||
.queue(cursor::MoveToColumn(prefix_length as u16))?
|
||||
.queue(cursor::MoveToColumn(
|
||||
std::cmp::min(u16::MAX as usize, prefix_length) as u16,
|
||||
))?
|
||||
.queue(terminal::Clear(terminal::ClearType::UntilNewLine))?
|
||||
.flush()?;
|
||||
|
||||
let printable_input_start = if input.len() > usable_space {
|
||||
let start_index = input.len() - usable_space;
|
||||
input
|
||||
// Find a word boundary, otherwise slice the current word in half
|
||||
if let Some((index, _)) = input
|
||||
.chars()
|
||||
.enumerate()
|
||||
.skip(start_index)
|
||||
.skip_while(|(_, ch)| !ch.is_whitespace())
|
||||
.next()
|
||||
.expect("any printable character")
|
||||
.0
|
||||
.find(|(_, ch)| ch.is_whitespace())
|
||||
{
|
||||
index
|
||||
} else {
|
||||
start_index
|
||||
}
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
@ -182,8 +189,7 @@ where
|
|||
if iter.peek().is_some()
|
||||
|| printable_input
|
||||
.chars()
|
||||
.rev()
|
||||
.next()
|
||||
.next_back()
|
||||
.is_some_and(char::is_whitespace)
|
||||
{
|
||||
terminal.queue(Print(" "))?;
|
||||
|
@ -259,7 +265,7 @@ where
|
|||
Ok(passphrase)
|
||||
}
|
||||
|
||||
pub fn prompt_message(&mut self, prompt: Message) -> Result<()> {
|
||||
pub fn prompt_message(&mut self, prompt: &Message) -> Result<()> {
|
||||
let mut terminal = AlternateScreen::new(&mut self.write)?;
|
||||
let mut terminal = RawMode::new(&mut terminal)?;
|
||||
|
||||
|
@ -270,13 +276,13 @@ where
|
|||
.queue(terminal::Clear(terminal::ClearType::All))?
|
||||
.queue(cursor::MoveTo(0, 0))?;
|
||||
|
||||
use Message::*;
|
||||
match &prompt {
|
||||
Text(text) => {
|
||||
Message::Text(text) => {
|
||||
for line in text.lines() {
|
||||
let mut written_chars = 0;
|
||||
for word in line.split_whitespace() {
|
||||
let len = word.len() as u16;
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
let len = std::cmp::min(u16::MAX as usize, word.len()) as u16;
|
||||
written_chars += len + 1;
|
||||
if written_chars > cols {
|
||||
terminal
|
||||
|
@ -291,7 +297,7 @@ where
|
|||
.queue(cursor::MoveToColumn(0))?;
|
||||
}
|
||||
}
|
||||
Data(data) => {
|
||||
Message::Data(data) => {
|
||||
let count = data.lines().count();
|
||||
// NOTE: GE to allow a MoveDown(1)
|
||||
if count >= rows as usize {
|
||||
|
@ -318,9 +324,10 @@ where
|
|||
.queue(PrintStyledContent(" OK ".negative()))?
|
||||
.flush()?;
|
||||
|
||||
#[allow(clippy::single_match)]
|
||||
match read()? {
|
||||
Event::Key(k) => match k.code {
|
||||
KeyCode::Enter | KeyCode::Char(' ') | KeyCode::Char('q') => break,
|
||||
KeyCode::Enter | KeyCode::Char(' ' | 'q') => break,
|
||||
_ => (),
|
||||
},
|
||||
_ => (),
|
||||
|
|
Loading…
Reference in New Issue