From 69e40a3ea55248edc461b82ab460690bbd01f6fc Mon Sep 17 00:00:00 2001 From: Ingrid Date: Sat, 30 Dec 2023 16:33:40 +0100 Subject: [PATCH] handle NotFound error when reading histfile before it has been created --- helix-term/src/session.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/helix-term/src/session.rs b/helix-term/src/session.rs index 1019b6a9f..2a6ccce46 100644 --- a/helix-term/src/session.rs +++ b/helix-term/src/session.rs @@ -1,7 +1,7 @@ use helix_loader::command_histfile; use std::{ fs::{File, OpenOptions}, - io::{self, BufRead, BufReader, Lines, Write}, + io::{self, BufRead, BufReader, Write}, path::PathBuf, }; @@ -22,14 +22,23 @@ pub fn push_history(register: char, line: &str) { writeln!(file, "{}", line).unwrap(); } -fn read_histfile(filepath: PathBuf) -> Lines> { - // TODO: do something about this unwrap - BufReader::new(File::open(filepath).unwrap()).lines() +fn read_histfile(filepath: PathBuf) -> Vec { + match File::open(filepath) { + Ok(file) => { + BufReader::new(file) + .lines() + .collect::>>() + // TODO: do something about this unwrap + .unwrap() + } + Err(e) => match e.kind() { + io::ErrorKind::NotFound => Vec::new(), + // TODO: do something about this panic + _ => panic!(), + }, + } } pub fn read_command_history() -> Vec { read_histfile(command_histfile()) - .collect::>>() - // TODO: do something about this unwrap - .unwrap() }