Skip to content

Commit 1120f07

Browse files
committed
feat: implement otp uri copying from QRCode page
1 parent e80d60f commit 1120f07

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/interface/app.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const LARGE_APPLICATION_WIDTH: u16 = 75;
2121
/// Application result type.
2222
pub type AppResult<T> = Result<T, Box<dyn error::Error>>;
2323

24+
const DEFAULT_QRCODE_LABEL: &'static str = "Press enter to copy the OTP URI code";
25+
2426
/// Application.
2527
pub struct App<'a> {
2628
/// Is the application running?
@@ -36,6 +38,9 @@ pub struct App<'a> {
3638
pub(crate) search_query: String,
3739
pub(crate) focus: Focus,
3840
pub(crate) popup: Popup,
41+
42+
/// Info text in the QRCode page
43+
pub(crate) qr_code_page_label: &'static str,
3944
}
4045

4146
pub struct Popup {
@@ -60,7 +65,7 @@ impl<'a> App<'a> {
6065
progress: percentage(),
6166
label_text: String::from(""),
6267
print_percentage: true,
63-
current_page: Main,
68+
current_page: Page::default(),
6469
search_query: String::from(""),
6570
focus: Focus::MainPage,
6671
popup: Popup {
@@ -69,9 +74,16 @@ impl<'a> App<'a> {
6974
percent_x: 60,
7075
percent_y: 20,
7176
},
77+
qr_code_page_label: DEFAULT_QRCODE_LABEL,
7278
}
7379
}
7480

81+
pub(crate) fn reset(&mut self) {
82+
self.current_page = Page::default();
83+
self.print_percentage = true;
84+
self.qr_code_page_label = DEFAULT_QRCODE_LABEL;
85+
}
86+
7587
/// Handles the tick event of the terminal.
7688
pub fn tick(&mut self, force_update: bool) {
7789
// Update progress bar
@@ -105,11 +117,15 @@ impl<'a> App<'a> {
105117
} else {
106118
format!("{} - {}", &element.issuer, &element.label)
107119
};
108-
Paragraph::new(element.get_qrcode())
109-
.block(Block::default().title(title).borders(Borders::ALL))
110-
.style(Style::default().fg(Color::White).bg(Color::Reset))
111-
.alignment(Alignment::Center)
112-
.wrap(Wrap { trim: true })
120+
Paragraph::new(format!(
121+
"{}\n{}",
122+
element.get_qrcode(),
123+
self.qr_code_page_label
124+
))
125+
.block(Block::default().title(title).borders(Borders::ALL))
126+
.style(Style::default().fg(Color::White).bg(Color::Reset))
127+
.alignment(Alignment::Center)
128+
.wrap(Wrap { trim: true })
113129
})
114130
.unwrap_or_else(|| {
115131
Paragraph::new("No element is selected")

src/interface/handlers/main_window.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use super::{
55
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
66

77
use crate::{
8+
clipboard::copy_string_to_clipboard,
89
interface::{
910
app::{App, Popup},
1011
enums::{Focus, Page, PopupAction},
@@ -102,10 +103,25 @@ pub(super) fn main_handler(key_event: KeyEvent, app: &mut App) {
102103

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

105-
KeyCode::Enter => {
106-
app.label_text = copy_selected_code_to_clipboard(app);
107-
app.print_percentage = false;
108-
}
106+
KeyCode::Enter => match app.current_page {
107+
Main => {
108+
app.label_text = copy_selected_code_to_clipboard(app);
109+
app.print_percentage = false;
110+
}
111+
Qrcode => {
112+
let selected_element = app
113+
.table
114+
.state
115+
.selected()
116+
.and_then(|index| app.database.elements_ref().get(index));
117+
118+
if let Some(element) = selected_element {
119+
let otp_uri = element.get_otpauth_uri();
120+
let _ = copy_string_to_clipboard(&otp_uri);
121+
app.qr_code_page_label = "OTP URI Copied to clipboard";
122+
}
123+
}
124+
},
109125
_ => {}
110126
}
111127
}
@@ -128,9 +144,8 @@ fn handle_counter_switch(app: &mut App, increment: bool) {
128144
}
129145

130146
fn handle_switch_page(app: &mut App, page: Page) {
131-
let default_page = Main;
132147
if app.current_page == page {
133-
app.current_page = default_page;
148+
app.reset();
134149
} else {
135150
app.current_page = page;
136151
}

0 commit comments

Comments
 (0)