Skip to content

Commit

Permalink
Handle none case with warning
Browse files Browse the repository at this point in the history
  • Loading branch information
nnh12 committed Nov 25, 2024
1 parent 286ace5 commit 26bf332
Showing 1 changed file with 77 additions and 80 deletions.
157 changes: 77 additions & 80 deletions applications/less/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,10 @@ fn get_content_string(file_path: String) -> Result<String, String> {
}
},
None => {
// Handle the case where the path wasn't found
// // For example, you could return an error or print a message:
Err(format!("Couldn't find file at path".to_string()))
println!("Warning: path is not found");
// Optionally, return a default or handle it another way.
Ok("".to_string()) // Example: return empty string or a default value
}
//_ => {
//println!("Couldn't find file at path {}", path)
//}
}
}

Expand Down Expand Up @@ -167,82 +164,82 @@ fn parse_content(content: &String) -> Result<BTreeMap<usize, LineSlice>, &'stati

// /// Display part of the file (may be whole file if the file is short) to the terminal, starting
// /// at line number `line_start`.
fn display_content(content: &str, map: &BTreeMap<usize, LineSlice>,
line_start: usize, terminal: &Terminal)
-> Result<(), &'static str> {
// Get exclusive control of the terminal. It is locked through the whole function to
// avoid the overhead of locking it multiple times.
let mut locked_terminal = Terminal::new().expect("Failed to create terminal");
// let mut locked_terminal = terminal.lock();

// Calculate the last line to display. Make sure we don't extend over the end of the file.
let (_width, height) = locked_terminal.get_text_dimensions();
let mut line_end: usize = line_start + height;
if line_end > map.len() {
line_end = map.len();
}

// Refresh the terminal with the lines we've selected.
let start_indices = match map.get(&line_start) {
Some(indices) => indices,
None => return Err("failed to get the byte indices of the first line")
};
let end_indices = match map.get(&(line_end - 1)) {
Some(indices) => indices,
None => return Err("failed to get the byte indices of the last line")
};
locked_terminal.clear();
locked_terminal.print_to_terminal(
content[start_indices.start..end_indices.end].to_string()
);
locked_terminal.refresh_display()
}
// fn display_content(content: &str, map: &BTreeMap<usize, LineSlice>,
// line_start: usize, terminal: &Terminal)
// -> Result<(), &'static str> {
// // Get exclusive control of the terminal. It is locked through the whole function to
// // avoid the overhead of locking it multiple times.
// let mut locked_terminal = Terminal::new().expect("Failed to create terminal");
// // let mut locked_terminal = terminal.lock();

// // Calculate the last line to display. Make sure we don't extend over the end of the file.
// let (_width, height) = locked_terminal.get_text_dimensions();
// let mut line_end: usize = line_start + height;
// if line_end > map.len() {
// line_end = map.len();
// }

// // Refresh the terminal with the lines we've selected.
// let start_indices = match map.get(&line_start) {
// Some(indices) => indices,
// None => return Err("failed to get the byte indices of the first line")
// };
// let end_indices = match map.get(&(line_end - 1)) {
// Some(indices) => indices,
// None => return Err("failed to get the byte indices of the last line")
// };
// locked_terminal.clear();
// locked_terminal.print_to_terminal(
// content[start_indices.start..end_indices.end].to_string()
// );
// locked_terminal.refresh_display()
// }

// /// Handle user keyboard strikes and perform corresponding operations.
fn event_handler_loop(content: &String, map: &BTreeMap<usize, LineSlice>,
key_event_queue: &KeyEventQueueReader)
-> Result<(), &'static str> {
// Get a reference to this task's terminal. The terminal is *not* locked here.
//let terminal = app_io::get_my_terminal().ok_or("couldn't get terminal for `less` app")?;
let mut terminal = Terminal::new().expect("Failed to create terminal");

// Display the beginning of the file.
let mut line_start: usize = 0;
display_content(content, map, 0, &terminal)?;

// Handle user keyboard strikes.
loop {
match key_event_queue.read_one() {
Some(keyevent) => {
if keyevent.action != KeyAction::Pressed { continue; }
match keyevent.keycode {
// Quit the program on "Q".
Keycode::Q => {
//let mut locked_terminal = terminal.lock();
//locked_terminal.clear();
return terminal.refresh_display()
},
// Scroll down a line on "Down".
Keycode::Down => {
if line_start + 1 < map.len() {
line_start += 1;
}
display_content(content, map, line_start, &terminal)?;
},
// Scroll up a line on "Up".
Keycode::Up => {
if line_start > 0 {
line_start -= 1;
}
display_content(content, map, line_start, &terminal)?;
}
_ => {}
}
},
_ => {}
}
}
}
// fn event_handler_loop(content: &String, map: &BTreeMap<usize, LineSlice>,
// key_event_queue: &KeyEventQueueReader)
// -> Result<(), &'static str> {
// // Get a reference to this task's terminal. The terminal is *not* locked here.
// //let terminal = app_io::get_my_terminal().ok_or("couldn't get terminal for `less` app")?;
// let mut terminal = Terminal::new().expect("Failed to create terminal");

// // Display the beginning of the file.
// let mut line_start: usize = 0;
// display_content(content, map, 0, &terminal)?;

// // Handle user keyboard strikes.
// loop {
// match key_event_queue.read_one() {
// Some(keyevent) => {
// if keyevent.action != KeyAction::Pressed { continue; }
// match keyevent.keycode {
// // Quit the program on "Q".
// Keycode::Q => {
// //let mut locked_terminal = terminal.lock();
// //locked_terminal.clear();
// return terminal.refresh_display()
// },
// // Scroll down a line on "Down".
// Keycode::Down => {
// if line_start + 1 < map.len() {
// line_start += 1;
// }
// display_content(content, map, line_start, &terminal)?;
// },
// // Scroll up a line on "Up".
// Keycode::Up => {
// if line_start > 0 {
// line_start -= 1;
// }
// display_content(content, map, line_start, &terminal)?;
// }
// _ => {}
// }
// },
// _ => {}
// }
// }
// }


pub fn main(args: Vec<String>) -> isize {
Expand Down

0 comments on commit 26bf332

Please sign in to comment.