kptui 0.20.0

TUI password manager for KeePass vaults
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use ratatui::{
	Frame,
	layout::{Alignment, Constraint, Direction, Layout, Rect},
	style::Style,
	text::{Line, Span},
	widgets::{Block, Borders, List, ListItem, Padding, Paragraph},
};

use crate::app::App;
use crate::input::settings::{AUTO_LOCK_ROW, DATABASE_ROW, KEYFILE_ROW, THEME_ROW};
use crate::util::wrap_help_items;

fn centered_cursor_x(input_area: Rect, typed_len: u16) -> u16 {
	let inner = Block::default().borders(Borders::ALL).inner(input_area);
	inner.x + (inner.width / 2).saturating_sub(typed_len / 2) + typed_len
}

fn nav_help_items(slim_mode: bool) -> &'static [&'static str] {
	if slim_mode { &["[↑↓]", "[Enter]", "[Esc]"] } else { &["[↑↓] Navigate", "[Enter] Modify", "[Esc] Back"] }
}

fn field_help_items(slim_mode: bool) -> &'static [&'static str] {
	if slim_mode { &["[Enter]", "[Esc]"] } else { &["[Enter] Save", "[Esc] Cancel"] }
}

fn change_password_help_items(slim_mode: bool) -> &'static [&'static str] {
	if slim_mode { &["[Enter]", "[Esc]"] } else { &["[Enter] Confirm", "[Esc] Cancel"] }
}

fn export_help_items(slim_mode: bool) -> &'static [&'static str] {
	if slim_mode { &["[Enter]", "[Esc]"] } else { &["[Enter] Continue", "[Esc] Cancel"] }
}

fn import_help_items(slim_mode: bool) -> &'static [&'static str] {
	if slim_mode { &["[Enter]", "[Esc]"] } else { &["[Enter] Continue", "[Esc] Cancel"] }
}

pub fn draw_settings(frame: &mut Frame, app: &mut App) {
	if app.exporting_database {
		draw_export(frame, app);
	} else if app.importing_database {
		draw_import(frame, app);
	} else if app.changing_password {
		draw_change_password(frame, app);
	} else if app.choosing_theme {
		draw_theme_picker(frame, app);
	} else {
		draw_main_settings(frame, app);
	}
}

