mirror of
https://github.com/NotAShelf/stash.git
synced 2026-04-12 22:17:41 +00:00
commands: prevent usize underflow when navigating empty entry list
Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I0432dcc88b22226772f6bb6e05cc64d36a6a6964
This commit is contained in:
parent
20b6a12461
commit
3165543580
1 changed files with 85 additions and 83 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue