1- use std:: { fs:: { File , OpenOptions } , sync:: mpsc:: { Receiver , TryRecvError } , io:: { BufReader , Seek , SeekFrom , Read } , time:: Duration , path:: Path } ;
1+ use std:: { fs:: { File , OpenOptions } , sync:: mpsc:: { Receiver , TryRecvError } , io:: { BufReader , Seek , SeekFrom , Read } , time:: Duration , path:: { Path , PathBuf } } ;
22use directories:: UserDirs ;
33
44use crate :: constants;
@@ -8,9 +8,9 @@ use crate::constants;
88/// In case of error will return Some with that error message, otherwise will return None.
99/// For `printer` parameter you can pass any function or closure that you want to print log text with,
1010/// the string passed to said printer will consist of one or more lines of text.
11- pub fn tail_scriptslog < P > ( printer : P , refresh_time_millis : u64 , cancel_token : Receiver < ( ) > ) -> Option < String >
11+ pub fn tail_scriptslog < P > ( printer : P , refresh_time_millis : u64 , cancel_token : Receiver < ( ) > , custom_path : Option < String > ) -> Option < String >
1212where P : Fn ( & String ) -> ( ) {
13- match scriptslog_file ( ) {
13+ match scriptslog_file ( custom_path ) {
1414 Ok ( file) => {
1515 let mut reader = BufReader :: new ( & file) ;
1616 // start from the end of the file
@@ -56,38 +56,47 @@ where P: Fn(&String) -> () {
5656 }
5757}
5858
59- fn scriptslog_file ( ) -> Result < File , String > {
60- let mut docs = None ;
61- if let Some ( ud) = UserDirs :: new ( ) {
59+ fn scriptslog_file ( custon_path : Option < String > ) -> Result < File , String > {
60+ let scriptslog_path: PathBuf ;
61+
62+ if let Some ( custom_path) = custon_path {
63+ scriptslog_path = Path :: new ( & custom_path) . to_owned ( ) ;
64+ } else if let Some ( ud) = UserDirs :: new ( ) {
65+ let mut docs = None ;
6266 if cfg ! ( windows) {
6367 if let Some ( path) = ud. document_dir ( ) {
6468 docs = Some ( path. to_owned ( ) ) ;
6569 }
66- } else if cfg ! ( unix) {
70+ }
71+ else if cfg ! ( unix) {
6772 if let Some ( path) = Some ( ud. home_dir ( ) ) {
6873 docs = Some ( path. join ( constants:: LINUX_STEAM_PFX_PATH ) . to_owned ( ) ) ;
6974 }
70- } else {
75+ }
76+ else {
7177 unimplemented ! ( ) ;
7278 }
73- }
74-
75- if let Some ( docs) = docs {
76- let scriptslog_path = docs. join ( Path :: new ( "The Witcher 3" ) . join ( constants:: SCRIPTSLOG_FILE_NAME ) ) ;
77-
78- let file = OpenOptions :: new ( )
79- . read ( true )
80- . write ( true ) // so that it can be created if doesn't exist
81- . create ( true )
82- . open ( scriptslog_path ) ;
8379
84- if let Err ( e) = file {
85- println ! ( "{:?}" , e. kind( ) ) ;
86- return Err ( "File open error: " . to_owned ( ) + & e. to_string ( ) ) ;
80+ if let Some ( docs) = docs {
81+ scriptslog_path = docs. join ( Path :: new ( "The Witcher 3" ) . join ( constants:: SCRIPTSLOG_FILE_NAME ) ) ;
8782 } else {
88- return Ok ( file . unwrap ( ) ) ;
83+ return Err ( "Documents directory could not be found." . to_owned ( ) ) ;
8984 }
85+
86+ } else {
87+ return Err ( "Scriptslog path could not be resolved" . to_owned ( ) ) ;
88+ }
89+
90+ let file = OpenOptions :: new ( )
91+ . read ( true )
92+ . write ( true ) // so that it can be created if doesn't exist
93+ . create ( true )
94+ . open ( scriptslog_path) ;
95+
96+ if let Err ( e) = file {
97+ println ! ( "{:?}" , e. kind( ) ) ;
98+ return Err ( "File open error: " . to_owned ( ) + & e. to_string ( ) ) ;
9099 } else {
91- Err ( "Documents directory could not be found." . to_owned ( ) )
100+ return Ok ( file . unwrap ( ) ) ;
92101 }
93102}
0 commit comments