fn draw_main_settings(frame: &mut Frame, app: &mut App) {
	let full_area = frame.area();

	let editing_field = app.editing_field;
	let selected = app.settings_state.selected().unwrap_or(0);

	let help_items = if editing_field { field_help_items(app.slim_mode) } else { nav_help_items(app.slim_mode) };

	let help_width = full_area.width.saturating_sub(2);
	let help_lines = wrap_help_items(help_items, help_width);
	let help_height = help_lines.len() as u16;

	let vertical = Layout::default()
		.direction(Direction::Vertical)
		.constraints([Constraint::Fill(1), Constraint::Length(help_height)])
		.split(full_area);

	let theme = &app.theme;
	let normal = Style::new().fg(theme.text);
	let accent = Style::new().fg(theme.accent);
	let warning = Style::new().fg(theme.warning);
	let border_style = Style::new().fg(theme.border);
	let placeholder = Style::new().fg(theme.text).bold();
	let editing_style = Style::new().fg(theme.selection_fg).bg(theme.selection_bg);
	let label_style = Style::new().fg(theme.header).bold();

	let field = |field_index: usize, label: &'static str, value: String| -> ListItem<'static> {
		let current_label_style = if selected == field_index { editing_style } else { label_style };

		let value_line = if value.is_empty() {
			Line::from(Span::styled("(not set)", placeholder))
		} else {
			Line::from(Span::styled(value, normal))
		};

		ListItem::new(vec![Line::from(Span::styled(label, current_label_style)), value_line, Line::from("")])
	};

	let database_value = app.config.default_database.as_ref().map(|p| p.display().to_string()).unwrap_or_default();

	let keyfile_value = app.config.keyfile.as_ref().map(|p| p.display().to_string()).unwrap_or_default();

	let auto_lock_value = app.config.auto_lock.as_secs().to_string();

	let theme_value = if app.available_themes.is_empty() {
		Line::from(Span::styled("no themes found in ~/.config/kptui/themes", warning))
	} else {
		match app.config.theme.as_deref() {
			Some(name) => Line::from(Span::styled(crate::theme::slugify(name), normal)),
			None => Line::from(Span::styled("(not set)", placeholder)),
		}
	};

	let items = vec![
		field(DATABASE_ROW, "Default database", database_value),
		field(KEYFILE_ROW, "Keyfile (optional)", keyfile_value),
		field(AUTO_LOCK_ROW, "Auto-lock (seconds)", auto_lock_value),
		ListItem::new(vec![
			Line::from(Span::styled("Theme", if selected == THEME_ROW { editing_style } else { label_style })),
			theme_value,
			Line::from(""),
		]),
		ListItem::new(vec![
			Line::from(Span::styled("Change master password", if selected == 4 { editing_style } else { label_style })),
			Line::from(Span::styled("[Enter] Change", placeholder)),
			Line::from(""),
		]),
		ListItem::new(vec![
			Line::from(Span::styled("Import database", if selected == 5 { editing_style } else { label_style })),
			Line::from(Span::styled("[Enter] Import", placeholder)),
			Line::from(""),
		]),
		ListItem::new(vec![
			Line::from(Span::styled("Export database", if selected == 6 { editing_style } else { label_style })),
			Line::from(Span::styled("[Enter] Export", placeholder)),
			Line::from(""),
		]),
	];

	let list = List::new(items).block(Block::default().borders(Borders::ALL).padding(Padding::horizontal(1)).border_style(border_style).title(" Settings "));

	frame.render_stateful_widget(list, vertical[0], &mut app.settings_state);

	// Overlay the FieldEditor on the selected editable setting.
	if editing_field
		&& selected < 3
		&& let Some(editor) = app.field_editor.as_ref()
	{
		let list_area = vertical[0];

		// The List has a one-cell border and one-cell horizontal padding.
		// Each setting occupies three rows:
		//   label
		//   value/editor
		//   blank
		let value_y = list_area.y + 2 + selected as u16 * 3;

		let editor_x = list_area.x + 2;
		let editor_width = list_area.width.saturating_sub(3);

		if editor_width > 0 {
			let (visible, cursor_column) = editor.single_line_view(editor_width as usize);

			let field_area = Rect { x: editor_x, y: value_y, width: editor_width, height: 1 };

			frame.render_widget(Paragraph::new(visible).style(normal), field_area);

			let cursor_x = field_area.x + cursor_column as u16;
			frame.set_cursor_position((cursor_x, field_area.y));
		}
	}

	let help = if let Some(status) = &app.status {
		Paragraph::new(format!("  {status}")).style(warning)
	} else {
		let help_text = help_lines.iter().map(|line| format!("  {line}")).collect::<Vec<_>>().join("\n");

		Paragraph::new(help_text).style(accent)
	};

	frame.render_widget(help, vertical[1]);
}

fn draw_change_password(frame: &mut Frame, app: &mut App) {
	let full_area = frame.area();

	let help_width = full_area.width.saturating_sub(2);
	let help_lines = wrap_help_items(change_password_help_items(app.slim_mode), help_width);
	let help_height = help_lines.len() as u16;

	let vertical = Layout::default()
		.direction(Direction::Vertical)
		.constraints([Constraint::Fill(1), Constraint::Length(3), Constraint::Fill(1), Constraint::Length(help_height)])
		.split(full_area);

	let input_row = vertical[1];

	let horizontal = Layout::default()
		.direction(Direction::Horizontal)
		.constraints([Constraint::Fill(1), Constraint::Length(50), Constraint::Fill(1)])
		.split(input_row);

	let input_area = horizontal[1];
	app.max_len = (input_area.width - 2) as usize;

	let title = match app.password_change_step {
		crate::app::PasswordChangeStep::Current => " Current master password ",
		crate::app::PasswordChangeStep::New => " New master password ",
		crate::app::PasswordChangeStep::ConfirmNew => " Confirm new master password ",
	};

	let buf = match app.password_change_step {
		crate::app::PasswordChangeStep::Current => &app.current_password_buffer,
		crate::app::PasswordChangeStep::New => &app.new_password_buffer,
		crate::app::PasswordChangeStep::ConfirmNew => &app.new_password_confirm,
	};

	let input = Paragraph::new("".repeat(buf.chars().count()))
		.alignment(Alignment::Center)
		.block(Block::default().borders(Borders::ALL).border_style(Style::default().fg(app.theme.border)).title(title));

	frame.render_widget(input, input_area);

	let hint_area = Rect { x: input_area.x, y: input_area.y + input_area.height, width: input_area.width, height: 1 };

	let default_hint = match app.password_change_step {
		crate::app::PasswordChangeStep::Current => "verify current master password",
		crate::app::PasswordChangeStep::New | crate::app::PasswordChangeStep::ConfirmNew => "this re-encrypts the vault with the new password",
	};
	let hint_text = app.status.clone().unwrap_or_else(|| default_hint.to_string());
	let hint_style = if app.status.is_some() { Style::new().fg(app.theme.error) } else { Style::new().fg(app.theme.warning) };

	let hint = Paragraph::new(hint_text).alignment(Alignment::Center).style(hint_style);

	frame.render_widget(hint, hint_area);

	let typed_len = buf.chars().count() as u16;
	let cursor_x = centered_cursor_x(input_area, typed_len);
	frame.set_cursor_position((cursor_x, input_area.y + 1));

	let accent = Style::new().fg(app.theme.accent);
	let help_text = help_lines.iter().map(|line| format!("  {line}")).collect::<Vec<_>>().join("\n");
	frame.render_widget(Paragraph::new(help_text).style(accent), vertical[3]);
}

