Skip to content

Commit 88404c4

Browse files
feat: custom path to the scriptslog
1 parent 93d64e4 commit 88404c4

2 files changed

Lines changed: 39 additions & 26 deletions

File tree

src/cli/local_subcommands.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ pub(crate) enum LocalSubcommands {
2121

2222
/// Filter out lines that do not containt highlighted text
2323
#[clap(short, long)]
24-
filter_non_highlighted: bool
24+
filter_non_highlighted: bool,
25+
26+
/// Specify a custom, full path to the scriptslog file
27+
#[clap(short='p', long)]
28+
custom_path: Option<String>
2529
}
2630
}
2731

@@ -57,9 +61,9 @@ pub(crate) fn handle_local_subcommand( cmd: LocalSubcommands, options: CliOption
5761
if !options.no_wait { thread::sleep( Duration::from_millis(3000) ) }
5862

5963
match cmd {
60-
LocalSubcommands::Scriptslog { colors, refresh_time, filter_non_highlighted } => {
64+
LocalSubcommands::Scriptslog { colors, refresh_time, filter_non_highlighted, custom_path } => {
6165
let highlights = scriptslog_colors_to_highlight_records(colors);
62-
if let Some(err) = rw3d_core::scriptslog::tail_scriptslog(|text| scriptslog_printer(text, &highlights, filter_non_highlighted), refresh_time, logger_rcv) {
66+
if let Some(err) = rw3d_core::scriptslog::tail_scriptslog(|text| scriptslog_printer(text, &highlights, filter_non_highlighted), refresh_time, logger_rcv, custom_path) {
6367
println!("{}", err);
6468
}
6569
}

src/core/scriptslog.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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}};
22
use directories::UserDirs;
33

44
use 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>
1212
where 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

Comments
 (0)