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_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())?;
|
let mut mgr = PromptManager::new(stdin(), stdout())?;
|
||||||
mgr.prompt_passphrase("Passphrase: ")?;
|
mgr.prompt_passphrase("Passphrase: ")?;
|
||||||
let string = mgr.prompt_wordlist("Mnemonic: ", &Default::default())?;
|
let string = mgr.prompt_wordlist("Mnemonic: ", &Default::default())?;
|
||||||
let mnemonic = Mnemonic::from_str(&string).unwrap();
|
let mnemonic = Mnemonic::from_str(&string).unwrap();
|
||||||
let entropy = mnemonic.entropy();
|
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)?;
|
let qrcode = qrencode::qrencode(&string)?;
|
||||||
mgr.prompt_message(Message::Data(qrcode))?;
|
mgr.prompt_message(&Message::Data(qrcode))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,8 +17,8 @@ use crossterm::{
|
||||||
|
|
||||||
mod alternate_screen;
|
mod alternate_screen;
|
||||||
mod raw_mode;
|
mod raw_mode;
|
||||||
use alternate_screen::*;
|
use alternate_screen::AlternateScreen;
|
||||||
use raw_mode::*;
|
use raw_mode::RawMode;
|
||||||
|
|
||||||
#[cfg(feature = "qrencode")]
|
#[cfg(feature = "qrencode")]
|
||||||
pub mod qrencode;
|
pub mod qrencode;
|
||||||
|
@ -82,6 +82,7 @@ where
|
||||||
|
|
||||||
// TODO: create a wrapper for bracketed paste similar to RawMode
|
// TODO: create a wrapper for bracketed paste similar to RawMode
|
||||||
#[cfg(feature = "mnemonic")]
|
#[cfg(feature = "mnemonic")]
|
||||||
|
#[allow(clippy::too_many_lines)]
|
||||||
pub fn prompt_wordlist(&mut self, prompt: &str, wordlist: &Wordlist) -> Result<String> {
|
pub fn prompt_wordlist(&mut self, prompt: &str, wordlist: &Wordlist) -> Result<String> {
|
||||||
let mut terminal = AlternateScreen::new(&mut self.write)?;
|
let mut terminal = AlternateScreen::new(&mut self.write)?;
|
||||||
let mut terminal = RawMode::new(&mut terminal)?;
|
let mut terminal = RawMode::new(&mut terminal)?;
|
||||||
|
@ -137,7 +138,7 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
KeyCode::Char(' ') => {
|
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(' ');
|
input.push(' ');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,21 +152,27 @@ where
|
||||||
|
|
||||||
let usable_space = cols as usize - prefix_length - 1;
|
let usable_space = cols as usize - prefix_length - 1;
|
||||||
|
|
||||||
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
terminal
|
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))?
|
.queue(terminal::Clear(terminal::ClearType::UntilNewLine))?
|
||||||
.flush()?;
|
.flush()?;
|
||||||
|
|
||||||
let printable_input_start = if input.len() > usable_space {
|
let printable_input_start = if input.len() > usable_space {
|
||||||
let start_index = 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()
|
.chars()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.skip(start_index)
|
.skip(start_index)
|
||||||
.skip_while(|(_, ch)| !ch.is_whitespace())
|
.find(|(_, ch)| ch.is_whitespace())
|
||||||
.next()
|
{
|
||||||
.expect("any printable character")
|
index
|
||||||
.0
|
} else {
|
||||||
|
start_index
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
};
|
};
|
||||||
|
@ -182,8 +189,7 @@ where
|
||||||
if iter.peek().is_some()
|
if iter.peek().is_some()
|
||||||
|| printable_input
|
|| printable_input
|
||||||
.chars()
|
.chars()
|
||||||
.rev()
|
.next_back()
|
||||||
.next()
|
|
||||||
.is_some_and(char::is_whitespace)
|
.is_some_and(char::is_whitespace)
|
||||||
{
|
{
|
||||||
terminal.queue(Print(" "))?;
|
terminal.queue(Print(" "))?;
|
||||||
|
@ -259,7 +265,7 @@ where
|
||||||
Ok(passphrase)
|
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 = AlternateScreen::new(&mut self.write)?;
|
||||||
let mut terminal = RawMode::new(&mut terminal)?;
|
let mut terminal = RawMode::new(&mut terminal)?;
|
||||||
|
|
||||||
|
@ -270,13 +276,13 @@ where
|
||||||
.queue(terminal::Clear(terminal::ClearType::All))?
|
.queue(terminal::Clear(terminal::ClearType::All))?
|
||||||
.queue(cursor::MoveTo(0, 0))?;
|
.queue(cursor::MoveTo(0, 0))?;
|
||||||
|
|
||||||
use Message::*;
|
|
||||||
match &prompt {
|
match &prompt {
|
||||||
Text(text) => {
|
Message::Text(text) => {
|
||||||
for line in text.lines() {
|
for line in text.lines() {
|
||||||
let mut written_chars = 0;
|
let mut written_chars = 0;
|
||||||
for word in line.split_whitespace() {
|
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;
|
written_chars += len + 1;
|
||||||
if written_chars > cols {
|
if written_chars > cols {
|
||||||
terminal
|
terminal
|
||||||
|
@ -291,7 +297,7 @@ where
|
||||||
.queue(cursor::MoveToColumn(0))?;
|
.queue(cursor::MoveToColumn(0))?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Data(data) => {
|
Message::Data(data) => {
|
||||||
let count = data.lines().count();
|
let count = data.lines().count();
|
||||||
// NOTE: GE to allow a MoveDown(1)
|
// NOTE: GE to allow a MoveDown(1)
|
||||||
if count >= rows as usize {
|
if count >= rows as usize {
|
||||||
|
@ -318,9 +324,10 @@ where
|
||||||
.queue(PrintStyledContent(" OK ".negative()))?
|
.queue(PrintStyledContent(" OK ".negative()))?
|
||||||
.flush()?;
|
.flush()?;
|
||||||
|
|
||||||
|
#[allow(clippy::single_match)]
|
||||||
match read()? {
|
match read()? {
|
||||||
Event::Key(k) => match k.code {
|
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