fn draw_import(frame: &mut Frame, app: &mut App) {
	let full_area = frame.area();

	let help_width = full_area.width.saturating_sub(2);
	let help_lines = wrap_help_items(import_help_items(app.slim_mode), help_width);
	let help_height = help_lines.len() as u16;

	let vertical = Layout::default()
		.direction(Direction::Vertical)
		.constraints([Constraint::Fill(1), Constraint::Length(3), Constraint::Fill(1), Constraint::Length(help_height)])
		.split(full_area);

	let input_row = vertical[1];

	let horizontal = Layout::default()
		.direction(Direction::Horizontal)
		.constraints([Constraint::Fill(1), Constraint::Length(60), Constraint::Fill(1)])
		.split(input_row);

	let input_area = horizontal[1];
	app.max_len = (input_area.width - 2) as usize;

	let masked = matches!(app.import_step, crate::app::ImportStep::KdbxPassword);

	let title = match app.import_step {
		crate::app::ImportStep::Path => " Import from file (.kdbx/.csv/.json) ",
		crate::app::ImportStep::KdbxPassword => " Password for that vault ",
	};

	let buf = match app.import_step {
		crate::app::ImportStep::Path => &app.import_path_buffer,
		crate::app::ImportStep::KdbxPassword => &app.import_kdbx_password_buffer,
	};

	let display_text = if masked { "".repeat(buf.chars().count()) } else { buf.clone() };

	let input = Paragraph::new(display_text)
		.alignment(Alignment::Center)
		.block(Block::default().borders(Borders::ALL).border_style(Style::default().fg(app.theme.border)).title(title));

	frame.render_widget(input, input_area);

	let hint_area = Rect { x: input_area.x, y: input_area.y + input_area.height, width: input_area.width, height: 1 };

	let default_hint = match app.import_step {
		crate::app::ImportStep::Path => "only Name, User, Password, URL, TOTP and Notes are imported",
		crate::app::ImportStep::KdbxPassword => "that file's master password (not for this vault!)",
	};
	let hint_text = app.status.clone().unwrap_or_else(|| default_hint.to_string());
	let hint_style = if app.status.is_some() { Style::new().fg(app.theme.error) } else { Style::new().fg(app.theme.warning) };

	let hint = Paragraph::new(hint_text).alignment(Alignment::Center).style(hint_style);

	frame.render_widget(hint, hint_area);

	let typed_len = buf.chars().count() as u16;
	let cursor_x = centered_cursor_x(input_area, typed_len);
	frame.set_cursor_position((cursor_x, input_area.y + 1));

	let accent = Style::new().fg(app.theme.accent);
	let help_text = help_lines.iter().map(|line| format!("  {line}")).collect::<Vec<_>>().join("\n");
	frame.render_widget(Paragraph::new(help_text).style(accent), vertical[3]);
}

