interactive_edit/
interactive_edit.rs

1use open_editor::editor_call_builder::EditorCallBuilder;
2use std::{
3    io::{self, Write},
4    process::exit,
5};
6
7fn main() -> Result<(), Box<dyn std::error::Error>> {
8    let (filename, line, column) = get_parameters()?;
9
10    EditorCallBuilder::new(filename)?
11        .at_line(line)
12        .at_column(column)
13        .call_editor()?;
14
15    Ok(())
16}
17
18fn get_parameters() -> Result<(String, usize, usize), Box<dyn std::error::Error>> {
19    print!("Path to file to open [./Cargo.toml]: ");
20    io::stdout().flush()?;
21    let mut filename = String::new();
22    io::stdin().read_line(&mut filename)?;
23    let mut filename = filename.trim();
24    if filename.is_empty() {
25        filename = "./Cargo.toml";
26    }
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((filename.to_owned(), line, column))
48}