Skip to content

Commit

Permalink
Print filename (or 'STDIN' if reading from STDIN) on right side of st…
Browse files Browse the repository at this point in the history
…atus bar.
  • Loading branch information
PaulJuliusMartinez committed Aug 26, 2021
1 parent b9e07c6 commit 3ade459
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
19 changes: 15 additions & 4 deletions src/jless.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ pub struct JLess {
viewer: JsonViewer,
screen_writer: ScreenWriter,
input_buffer: Vec<u8>,
input_filename: String,
}

pub const MAX_BUFFER_SIZE: usize = 9;
const BELL: &'static str = "\x07";

pub fn new(opt: &Opt, json: String, stdout: Box<dyn Write>) -> Result<JLess, String> {
pub fn new(
opt: &Opt,
json: String,
input_filename: String,
stdout: Box<dyn Write>,
) -> Result<JLess, String> {
let flatjson = match flatjson::parse_top_level_json(json) {
Ok(flatjson) => flatjson,
Err(err) => return Err(format!("Unable to parse input: {:?}", err)),
Expand All @@ -46,6 +52,7 @@ pub fn new(opt: &Opt, json: String, stdout: Box<dyn Write>) -> Result<JLess, Str
viewer,
screen_writer,
input_buffer: vec![],
input_filename,
})
}

Expand All @@ -54,7 +61,8 @@ impl JLess {
let dimensions = TTYDimensions::from_size(termion::terminal_size().unwrap());
self.viewer.dimensions = dimensions.without_status_bar();
self.screen_writer.dimensions = dimensions;
self.screen_writer.print(&self.viewer, &self.input_buffer);
self.screen_writer
.print(&self.viewer, &self.input_buffer, &self.input_filename);

for event in input {
let event = event.unwrap();
Expand Down Expand Up @@ -182,8 +190,11 @@ impl JLess {
self.viewer.perform_action(action);
self.screen_writer.print_viewer(&self.viewer);
}
self.screen_writer
.print_status_bar(&self.viewer, &self.input_buffer);
self.screen_writer.print_status_bar(
&self.viewer,
&self.input_buffer,
&self.input_filename,
);
}
}

Expand Down
12 changes: 8 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct Opt {
fn main() {
let opt = Opt::from_args();

let json_string = match get_json_input(&opt) {
let (json_string, input_filename) = match get_json_input(&opt) {
Ok(json_string) => json_string,
Err(err) => {
println!("Unable to get input: {}", err);
Expand All @@ -56,7 +56,7 @@ fn main() {
let stdout = MouseTerminal::from(HideCursor::from(AlternateScreen::from(
io::stdout().into_raw_mode().unwrap(),
)));
let mut app = match jless::new(&opt, json_string, Box::new(stdout)) {
let mut app = match jless::new(&opt, json_string, input_filename, Box::new(stdout)) {
Ok(jl) => jl,
Err(err) => {
eprintln!("{}", err);
Expand All @@ -67,25 +67,29 @@ fn main() {
app.run(Box::new(input::get_input()));
}

fn get_json_input(opt: &Opt) -> io::Result<String> {
fn get_json_input(opt: &Opt) -> io::Result<(String, String)> {
let mut json_string = String::new();
let filename;

match &opt.input {
None => {
if isatty::stdin_isatty() {
println!("Missing filename (\"jless --help\" for help)");
std::process::exit(1);
}
filename = "STDIN".to_string();
io::stdin().read_to_string(&mut json_string)?;
}
Some(path) => {
if *path == PathBuf::from("-") {
filename = "STDIN".to_string();
io::stdin().read_to_string(&mut json_string)?;
} else {
File::open(path)?.read_to_string(&mut json_string)?;
filename = String::from(path.file_name().unwrap().to_string_lossy());
}
}
}

Ok(json_string)
Ok((json_string, filename))
}
37 changes: 23 additions & 14 deletions src/screenwriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ lazy_static! {
}

impl ScreenWriter {
pub fn print(&mut self, viewer: &JsonViewer, input_buffer: &[u8]) {
pub fn print(&mut self, viewer: &JsonViewer, input_buffer: &[u8], input_filename: &str) {
self.print_viewer(viewer);
self.print_status_bar(viewer, input_buffer);
self.print_status_bar(viewer, input_buffer, input_filename);
}

pub fn print_viewer(&mut self, viewer: &JsonViewer) {
Expand All @@ -63,8 +63,13 @@ impl ScreenWriter {
}
}

pub fn print_status_bar(&mut self, viewer: &JsonViewer, input_buffer: &[u8]) {
match self.print_status_bar_impl(viewer, input_buffer) {
pub fn print_status_bar(
&mut self,
viewer: &JsonViewer,
input_buffer: &[u8],
input_filename: &str,
) {
match self.print_status_bar_impl(viewer, input_buffer, input_filename) {
Ok(_) => {}
Err(e) => {
eprintln!("Error while printing status bar: {}", e);
Expand Down Expand Up @@ -311,21 +316,32 @@ impl ScreenWriter {
&mut self,
viewer: &JsonViewer,
input_buffer: &[u8],
input_filename: &str,
) -> std::io::Result<()> {
self.tty_writer
.position_cursor(1, self.dimensions.height - 1)?;
self.invert_colors(Black)?;
self.tty_writer.clear_line()?;
self.tty_writer
.position_cursor(1, self.dimensions.height - 1)?;
write!(
self.tty_writer,
"{}input{}",
color::Fg(color::LightBlack),
color::Fg(color::Black)
)
.unwrap();
write!(
self.tty_writer,
"{}",
ScreenWriter::get_path_to_focused_node(viewer)
)?;
self.tty_writer
.position_cursor(self.dimensions.width - 8, self.dimensions.height - 1)?;
write!(self.tty_writer, "FILE NAME")?;
self.tty_writer.position_cursor(
// 1 indexed
1 + self.dimensions.width - input_filename.len() as u16,
self.dimensions.height - 1,
)?;
write!(self.tty_writer, "{}", input_filename)?;

self.reset_style()?;
self.tty_writer.position_cursor(1, self.dimensions.height)?;
Expand All @@ -349,13 +365,6 @@ impl ScreenWriter {

fn get_path_to_focused_node(viewer: &JsonViewer) -> String {
let mut buf = String::new();
write!(
buf,
"{}input{}",
color::Fg(color::LightBlack),
color::Fg(color::Black)
)
.unwrap();
ScreenWriter::build_path_to_focused_node(viewer, &mut buf, viewer.focused_row);
buf
}
Expand Down

0 comments on commit 3ade459

Please sign in to comment.