fn draw_export(frame: &mut Frame, app: &mut App) {
	let full_area = frame.area();

	let help_width = full_area.width.saturating_sub(2);
	let help_lines = wrap_help_items(export_help_items(app.slim_mode), help_width);
	let help_height = help_lines.len() as u16;

	let vertical = Layout::default()
		.direction(Direction::Vertical)
		.constraints([Constraint::Fill(1), Constraint::Length(3), Constraint::Fill(1), Constraint::Length(help_height)])
		.split(full_area);

	let input_row = vertical[1];

	let horizontal = Layout::default()
		.direction(Direction::Horizontal)
		.constraints([Constraint::Fill(1), Constraint::Length(60), Constraint::Fill(1)])
		.split(input_row);

	let input_area = horizontal[1];
	app.max_len = (input_area.width - 2) as usize;

	let title = match app.export_step {
		crate::app::ExportStep::Path => " Export to file (.kdbx/.csv/.json) ",
		crate::app::ExportStep::Confirm => " Confirm export ",
	};

	let input = Paragraph::new(app.export_path_buffer.clone())
		.alignment(Alignment::Center)
		.block(Block::default().borders(Borders::ALL).border_style(Style::default().fg(app.theme.border)).title(title));

	frame.render_widget(input, input_area);

	let hint_area = Rect { x: input_area.x, y: input_area.y + input_area.height, width: input_area.width, height: 1 };

	let default_hint = match app.export_step {
		crate::app::ExportStep::Path => "only Name, User, Password, URL, and TOTP are exported".to_string(),
		crate::app::ExportStep::Confirm => {
			let path = app.pending_export_path.as_deref();
			let plaintext = path
				.and_then(|p| crate::export::detect_format(p).ok())
				.map(|f| matches!(f, crate::export::ExportFormat::Csv | crate::export::ExportFormat::Json))
				.unwrap_or(false);
			let overwrite = path.map(|p| p.exists()).unwrap_or(false);

			match (plaintext, overwrite) {
				(true, true) => "overwriting an existing file with all passwords in PLAINTEXT — [Enter] confirm".to_string(),
				(true, false) => "writing all passwords in PLAINTEXT to disk — [Enter] Confirm".to_string(),
				(false, true) => "overwriting an existing file — [Enter] Confirm".to_string(),
				(false, false) => "[Enter] Confirm".to_string(),
			}
		}
	};
	let hint_text = app.status.clone().unwrap_or(default_hint);
	let hint_style = if app.status.is_some() { Style::new().fg(app.theme.error) } else { Style::new().fg(app.theme.warning) };

	let hint = Paragraph::new(hint_text).alignment(Alignment::Center).style(hint_style);

	frame.render_widget(hint, hint_area);

	let typed_len = app.export_path_buffer.chars().count() as u16;
	let cursor_x = centered_cursor_x(input_area, typed_len);
	frame.set_cursor_position((cursor_x, input_area.y + 1));

	let accent = Style::new().fg(app.theme.accent);
	let help_text = help_lines.iter().map(|line| format!("  {line}")).collect::<Vec<_>>().join("\n");
	frame.render_widget(Paragraph::new(help_text).style(accent), vertical[3]);
}

fn draw_theme_picker(frame: &mut Frame, app: &mut App) {
	let full_area = frame.area();

	let help_width = full_area.width.saturating_sub(2);
	let help_lines = wrap_help_items(export_help_items(app.slim_mode), help_width);
	let help_height = help_lines.len() as u16;

	let vertical = Layout::default()
		.direction(Direction::Vertical)
		.constraints([Constraint::Fill(1), Constraint::Length(help_height)])
		.split(full_area);

	let theme = &app.theme;
	let normal = Style::new().fg(theme.text);
	let accent = Style::new().fg(theme.accent);
	let warning = Style::new().fg(theme.warning);
	let border_style = Style::new().fg(theme.border);
	let current_theme = app.config.theme.as_deref().map(crate::theme::slugify);

	let items: Vec<ListItem> = app
		.available_themes
		.iter()
		.map(|name| {
			let is_active = current_theme.as_deref().is_some_and(|current| current == crate::theme::slugify(name));

			let marker = if is_active { "" } else { "" };
			let marker_style = if is_active { accent } else { normal };

			ListItem::new(Line::from(vec![Span::styled(format!("{marker} "), marker_style), Span::styled(name.clone(), normal)]))
		})
		.collect();

	let list = List::new(items)
		.block(Block::default().borders(Borders::ALL).padding(Padding::horizontal(1)).border_style(border_style).title(" Theme "))
		.highlight_style(Style::new().fg(theme.selection_fg).bg(theme.selection_bg).bold());

	frame.render_stateful_widget(list, vertical[0], &mut app.theme_state);

	let help = if let Some(status) = &app.status {
		Paragraph::new(format!("  {status}")).style(warning)
	} else {
		let help_text = help_lines.iter().map(|line| format!("  {line}")).collect::<Vec<_>>().join("\n");
		Paragraph::new(help_text).style(accent)
	};

	frame.render_widget(help, vertical[1]);
}