Skip to content

Commit

Permalink
Add a "touch" command (kamiyaa#66)
Browse files Browse the repository at this point in the history
* touch existing file works

* touch can create files

* bind key sequence "ft" to command ":touch "

* fix: touch can touch new file in empty directory
  • Loading branch information
DLFW authored Jun 9, 2021
1 parent 254ad21 commit 00c1c5a
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ alphanumeric-sort = "^1"
chrono = "^0"
colors-transform = "^0"
dirs-next = "^2"
filetime = "^0"
globset = "^0"
lazy_static = "^1"
libc = "^0"
Expand Down
3 changes: 3 additions & 0 deletions config/keymap.toml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ keys = [ "m", "k" ]
[[mapcommand]]
command = ":rename "
keys = [ "c", "w" ]
[[mapcommand]]
command = ":touch"
keys = [ "f", "t" ]

[[mapcommand]]
command = "sort lexical"
Expand Down
4 changes: 4 additions & 0 deletions src/commands/key_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub enum KeyCommand {
RenameFile(path::PathBuf),
RenameFileAppend,
RenameFilePrepend,
TouchFile(String),

SearchGlob(String),
SearchString(String),
Expand Down Expand Up @@ -109,6 +110,7 @@ impl KeyCommand {
Self::ForceQuit => "force_quit",
Self::ReloadDirList => "reload_dirlist",
Self::RenameFile(_) => "rename",
Self::TouchFile(_) => "touch",
Self::RenameFileAppend => "rename_append",
Self::RenameFilePrepend => "rename_prepend",

Expand Down Expand Up @@ -263,6 +265,7 @@ impl KeyCommand {
Ok(Self::RenameFile(path))
}
},
"touch" => Ok(Self::TouchFile(arg.to_string())),
"rename_append" => Ok(Self::RenameFileAppend),
"rename_prepend" => Ok(Self::RenameFilePrepend),
"search" => match arg {
Expand Down Expand Up @@ -392,6 +395,7 @@ impl AppExecute for KeyCommand {
Self::RenameFile(p) => rename_file::rename_file(context, p.as_path()),
Self::RenameFileAppend => rename_file::rename_file_append(context, backend),
Self::RenameFilePrepend => rename_file::rename_file_prepend(context, backend),
Self::TouchFile(arg) => touch_file::touch_file(context, arg.as_str()),
Self::SearchGlob(pattern) => search_glob::search_glob(context, pattern.as_str()),
Self::SearchString(pattern) => search_string::search_string(context, pattern.as_str()),
Self::SearchSkim => search_skim::search_skim(context, backend),
Expand Down
1 change: 1 addition & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod show_hidden;
pub mod show_workers;
pub mod sort;
pub mod tab_ops;
pub mod touch_file;

pub mod command_keybind;
pub mod key_command;
Expand Down
49 changes: 49 additions & 0 deletions src/commands/touch_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use std::path;

use crate::context::AppContext;
use crate::error::JoshutoResult;
use crate::util::load_child::LoadChild;
use filetime::FileTime;
use std::fs::File;
use std::time::SystemTime;

fn _update_actime(file: &path::Path) -> std::io::Result<()> {
let file_time = FileTime::from_system_time(SystemTime::now());
filetime::set_file_times(file, file_time, file_time)
}

fn _create_file(file: &path::Path) -> std::io::Result<()> {
File::create(file)?;
Ok(())
}

pub fn touch_file(context: &mut AppContext, arg: &str) -> JoshutoResult<()> {
match arg {
"" => {
match context
.tab_context_ref()
.curr_tab_ref()
.curr_list_ref()
.and_then(|s| s.curr_entry_ref())
.map(|s| s.file_path().to_path_buf())
{
Some(selected_file_path) => _update_actime(&selected_file_path)?,
None => {}
}
}
file_arg => {
let file = path::PathBuf::from(file_arg);
if file.exists() {
_update_actime(file.as_path())?;
} else {
_create_file(file.as_path())?;
}
}
}
let options = context.config_ref().display_options_ref().clone();
if let Some(curr_list) = context.tab_context_mut().curr_tab_mut().curr_list_mut() {
curr_list.reload_contents(&options)?;
}
LoadChild::load_child(context)?;
Ok(())
}

0 comments on commit 00c1c5a

Please sign in to comment.