Skip to content

Commit

Permalink
Add :write <file> (and :write!) commands for writing input to a file.
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulJuliusMartinez committed Jun 1, 2024
1 parent 3d1c43f commit bd58080
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ main

Improvements:
- [Issue #143]: `ctrl-z` will now send jless to the background
- `:w[rite] <file>` and `:w[rite]! <file>` can be used to write the
current input to a file

v0.9.0 (2023-07-16)
==================
Expand Down
65 changes: 56 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::error::Error;
use std::fs::File;
use std::io;
use std::io::Write;

Expand Down Expand Up @@ -68,6 +69,10 @@ enum Command {
Help,
SetShowLineNumber(Option<bool>),
SetShowRelativeLineNumber(Option<bool>),
WriteFile {
filename: String,
overwrite_existing: bool,
},
Unknown,
}

Expand Down Expand Up @@ -483,6 +488,12 @@ impl App {
self.screen_writer.show_relative_line_numbers =
!self.screen_writer.show_relative_line_numbers
}
Command::WriteFile {
filename,
overwrite_existing,
} => {
self.write_contents_to_file(filename, overwrite_existing);
}
Command::Unknown => {
self.set_warning_message(format!(
"Unknown command: {command}"
Expand Down Expand Up @@ -725,15 +736,28 @@ impl App {
}

fn parse_command(command: &str) -> Command {
match command {
"h" | "he" | "hel" | "help" => Command::Help,
"q" | "qu" | "qui" | "quit" | "quit()" | "exit" | "exit()" => Command::Quit,
"set number" => Command::SetShowLineNumber(Some(true)),
"set number!" => Command::SetShowLineNumber(None),
"set nonumber" => Command::SetShowLineNumber(Some(false)),
"set relativenumber" => Command::SetShowRelativeLineNumber(Some(true)),
"set relativenumber!" => Command::SetShowRelativeLineNumber(None),
"set norelativenumber" => Command::SetShowRelativeLineNumber(Some(false)),
let args: Vec<&str> = command.split(" ").filter(|s| !s.is_empty()).collect();

match args.as_slice() {
["h" | "help"] => Command::Help,
["q" | "quit" | "quit()" | "exit" | "exit()"] => Command::Quit,
["set", arg] => match *arg {
"number" => Command::SetShowLineNumber(Some(true)),
"number!" => Command::SetShowLineNumber(None),
"nonumber" => Command::SetShowLineNumber(Some(false)),
"relativenumber" => Command::SetShowRelativeLineNumber(Some(true)),
"relativenumber!" => Command::SetShowRelativeLineNumber(None),
"norelativenumber" => Command::SetShowRelativeLineNumber(Some(false)),
_ => Command::Unknown,
},
["w" | "write", filename] => Command::WriteFile {
filename: filename.to_string(),
overwrite_existing: false,
},
["w!" | "write!", filename] => Command::WriteFile {
filename: filename.to_string(),
overwrite_existing: true,
},
_ => Command::Unknown,
}
}
Expand Down Expand Up @@ -893,4 +917,27 @@ impl App {
}
}
}

fn write_contents_to_file(&mut self, filename: String, overwrite_existing: bool) {
let mut file_open_options = File::options();
file_open_options
.read(true)
.write(true)
.create_new(!overwrite_existing);

match file_open_options.open(&filename) {
Err(err) => match err.kind() {
io::ErrorKind::AlreadyExists => self
.set_error_message(format!("{filename} already exists (add ! to overwrite)")),
_ => self.set_error_message(format!("Error opening file for writing: {err}")),
},
Ok(mut file) => match self.viewer.flat_json.pretty_printed() {
Err(err) => self.set_error_message(format!("Error pretty printing input: {err}")),
Ok(pretty_printed) => match file.write_all(pretty_printed.as_bytes()) {
Ok(()) => self.set_info_message(format!("{filename} written")),
Err(err) => self.set_error_message(format!("Error writing file: {err}")),
},
},
}
}
}
10 changes: 8 additions & 2 deletions src/jless.help
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
Commands requiring multiple key-presses may be cancelled with the
Escape key.

q ^c Exit jless.
q :q[uit] ^c Exit jless.

F1 :help Show this help screen.
F1 :h[elp] Show this help screen.

^z Suspend jless.

Expand Down Expand Up @@ -120,6 +120,12 @@
yq pq Copy/print a path that can be used by jq to filter the input JSON and
return the currently focused value.

WRITING

:w[rite] <name> Write the input JSON to a file.
:w[rite]! <name> Write the input JSON to a file, even if the file already
exists.

SEARCH

jless supports full-text search over the input JSON.
Expand Down

0 comments on commit bd58080

Please sign in to comment.