Skip to content

Commit

Permalink
feat: implement :read typed command
Browse files Browse the repository at this point in the history
  • Loading branch information
idursun committed Oct 4, 2022
1 parent ed5febf commit 05a888e
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,42 @@ fn run_shell_command(
Ok(())
}

fn read_file_info_buffer(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
) -> anyhow::Result<()> {
if event != PromptEvent::Validate {
return Ok(());
}

let (view, doc) = current!(cx.editor);

ensure!(!args.is_empty(), "file name is expected");

let filename = args.get(0).unwrap();
let path = PathBuf::from(filename.to_string());
if path.exists() {
match std::fs::read_to_string(path) {
Ok(contents) => {
let contents = Tendril::from(contents);
let selection = doc.selection(view.id);
let transaction = Transaction::insert(doc.text(), selection, contents);
doc.apply(&transaction, view.id);
}
Err(error) => {
cx.editor
.set_error(format!("error reading file: {}", error));
}
}
} else {
cx.editor
.set_error(format!("file doesn't exist: {}", filename));
}

Ok(())
}

pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
TypableCommand {
name: "quit",
Expand Down Expand Up @@ -2093,6 +2129,13 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
fun: run_shell_command,
completer: Some(completers::directory),
},
TypableCommand {
name: "read",
aliases: &["r"],
doc: "Load a file into buffer",
fun: read_file_info_buffer,
completer: Some(completers::filename),
},
];

pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableCommand>> =
Expand Down

0 comments on commit 05a888e

Please sign in to comment.