| 1 | //! `edit` lets you open and edit something in a text editor, regardless of platform. |
| 2 | //! (Think `git commit`.) |
| 3 | //! |
| 4 | //! It works on Windows, Mac, and Linux, and [knows about] lots of different text editors to fall |
| 5 | //! back upon in case standard environment variables such as `VISUAL` and `EDITOR` aren't set. |
| 6 | //! |
| 7 | //! ```rust,ignore |
| 8 | //! let template = "Fill in the blank: Hello, _____!"; |
| 9 | //! let edited = edit::edit(template)?; |
| 10 | //! println!("after editing: '{}'", edited); |
| 11 | //! // after editing: 'Fill in the blank: Hello, world!' |
| 12 | //! ``` |
| 13 | //! |
| 14 | //! [knows about]: ../src/edit/lib.rs.html#31-61 |
| 15 | //! |
| 16 | //! Features |
| 17 | //! ======== |
| 18 | //! |
| 19 | //! The `edit` crate has the following optional features: |
| 20 | //! |
| 21 | //! - `better-path` *(enabled by default)* — Use |
| 22 | //! [`which`](https://docs.rs/which) to locate executable programs in `PATH`. |
| 23 | //! If this is disabled, programs are still looked up in `PATH`, but a basic |
| 24 | //! search is used that does not check for executability. |
| 25 | //! |
| 26 | //! - `quoted-env` — Use [`shell-words`](https://docs.rs/shell-words) to split |
| 27 | //! apart the values of the `VISUAL` and `EDITOR` environment variables. If |
| 28 | //! this is disabled, the envvars are split up on whitespace. |
| 29 | |
| 30 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 31 | envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/, |
| 32 | ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#, |
| 33 | fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/, |
| 34 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#, ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#, Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}, |
| 35 | pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/::{Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#, PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#}, |
| 36 | processrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/::{Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#, Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#}, |
| 37 | }; |
| 38 | pub use tempfile::Builder; |
| 39 | #[cfg(feature = "which")] |
| 40 | use which::which; |
| 41 | |
| 42 | static ENV_VARSrust-analyzer cargo edit 0.1.5 ENV_VARS.: &[&str] = &["VISUAL", "EDITOR"]; |
| 43 | |
| 44 | // TODO: should we hardcode full paths as well in case $PATH is borked? |
| 45 | #[cfg(not(any(target_os = "windows", target_os = "macos")))] |
| 46 | #[rustfmt::skip] |
| 47 | static HARDCODED_NAMESrust-analyzer cargo edit 0.1.5 HARDCODED_NAMES.: &[&str] = &[ |
| 48 | // CLI editors |
| 49 | "sensible-editor", "nano", "pico", "vim", "nvim", "vi", "emacs", |
| 50 | // GUI editors |
| 51 | "code", "atom", "subl", "gedit", "gvim", |
| 52 | // Generic "file openers" |
| 53 | "xdg-open", "gnome-open", "kde-open", |
| 54 | ]; |
| 55 | |
| 56 | #[cfg(target_os = "macos")] |
| 57 | #[rustfmt::skip] |
| 58 | static HARDCODED_NAMES: &[&str] = &[ |
| 59 | // CLI editors |
| 60 | "nano", "pico", "vim", "nvim", "vi", "emacs", |
| 61 | // open has a special flag to open in the default text editor |
| 62 | // (this really should come before the CLI editors, but in order |
| 63 | // not to break compatibility, we still prefer CLI over GUI) |
| 64 | "open -Wt", |
| 65 | // GUI editors |
| 66 | "code -w", "atom -w", "subl -w", "gvim", "mate", |
| 67 | // Generic "file openers" |
| 68 | "open -a TextEdit", |
| 69 | "open -a TextMate", |
| 70 | // TODO: "open -f" reads input from standard input and opens with |
| 71 | // TextEdit. if this flag were used we could skip the tempfile |
| 72 | "open", |
| 73 | ]; |
| 74 | |
| 75 | #[cfg(target_os = "windows")] |
| 76 | #[rustfmt::skip] |
| 77 | static HARDCODED_NAMES: &[&str] = &[ |
| 78 | // GUI editors |
| 79 | "code.cmd -n -w", "atom.exe -w", "subl.exe -w", |
| 80 | // notepad++ does not block for input |
| 81 | // Installed by default |
| 82 | "notepad.exe", |
| 83 | // Generic "file openers" |
| 84 | "cmd.exe /C start", |
| 85 | ]; |
| 86 | |
| 87 | #[cfg(feature = "better-path")] |
| 88 | fn get_full_editor_path<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#>>(binary_name: T) -> which::Result<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 89 | which(binary_name) |
| 90 | } |
| 91 | |
| 92 | #[cfg(not(feature = "better-path"))] |
| 93 | fn get_full_editor_pathrust-analyzer cargo edit 0.1.5 get_full_editor_path().<T: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<OsStrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/OsStr#> + AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(binary_name: T) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 94 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(paths) = envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::var_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var_os().("PATH") { |
| 95 | for dir in envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::split_pathsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/split_paths().(&paths) { |
| 96 | if dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(&binary_name).is_filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]is_file().() { |
| 97 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(dir.joinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]join().(&binary_name)); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][`From<ErrorKind>`]from().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#)) |
| 103 | } |
| 104 | |
| 105 | #[cfg(not(feature = "quoted-env"))] |
| 106 | fn string_to_cmdrust-analyzer cargo edit 0.1.5 string_to_cmd().(s: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> (PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) { |
| 107 | let mut args = s.split_ascii_whitespacerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_ascii_whitespace().(); |
| 108 | ( |
| 109 | args.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/iter/impl#[`SplitAsciiWhitespace<'a>`][Iterator]next().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 110 | args.maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::fromrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`From<&str>`]from().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 111 | ) |
| 112 | } |
| 113 | |
| 114 | #[cfg(feature = "quoted-env")] |
| 115 | fn string_to_cmd(s: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> (PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>) { |
| 116 | match shell_words::split(&s) { |
| 117 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mut v) if !v.is_empty() => (v.remove(0).into(), v), |
| 118 | _ => { |
| 119 | let mut args = s.split_ascii_whitespace(); |
| 120 | ( |
| 121 | args.next().unwrap().into(), |
| 122 | args.map(Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from).collect(), |
| 123 | ) |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | fn get_full_editor_cmdrust-analyzer cargo edit 0.1.5 get_full_editor_cmd().(s: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>)> { |
| 129 | let (path, args) = string_to_cmdrust-analyzer cargo edit 0.1.5 string_to_cmd().(s); |
| 130 | match get_full_editor_pathrust-analyzer cargo edit 0.1.5 get_full_editor_path().(&path) { |
| 131 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(result) => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((result, args)), |
| 132 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) if path.existsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]exists().() => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((path, args)), |
| 133 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][`From<ErrorKind>`]from().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#)) |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | fn get_editor_argsrust-analyzer cargo edit 0.1.5 get_editor_args().() -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<(PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#, Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>)> { |
| 138 | ENV_VARSrust-analyzer cargo edit 0.1.5 ENV_VARS. |
| 139 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 140 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(envrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/::var_osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std env/var_os().) |
| 141 | .filterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter().(|v| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().v.is_emptyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsStr]is_empty().()) |
| 142 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|v| v.into_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/os_str/impl#[OsString]into_string().().okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 143 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|s| get_full_editor_cmdrust-analyzer cargo edit 0.1.5 get_full_editor_cmd().(s).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 144 | .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().() |
| 145 | .or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]or_else().(|| { |
| 146 | HARDCODED_NAMESrust-analyzer cargo edit 0.1.5 HARDCODED_NAMES. |
| 147 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 148 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|s| s.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 149 | .filter_maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#filter_map().(|s| get_full_editor_cmdrust-analyzer cargo edit 0.1.5 get_full_editor_cmd().(s).okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]ok().()) |
| 150 | .nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/adapters/filter_map/impl#[`FilterMap<I, F>`][Iterator]next().() |
| 151 | }) |
| 152 | .ok_or_elserust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]ok_or_else().(|| Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][`From<ErrorKind>`]from().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::NotFoundrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#NotFound#)) |
| 153 | } |
| 154 | |
| 155 | /// Find the system default editor, if there is one. |
| 156 | /// |
| 157 | /// This function checks several sources to find an editor binary (in order of precedence): |
| 158 | /// |
| 159 | /// - the `VISUAL` environment variable |
| 160 | /// - the `EDITOR` environment variable |
| 161 | /// - hardcoded lists of common CLI editors on MacOS/Unix |
| 162 | /// - hardcoded lists of GUI editors on Windows/MacOS/Unix |
| 163 | /// - platform-specific generic "file openers" (e.g. `xdg-open` on Linux and `open` on MacOS) |
| 164 | /// |
| 165 | /// Also, it doesn't blindly return whatever is in an environment variable. If a specified editor |
| 166 | /// can't be found or isn't marked as executable (the executable bit is checked when the default |
| 167 | /// feature `better-path` is enabled), this function will fall back to the next one that is. |
| 168 | /// |
| 169 | /// # Returns |
| 170 | /// |
| 171 | /// If successful, returns the name of the system default editor. |
| 172 | /// Note that in most cases the full path of the editor isn't returned; what is guaranteed is the |
| 173 | /// return value being suitable as the program name for e.g. [`Command::new`]. |
| 174 | /// |
| 175 | /// On some platforms, a text editor is installed by default, so the chances of a failure are low |
| 176 | /// save for `PATH` being unset or something weird like that. However, it is possible for one not |
| 177 | /// to be located, and in that case `get_editor` will return [`ErrorKind::NotFound`]. |
| 178 | /// |
| 179 | /// # Example |
| 180 | /// |
| 181 | /// ```rust,ignore |
| 182 | /// use edit::get_editor; |
| 183 | /// |
| 184 | /// // will print e.g. "default editor: nano" |
| 185 | /// println!("default editor:", get_editor().expect("can't find an editor").to_str()); |
| 186 | /// ``` |
| 187 | /// |
| 188 | /// [`Command::new`]: https://doc.rust-lang.org/std/process/struct.Command.html#method.new |
| 189 | /// [`ErrorKind::NotFound`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.NotFound |
| 190 | pub fn get_editorrust-analyzer cargo edit 0.1.5 get_editor().() -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<PathBufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/PathBuf#> { |
| 191 | get_editor_argsrust-analyzer cargo edit 0.1.5 get_editor_args().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map().(|(x, _)| x) |
| 192 | } |
| 193 | |
| 194 | /// Open the contents of a string or buffer in the [default editor]. |
| 195 | /// |
| 196 | /// This function saves its input to a temporary file and then opens the default editor to it. |
| 197 | /// It waits for the editor to return, re-reads the (possibly changed/edited) temporary file, and |
| 198 | /// then deletes it. |
| 199 | /// |
| 200 | /// # Arguments |
| 201 | /// |
| 202 | /// `text` is written to the temporary file before invoking the editor. (The editor opens with |
| 203 | /// the contents of `text` already in the file). |
| 204 | /// |
| 205 | /// # Returns |
| 206 | /// |
| 207 | /// If successful, returns the edited string. |
| 208 | /// If the edited version of the file can't be decoded as UTF-8, returns [`ErrorKind::InvalidData`]. |
| 209 | /// If no text editor could be found, returns [`ErrorKind::NotFound`]. |
| 210 | /// Any errors related to spawning the editor process will also be passed through. |
| 211 | /// |
| 212 | /// [default editor]: fn.get_editor.html |
| 213 | /// [`ErrorKind::InvalidData`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.InvalidData |
| 214 | /// [`ErrorKind::NotFound`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.NotFound |
| 215 | pub fn editrust-analyzer cargo edit 0.1.5 edit().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(text: S) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 216 | let builder = Builder::new(); |
| 217 | edit_with_builderrust-analyzer cargo edit 0.1.5 edit_with_builder().(text, &builder) |
| 218 | } |
| 219 | |
| 220 | /// Open the contents of a string or buffer in the [default editor] using a temporary file with a |
| 221 | /// custom path or filename. |
| 222 | /// |
| 223 | /// This function saves its input to a temporary file created using `builder`, then opens the |
| 224 | /// default editor to it. It waits for the editor to return, re-reads the (possibly changed/edited) |
| 225 | /// temporary file, and then deletes it. |
| 226 | /// |
| 227 | /// Other than the custom [`Builder`], this function is identical to [`edit`]. |
| 228 | /// |
| 229 | /// # Arguments |
| 230 | /// |
| 231 | /// `builder` is used to create a temporary file, potentially with a custom name, path, or prefix. |
| 232 | /// |
| 233 | /// `text` is written to the temporary file before invoking the editor. (The editor opens with |
| 234 | /// the contents of `text` already in the file). |
| 235 | /// |
| 236 | /// # Returns |
| 237 | /// |
| 238 | /// If successful, returns the edited string. |
| 239 | /// If the temporary file can't be created with the provided builder, may return any error returned |
| 240 | /// by [`OpenOptions::open`]. |
| 241 | /// If the edited version of the file can't be decoded as UTF-8, returns [`ErrorKind::InvalidData`]. |
| 242 | /// If no text editor could be found, returns [`ErrorKind::NotFound`]. |
| 243 | /// Any errors related to spawning the editor process will also be passed through. |
| 244 | /// |
| 245 | /// [default editor]: fn.get_editor.html |
| 246 | /// [`edit`]: fn.edit.html |
| 247 | /// [`Builder`]: struct.Builder.html |
| 248 | /// [`OpenOptions::open`]: https://doc.rust-lang.org/std/fs/struct.OpenOptions.html#errors |
| 249 | /// [`ErrorKind::InvalidData`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.InvalidData |
| 250 | /// [`ErrorKind::NotFound`]: https://doc.rust-lang.org/std/io/enum.ErrorKind.html#variant.NotFound |
| 251 | pub fn edit_with_builderrust-analyzer cargo edit 0.1.5 edit_with_builder().<S: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(text: S, builder: &Builder) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#> { |
| 252 | Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::from_utf8rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]from_utf8().(edit_bytes_with_builderrust-analyzer cargo edit 0.1.5 edit_bytes_with_builder().(text, builder)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().) |
| 253 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|_| Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::fromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error][`From<ErrorKind>`]from().(ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#)) |
| 254 | } |
| 255 | |
| 256 | /// Open the contents of a string or buffer in the [default editor] and return them as raw bytes. |
| 257 | /// |
| 258 | /// See [`edit`], the version of this function that takes and returns [`String`]. |
| 259 | /// |
| 260 | /// # Arguments |
| 261 | /// |
| 262 | /// `buf` is written to the temporary file before invoking the editor. |
| 263 | /// |
| 264 | /// # Returns |
| 265 | /// |
| 266 | /// If successful, returns the contents of the temporary file in raw (`Vec<u8>`) form. |
| 267 | /// |
| 268 | /// [default editor]: fn.get_editor.html |
| 269 | /// [`edit`]: fn.edit.html |
| 270 | /// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html |
| 271 | pub fn edit_bytesrust-analyzer cargo edit 0.1.5 edit_bytes().<B: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(buf: B) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> { |
| 272 | let builder = Builder::new(); |
| 273 | edit_bytes_with_builderrust-analyzer cargo edit 0.1.5 edit_bytes_with_builder().(buf, &builder) |
| 274 | } |
| 275 | |
| 276 | /// Open the contents of a string or buffer in the [default editor] using a temporary file with a |
| 277 | /// custom path or filename and return them as raw bytes. |
| 278 | /// |
| 279 | /// See [`edit_with_builder`], the version of this function that takes and returns [`String`]. |
| 280 | /// |
| 281 | /// Other than the custom [`Builder`], this function is identical to [`edit_bytes`]. |
| 282 | /// |
| 283 | /// # Arguments |
| 284 | /// |
| 285 | /// `builder` is used to create a temporary file, potentially with a custom name, path, or prefix. |
| 286 | /// |
| 287 | /// `buf` is written to the temporary file before invoking the editor. |
| 288 | /// |
| 289 | /// # Returns |
| 290 | /// |
| 291 | /// If successful, returns the contents of the temporary file in raw (`Vec<u8>`) form. |
| 292 | /// |
| 293 | /// [default editor]: fn.get_editor.html |
| 294 | /// [`edit_with_builder`]: fn.edit_with_builder.html |
| 295 | /// [`String`]: https://doc.rust-lang.org/std/string/struct.String.html |
| 296 | /// [`Builder`]: struct.Builder.html |
| 297 | /// [`edit_bytes`]: fn.edit_bytes.html |
| 298 | pub fn edit_bytes_with_builderrust-analyzer cargo edit 0.1.5 edit_bytes_with_builder().<B: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>>(buf: B, builder: &Builder) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>> { |
| 299 | let mut file = builder.tempfile()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().; |
| 300 | file.write_all(buf.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().; |
| 301 | |
| 302 | let path = file.into_temp_path(); |
| 303 | edit_filerust-analyzer cargo edit 0.1.5 edit_file().(&path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 304 | |
| 305 | let edited = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read().(&path)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 306 | |
| 307 | path.close()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/try_trait/Try#branch().; |
| 308 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(edited) |
| 309 | } |
| 310 | |
| 311 | /// Open an existing file (or create a new one, depending on the editor's behavior) in the |
| 312 | /// [default editor] and wait for the editor to exit. |
| 313 | /// |
| 314 | /// # Arguments |
| 315 | /// |
| 316 | /// A [`Path`] to a file, new or existing, to open in the default editor. |
| 317 | /// |
| 318 | /// # Returns |
| 319 | /// |
| 320 | /// A Result is returned in case of errors finding or spawning the editor, but the contents of the |
| 321 | /// file are not read and returned as in [`edit`] and [`edit_bytes`]. |
| 322 | /// |
| 323 | /// [default editor]: fn.get_editor.html |
| 324 | /// [`Path`]: https://doc.rust-lang.org/std/path/struct.Path.html |
| 325 | /// [`edit`]: fn.edit.html |
| 326 | /// [`edit_bytes`]: fn.edit_bytes.html |
| 327 | pub fn edit_filerust-analyzer cargo edit 0.1.5 edit_file().<P: AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<Pathrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/Path#>>(file: P) -> Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 328 | let (editor, args) = get_editor_argsrust-analyzer cargo edit 0.1.5 get_editor_args().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 329 | let status = Commandrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Command#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]new().(&editor) |
| 330 | .argsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]args().(&args) |
| 331 | .argrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]arg().(file.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().()) |
| 332 | .stdinrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdin().(Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#::inheritrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Stdio]inherit().()) |
| 333 | .stdoutrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stdout().(Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#::inheritrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Stdio]inherit().()) |
| 334 | .stderrrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]stderr().(Stdiorust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Stdio#::inheritrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Stdio]inherit().()) |
| 335 | .outputrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[Command]output().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch(). |
| 336 | .statusrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/Output#status.; |
| 337 | |
| 338 | if status.successrust-analyzer cargo std https://github.com/rust-lang/rust/library/std process/impl#[ExitStatus]success().() { |
| 339 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 340 | } else { |
| 341 | let full_command = if args.is_emptyrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]is_empty().() { |
| 342 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 343 | "{} {}", |
| 344 | editor.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().(), |
| 345 | file.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().() |
| 346 | ) |
| 347 | } else { |
| 348 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 349 | "{} {} {}", |
| 350 | editor.to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().(), |
| 351 | args.joinrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]join().(" "), |
| 352 | file.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#as_ref().().to_string_lossyrust-analyzer cargo std https://github.com/rust-lang/rust/library/std path/impl#[Path]to_string_lossy().() |
| 353 | ) |
| 354 | }; |
| 355 | |
| 356 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().( |
| 357 | ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::Otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#Other#, |
| 358 | formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("editor '{}' exited with error: {}", full_command, status), |
| 359 | )) |
| 360 | } |
| 361 | } |