interactive_edit/
interactive_edit.rs

1use open_editor::EditorCallBuilder;
2use std::{
3    io::{self, Write},
4    path::PathBuf,
5    process::exit,
6};
7
8fn main() -> Result<(), Box<dyn std::error::Error>> {
9    let (file_path, line, column) = get_parameters()?;
10
11    EditorCallBuilder::new()
12        .at_line(line)
13        .at_column(column)
14        .open_file(&file_path)?;
15    Ok(())
16}
17
18fn get_parameters() -> Result<(PathBuf, usize, usize), Box<dyn std::error::Error>> {
19    print!("Path to file to open [./Cargo.toml]: ");
20    io::stdout().flush()?;
21    let mut file_path = String::new();
22    io::stdin().read_line(&mut file_path)?;
23    if file_path.trim().is_empty() {
24        file_path = "./Cargo.toml".to_string();
25    }
26    let file_path = PathBuf::from(file_path.trim());
27    print!("Line number: ");
28    io::stdout().flush()?;
29    let mut line = String::new();
30    io::stdin().read_line(&mut line)?;
31    let line: usize = if let Ok(num) = line.trim().parse() {
32        num
33    } else {
34        eprintln!("Error: Invalid line number");
35        exit(-1)
36    };
37    print!("Column number: ");
38    io::stdout().flush()?;
39    let mut column = String::new();
40    io::stdin().read_line(&mut column)?;
41    let column: usize = if let Ok(num) = column.trim().parse() {
42        num
43    } else {
44        eprintln!("Error: Invalid column number");
45        exit(-1)
46    };
47    Ok((file_path, line, column))
48}