1010use crate :: pattern:: HistoryPattern ;
1111use crate :: shell:: environment:: Environment ;
1212use std:: collections:: VecDeque ;
13- use std:: io:: Read ;
14- use std:: io:: Write ;
13+ use std:: io:: ErrorKind ;
1514use std:: path:: Path ;
1615
1716#[ derive( Debug , Eq , PartialEq ) ]
@@ -188,35 +187,22 @@ impl History {
188187}
189188
190189fn read_history_from_file ( path : & Path , max_entries : u32 ) -> History {
191- match std:: fs:: File :: options ( )
192- . read ( true )
193- . write ( true )
194- . create ( true )
195- . open ( path)
196- {
197- Ok ( mut file) => {
198- let mut history = History :: new ( max_entries) ;
199- let mut file_contents = String :: new ( ) ;
200- if let Err ( err) = file. read_to_string ( & mut file_contents) {
201- eprintln ! (
202- "sh: failed to read history file at {}, details: {err}" ,
203- path. to_string_lossy( )
204- ) ;
205- return history;
206- }
207- for line in file_contents. lines ( ) {
190+ let mut history = History :: new ( max_entries) ;
191+ match std:: fs:: read_to_string ( path) {
192+ Ok ( contents) => {
193+ for line in contents. lines ( ) {
208194 history. add_entry ( line. to_string ( ) ) ;
209195 }
210- history
211196 }
197+ Err ( err) if err. kind ( ) == ErrorKind :: NotFound => { }
212198 Err ( err) => {
213199 eprintln ! (
214200 "sh: failed to open history file at {}, details: {err}" ,
215201 path. to_string_lossy( )
216202 ) ;
217- History :: new ( max_entries)
218203 }
219204 }
205+ history
220206}
221207
222208pub fn write_history_to_file ( history : & History , env : & Environment ) {
@@ -231,6 +217,7 @@ pub fn write_history_to_file(history: &History, env: &Environment) {
231217 let mut content = String :: new ( ) ;
232218 for entry in & history. entries {
233219 content. push_str ( & entry. command ) ;
220+ content. push ( '\n' ) ;
234221 }
235222 if let Err ( err) = std:: fs:: write ( & path, content) {
236223 eprintln ! ( "sh: failed to open history file at {} ({err})" , path) ;
0 commit comments