keyfork-prompt: add DefaultPromptManager

This commit is contained in:
Ryan Heywood 2023-12-21 15:44:57 -05:00
parent bfb44292f4
commit 920e04ba23
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
3 changed files with 24 additions and 42 deletions

View File

@ -1,13 +1,13 @@
use std::{ use std::{
io::{BufRead, BufReader, Read, Write}, io::{stderr, stdin, BufRead, BufReader, Read, Stderr, Stdin, Write},
os::fd::AsRawFd, os::fd::AsRawFd,
}; };
use crossterm::{ use crossterm::{
cursor,
event::{read, Event, KeyCode}, event::{read, Event, KeyCode},
style::{Print, PrintStyledContent, Stylize}, style::{Print, PrintStyledContent, Stylize},
terminal, terminal,
cursor,
tty::IsTty, tty::IsTty,
QueueableCommand, QueueableCommand,
}; };
@ -57,9 +57,9 @@ where
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
terminal.queue(Print(line))?; terminal.queue(Print(line))?;
if lines.peek().is_some() { if lines.peek().is_some() {
terminal terminal
.queue(cursor::MoveDown(1))? .queue(cursor::MoveDown(1))?
.queue(cursor::MoveToColumn(0))?; .queue(cursor::MoveToColumn(0))?;
} }
} }
terminal.flush()?; terminal.flush()?;
@ -81,9 +81,9 @@ where
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
terminal.queue(Print(line))?; terminal.queue(Print(line))?;
if lines.peek().is_some() { if lines.peek().is_some() {
terminal terminal
.queue(cursor::MoveDown(1))? .queue(cursor::MoveDown(1))?
.queue(cursor::MoveToColumn(0))?; .queue(cursor::MoveToColumn(0))?;
} }
} }
terminal.flush()?; terminal.flush()?;
@ -128,9 +128,9 @@ where
while let Some(line) = lines.next() { while let Some(line) = lines.next() {
terminal.queue(Print(line))?; terminal.queue(Print(line))?;
if lines.peek().is_some() { if lines.peek().is_some() {
terminal terminal
.queue(cursor::MoveDown(1))? .queue(cursor::MoveDown(1))?
.queue(cursor::MoveToColumn(0))?; .queue(cursor::MoveToColumn(0))?;
} }
} }
terminal terminal
@ -153,3 +153,9 @@ where
Ok(()) Ok(())
} }
} }
pub type DefaultPromptManager = PromptManager<Stdin, Stderr>;
pub fn default_prompt_manager() -> Result<DefaultPromptManager> {
PromptManager::new(stdin(), stderr())
}

View File

@ -1,6 +1,4 @@
use std::fs::File; use keyfork_prompt::{Error as PromptError, DefaultPromptManager, default_prompt_manager};
use keyfork_prompt::{Error as PromptError, PromptManager};
use super::openpgp::{ use super::openpgp::{
self, self,
@ -18,12 +16,6 @@ pub enum Error {
#[error("Secret key was not found")] #[error("Secret key was not found")]
SecretKeyNotFound, SecretKeyNotFound,
#[error("Could not find TTY when prompting")]
NoTTY,
#[error("Could not open TTY: {0}")]
Io(#[from] std::io::Error),
#[error("Prompt failed: {0}")] #[error("Prompt failed: {0}")]
Prompt(#[from] PromptError), Prompt(#[from] PromptError),
} }
@ -33,20 +25,15 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
pub struct Keyring { pub struct Keyring {
full_certs: Vec<Cert>, full_certs: Vec<Cert>,
root: Option<Cert>, root: Option<Cert>,
pm: PromptManager<File, File>, pm: DefaultPromptManager,
} }
impl Keyring { impl Keyring {
pub fn new(certs: impl AsRef<[Cert]>) -> Result<Self> { pub fn new(certs: impl AsRef<[Cert]>) -> Result<Self> {
let tty = std::env::vars()
.filter(|(k, _v)| k.as_str() == "GPG_TTY")
.next()
.ok_or(Error::NoTTY)?
.1;
Ok(Self { Ok(Self {
full_certs: certs.as_ref().to_vec(), full_certs: certs.as_ref().to_vec(),
root: Default::default(), root: Default::default(),
pm: PromptManager::new(File::open(&tty)?, File::options().write(true).open(&tty)?)?, pm: default_prompt_manager()?,
}) })
} }

View File

@ -1,6 +1,6 @@
use std::{collections::HashSet, fs::File}; use std::{collections::HashSet};
use keyfork_prompt::{Error as PromptError, PromptManager}; use keyfork_prompt::{Error as PromptError, DefaultPromptManager, default_prompt_manager};
use super::openpgp::{ use super::openpgp::{
self, self,
@ -44,12 +44,6 @@ pub enum Error {
#[error("Invalid PIN entered too many times")] #[error("Invalid PIN entered too many times")]
InvalidPIN, InvalidPIN,
#[error("Could not find TTY when prompting")]
NoTTY,
#[error("Could not open TTY: {0}")]
Io(#[from] std::io::Error),
#[error("Prompt failed: {0}")] #[error("Prompt failed: {0}")]
Prompt(#[from] PromptError), Prompt(#[from] PromptError),
} }
@ -70,20 +64,15 @@ fn format_name(input: impl AsRef<str>) -> String {
pub struct SmartcardManager { pub struct SmartcardManager {
current_card: Option<Card<Open>>, current_card: Option<Card<Open>>,
root: Option<Cert>, root: Option<Cert>,
pm: PromptManager<File, File>, pm: DefaultPromptManager,
} }
impl SmartcardManager { impl SmartcardManager {
pub fn new() -> Result<Self> { pub fn new() -> Result<Self> {
let tty = std::env::vars()
.filter(|(k, _v)| k.as_str() == "GPG_TTY")
.next()
.ok_or(Error::NoTTY)?
.1;
Ok(Self { Ok(Self {
current_card: None, current_card: None,
root: None, root: None,
pm: PromptManager::new(File::open(&tty)?, File::options().write(true).open(&tty)?)?, pm: default_prompt_manager()?,
}) })
} }