Skip to content

Commit

Permalink
backspace or delete when textfield is empty should exit
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Jul 6, 2024
1 parent 3fe684a commit f314c73
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 25 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ xdg = "^2"

[dependencies.nix]
version = "^0"
default_features = false
default-features = false
features = ["user"]

[dependencies.ratatui]
Expand Down
4 changes: 2 additions & 2 deletions src/commands/bulk_rename.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::env;
use std::fs;
use std::io::{self, BufRead, Write};
use std::io::{BufRead, Write};
use std::path;
use std::process;

Expand Down Expand Up @@ -89,7 +89,7 @@ pub fn _bulk_rename(context: &mut AppContext) -> AppResult {
}
if paths_renamed.len() < entries.len() {
return Err(AppError::new(
AppErrorKind::Io(io::ErrorKind::InvalidInput),
AppErrorKind::Io,
"Insufficient inputs".to_string(),
));
}
Expand Down
3 changes: 1 addition & 2 deletions src/commands/file_ops.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::io;
use std::process::{Command, Stdio};

use crate::context::{AppContext, LocalStateContext};
Expand Down Expand Up @@ -46,7 +45,7 @@ pub fn paste(context: &mut AppContext, options: FileOperationOptions) -> AppResu
Ok(())
}
_ => Err(AppError::new(
AppErrorKind::Io(io::ErrorKind::InvalidData),
AppErrorKind::Io,
"no files selected".to_string(),
)),
}
Expand Down
5 changes: 2 additions & 3 deletions src/commands/open_file.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::io;
use std::path;

use crate::commands::{quit, reload};
Expand Down Expand Up @@ -209,7 +208,7 @@ pub fn open_with_index(

if paths.is_empty() {
return Err(AppError::new(
AppErrorKind::Io(io::ErrorKind::NotFound),
AppErrorKind::Io,
String::from("No files selected"),
));
}
Expand All @@ -218,7 +217,7 @@ pub fn open_with_index(

if index >= options.len() {
return Err(AppError::new(
AppErrorKind::Io(std::io::ErrorKind::InvalidData),
AppErrorKind::Io,
"option does not exist".to_string(),
));
}
Expand Down
4 changes: 1 addition & 3 deletions src/commands/quit.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};

Expand Down Expand Up @@ -33,7 +31,7 @@ pub fn quit_with_action(context: &mut AppContext, quit_action: QuitAction) -> Ap
let worker_context = context.worker_context_ref();
if worker_context.is_busy() || !worker_context.is_empty() {
Err(AppError::new(
AppErrorKind::Io(io::ErrorKind::Other),
AppErrorKind::Io,
String::from("operations running in background, use `quit --force` to quit"),
))
} else {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/search_fzf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use crate::commands::{cursor_move, fzf};
use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
Expand All @@ -22,7 +20,7 @@ pub fn search_fzf(context: &mut AppContext, backend: &mut AppBackend) -> AppResu

if items.is_empty() {
return Err(AppError::new(
AppErrorKind::Io(io::ErrorKind::InvalidData),
AppErrorKind::Io,
"no files to select".to_string(),
));
}
Expand Down
4 changes: 1 addition & 3 deletions src/commands/select_fzf.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io;

use crate::context::AppContext;
use crate::error::{AppError, AppErrorKind, AppResult};
use crate::ui::AppBackend;
Expand Down Expand Up @@ -28,7 +26,7 @@ pub fn select_fzf(

if items.is_empty() {
return Err(AppError::new(
AppErrorKind::Io(io::ErrorKind::InvalidData),
AppErrorKind::Io,
"no files to select".to_string(),
));
}
Expand Down
14 changes: 12 additions & 2 deletions src/ui/views/tui_textfield.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,24 @@ impl<'a> TuiTextField<'a> {
AppEvent::Termion(Event::Key(key)) => {
let dirty = match key {
Key::Backspace => {
let res = line_buffer.backspace(1, listener);
if line_buffer.is_empty() {
let _ = terminal.hide_cursor();
return None;
}

let res = line_buffer.backspace(1, listener);
if let Ok(command) = Command::from_str(line_buffer.as_str()) {
command.interactive_execute(context)
}
res
}
Key::Delete => line_buffer.delete(1, listener).is_some(),
Key::Delete => {
if line_buffer.is_empty() {
let _ = terminal.hide_cursor();
return None;
}
line_buffer.delete(1, listener).is_some()
}
Key::Home => line_buffer.move_home(),
Key::End => line_buffer.move_end(),
Key::Up => {
Expand Down
9 changes: 3 additions & 6 deletions src/util/mimetype.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::Path;
use std::{io, process::Command};
use std::process::Command;

use crate::error::{AppError, AppErrorKind, AppResult};

Expand Down Expand Up @@ -36,18 +36,15 @@ pub fn get_mimetype(p: &Path) -> AppResult<Mimetype> {
if !output.status.success() {
let stderr_msg = String::from_utf8_lossy(&output.stderr).to_string();

let error = AppError::new(AppErrorKind::Io(io::ErrorKind::InvalidInput), stderr_msg);
let error = AppError::new(AppErrorKind::Io, stderr_msg);
return Err(error);
}

let stdout_msg = String::from_utf8_lossy(&output.stdout).to_string();
match stdout_msg.trim().split_once('/') {
Some((ttype, subtype)) => Ok(Mimetype::new(ttype.to_string(), subtype.to_string())),
None => {
let error = AppError::new(
AppErrorKind::Io(io::ErrorKind::InvalidInput),
"Unknown mimetype".to_string(),
);
let error = AppError::new(AppErrorKind::Io, "Unknown mimetype".to_string());
Err(error)
}
}
Expand Down

0 comments on commit f314c73

Please sign in to comment.