Skip to content

Commit

Permalink
feat: implement otp uri copying from QRCode page
Browse files Browse the repository at this point in the history
  • Loading branch information
replydev committed May 2, 2024
1 parent e80d60f commit 1120f07
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
28 changes: 22 additions & 6 deletions src/interface/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const LARGE_APPLICATION_WIDTH: u16 = 75;
/// Application result type.
pub type AppResult<T> = Result<T, Box<dyn error::Error>>;

const DEFAULT_QRCODE_LABEL: &'static str = "Press enter to copy the OTP URI code";

/// Application.
pub struct App<'a> {
/// Is the application running?
Expand All @@ -36,6 +38,9 @@ pub struct App<'a> {
pub(crate) search_query: String,
pub(crate) focus: Focus,
pub(crate) popup: Popup,

/// Info text in the QRCode page
pub(crate) qr_code_page_label: &'static str,
}

pub struct Popup {
Expand All @@ -60,7 +65,7 @@ impl<'a> App<'a> {
progress: percentage(),
label_text: String::from(""),
print_percentage: true,
current_page: Main,
current_page: Page::default(),
search_query: String::from(""),
focus: Focus::MainPage,
popup: Popup {
Expand All @@ -69,9 +74,16 @@ impl<'a> App<'a> {
percent_x: 60,
percent_y: 20,
},
qr_code_page_label: DEFAULT_QRCODE_LABEL,
}
}

pub(crate) fn reset(&mut self) {
self.current_page = Page::default();
self.print_percentage = true;
self.qr_code_page_label = DEFAULT_QRCODE_LABEL;
}

/// Handles the tick event of the terminal.
pub fn tick(&mut self, force_update: bool) {
// Update progress bar
Expand Down Expand Up @@ -105,11 +117,15 @@ impl<'a> App<'a> {
} else {
format!("{} - {}", &element.issuer, &element.label)
};
Paragraph::new(element.get_qrcode())
.block(Block::default().title(title).borders(Borders::ALL))
.style(Style::default().fg(Color::White).bg(Color::Reset))
.alignment(Alignment::Center)
.wrap(Wrap { trim: true })
Paragraph::new(format!(
"{}\n{}",
element.get_qrcode(),
self.qr_code_page_label
))
.block(Block::default().title(title).borders(Borders::ALL))
.style(Style::default().fg(Color::White).bg(Color::Reset))
.alignment(Alignment::Center)
.wrap(Wrap { trim: true })
})
.unwrap_or_else(|| {
Paragraph::new("No element is selected")
Expand Down
27 changes: 21 additions & 6 deletions src/interface/handlers/main_window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use super::{
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::{
clipboard::copy_string_to_clipboard,
interface::{
app::{App, Popup},
enums::{Focus, Page, PopupAction},
Expand Down Expand Up @@ -102,10 +103,25 @@ pub(super) fn main_handler(key_event: KeyEvent, app: &mut App) {

KeyCode::Char('/') => app.focus = Focus::SearchBar,

KeyCode::Enter => {
app.label_text = copy_selected_code_to_clipboard(app);
app.print_percentage = false;
}
KeyCode::Enter => match app.current_page {
Main => {
app.label_text = copy_selected_code_to_clipboard(app);
app.print_percentage = false;
}
Qrcode => {
let selected_element = app
.table
.state
.selected()
.and_then(|index| app.database.elements_ref().get(index));

if let Some(element) = selected_element {
let otp_uri = element.get_otpauth_uri();
let _ = copy_string_to_clipboard(&otp_uri);
app.qr_code_page_label = "OTP URI Copied to clipboard";
}
}
},
_ => {}
}
}
Expand All @@ -128,9 +144,8 @@ fn handle_counter_switch(app: &mut App, increment: bool) {
}

fn handle_switch_page(app: &mut App, page: Page) {
let default_page = Main;
if app.current_page == page {
app.current_page = default_page;
app.reset();
} else {
app.current_page = page;
}
Expand Down

0 comments on commit 1120f07

Please sign in to comment.