open_editor/
editor_kind.rs1use std::{fmt::Display, path::Path};
2
3#[derive(Default, Debug, Clone)]
4pub enum EditorKind {
5 Vi,
7 Vim,
8 Nvim,
9 Emacs,
10 Nano,
11 Pico,
12 Helix,
13 Kakoune,
14 Code,
16 Gvim,
17 #[default]
18 UnknownEditor,
19}
20
21impl From<String> for EditorKind {
22 fn from(value: String) -> Self {
24 match value.as_str() {
25 "vi" => EditorKind::Vi,
26 "vim" => EditorKind::Vim,
27 "nvim" => EditorKind::Nvim,
28 "emacs" => EditorKind::Emacs,
29 "nano" => EditorKind::Nano,
30 "pico" => EditorKind::Pico,
31 "hx" => EditorKind::Helix,
32 "kak" => EditorKind::Kakoune,
33 "code" | "vscode" => EditorKind::Code,
34 "gvim" => EditorKind::Gvim,
35 _ => EditorKind::UnknownEditor,
36 }
37 }
38}
39impl EditorKind {
41 pub(crate) fn get_editor_args(
42 &self,
43 file_path: &Path,
44 wait: bool,
45 line: usize,
46 column: usize,
47 ) -> Vec<String> {
48 let path = file_path.to_string_lossy().into_owned();
49 match self {
50 EditorKind::Emacs => {
51 vec![format!("+{}:{}", line, column), path]
52 }
53 EditorKind::Nano | EditorKind::Pico => {
54 vec![format!("+{},{}", line, column), path]
55 }
56 EditorKind::Helix => {
57 vec![format!("{}:{}:{}", path, line, column)]
58 }
59 EditorKind::Kakoune => {
60 vec![format!("{}", path), format!("+{}:{}", line, column)]
61 }
62 EditorKind::Code => [
63 if wait { vec!["-w".to_string()] } else { vec![] },
64 vec!["--goto".to_string()],
65 vec![format!("{}:{}:{}", path, line, column)],
66 ]
67 .concat(),
68 EditorKind::Gvim | EditorKind::Vi | EditorKind::Vim | EditorKind::Nvim => [
69 vec![format!("+{}", line,)],
70 if wait {
71 vec![]
72 } else {
73 vec!["--nofork".to_string()]
74 },
75 vec![path],
76 ]
77 .concat(),
78
79 EditorKind::UnknownEditor => vec![path],
80 }
81 }
82}
83impl Display for EditorKind {
84 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85 match self {
86 EditorKind::Vi => write!(f, "vi"),
87 EditorKind::Vim => write!(f, "vim"),
88 EditorKind::Nvim => write!(f, "nvim"),
89 EditorKind::Emacs => write!(f, "emacs"),
90 EditorKind::Nano => write!(f, "nano"),
91 EditorKind::Pico => write!(f, "pico"),
92 EditorKind::Helix => write!(f, "hx"),
93 EditorKind::Kakoune => write!(f, "kak"),
94 EditorKind::Code => write!(f, "code"),
95 EditorKind::Gvim => write!(f, "gvim"),
96 EditorKind::UnknownEditor => Err(std::fmt::Error),
97 }
98 }
99}