commands: prevent usize underflow when navigating empty entry list

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I0432dcc88b22226772f6bb6e05cc64d36a6a6964
This commit is contained in:
raf 2026-01-22 13:28:43 +03:00
commit 3165543580
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -251,6 +251,9 @@ impl SqliteClipboardDb {
match (key.code, key.modifiers) {
(KeyCode::Char('q') | KeyCode::Esc, _) => break,
(KeyCode::Down | KeyCode::Char('j'), _) => {
if entries.is_empty() {
state.select(None);
} else {
let i = match state.selected() {
Some(i) => {
if i >= entries.len() - 1 {
@ -262,8 +265,12 @@ impl SqliteClipboardDb {
None => 0,
};
state.select(Some(i));
}
},
(KeyCode::Up | KeyCode::Char('k'), _) => {
if entries.is_empty() {
state.select(None);
} else {
let i = match state.selected() {
Some(i) => {
if i == 0 {
@ -275,10 +282,12 @@ impl SqliteClipboardDb {
None => 0,
};
state.select(Some(i));
}
},
(KeyCode::Enter, _) => {
if let Some(idx) = state.selected()
&& let Some((id, ..)) = entries.get(idx) {
&& let Some((id, ..)) = entries.get(idx)
{
match self.copy_entry(*id) {
Ok((new_id, contents, mime)) => {
if new_id != *id {
@ -291,15 +300,11 @@ impl SqliteClipboardDb {
let opts = Options::new();
let mime_type = match mime {
Some(ref m) if m == "text/plain" => MimeType::Text,
Some(ref m) => {
MimeType::Specific(m.clone().to_owned())
},
Some(ref m) => MimeType::Specific(m.clone().to_owned()),
None => MimeType::Text,
};
let copy_result = opts.copy(
Source::Bytes(contents.clone().into()),
mime_type,
);
let copy_result = opts
.copy(Source::Bytes(contents.clone().into()), mime_type);
match copy_result {
Ok(()) => {
let _ = Notification::new()
@ -308,14 +313,10 @@ impl SqliteClipboardDb {
.show();
},
Err(e) => {
log::error!(
"Failed to copy entry to clipboard: {e}"
);
log::error!("Failed to copy entry to clipboard: {e}");
let _ = Notification::new()
.summary("Stash")
.body(&format!(
"Failed to copy to clipboard: {e}"
))
.body(&format!("Failed to copy to clipboard: {e}"))
.show();
},
}
@ -332,7 +333,8 @@ impl SqliteClipboardDb {
},
(KeyCode::Char('D'), KeyModifiers::SHIFT) => {
if let Some(idx) = state.selected()
&& let Some((id, ..)) = entries.get(idx) {
&& let Some((id, ..)) = entries.get(idx)
{
// Delete entry from DB
self
.conn