Skip to content

Commit 1cd74ad

Browse files
author
Stephan Dilly
committed
fix messing up users terminal after opening file in vi (related to #173)
1 parent 0fd8a0e commit 1cd74ad

File tree

4 files changed

+47
-42
lines changed

4 files changed

+47
-42
lines changed

src/app.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,11 @@ impl App {
200200
self.external_editor_popup.hide();
201201
if let InputState::Paused = polling_state {
202202
let result = match self.file_to_open.take() {
203-
Some(path) => crate::open_file_in_editor(&path),
203+
Some(path) => {
204+
ExternalEditorComponent::open_file_in_editor(
205+
&path,
206+
)
207+
}
204208
None => self.commit.show_editor(),
205209
};
206210

src/components/commit.rs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::{
22
textinput::TextInputComponent, visibility_blocking,
33
CommandBlocking, CommandInfo, Component, DrawableComponent,
4+
ExternalEditorComponent,
45
};
56
use crate::{
67
get_app_config_path, keys,
@@ -13,15 +14,10 @@ use asyncgit::{
1314
sync::{self, CommitId, HookResult},
1415
CWD,
1516
};
16-
use crossterm::{
17-
event::Event,
18-
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
19-
ExecutableCommand,
20-
};
21-
use scopeguard::defer;
17+
use crossterm::event::Event;
2218
use std::{
2319
fs::File,
24-
io::{self, Read, Write},
20+
io::{Read, Write},
2521
path::PathBuf,
2622
};
2723
use tui::{backend::Backend, layout::Rect, Frame};
@@ -144,11 +140,11 @@ impl CommitComponent {
144140

145141
pub fn show_editor(&mut self) -> Result<()> {
146142
const COMMIT_MSG_FILE_NAME: &str = "COMMITMSG_EDITOR";
143+
//TODO: use a tmpfile here
147144
let mut config_path: PathBuf = get_app_config_path()?;
148145
config_path.push(COMMIT_MSG_FILE_NAME);
149146

150147
{
151-
//TODO: use a tmpfile here
152148
let mut file = File::create(&config_path)?;
153149
file.write_fmt(format_args!(
154150
"{}\n",
@@ -157,16 +153,10 @@ impl CommitComponent {
157153
file.write_all(strings::COMMIT_EDITOR_MSG.as_bytes())?;
158154
}
159155

160-
io::stdout().execute(LeaveAlternateScreen)?;
161-
defer! {
162-
io::stdout().execute(EnterAlternateScreen).expect("reset terminal");
163-
}
164-
165-
crate::open_file_in_editor(&config_path)?;
156+
ExternalEditorComponent::open_file_in_editor(&config_path)?;
166157

167158
let mut message = String::new();
168159

169-
//TODO: see above
170160
let mut file = File::open(&config_path)?;
171161
file.read_to_string(&mut message)?;
172162
drop(file);

src/components/externaleditor.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ use crate::{
66
strings,
77
ui::{self, style::SharedTheme},
88
};
9-
use anyhow::Result;
10-
use crossterm::event::Event;
9+
use anyhow::{anyhow, Result};
10+
use crossterm::{
11+
event::Event,
12+
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
13+
ExecutableCommand,
14+
};
15+
use scopeguard::defer;
16+
use std::{env, io, path::Path, process::Command};
1117
use tui::{
1218
backend::Backend,
1319
layout::Rect,
@@ -29,6 +35,34 @@ impl ExternalEditorComponent {
2935
theme,
3036
}
3137
}
38+
39+
///
40+
pub fn open_file_in_editor(path: &Path) -> Result<()> {
41+
io::stdout().execute(LeaveAlternateScreen)?;
42+
defer! {
43+
io::stdout().execute(EnterAlternateScreen).expect("reset terminal");
44+
}
45+
46+
let mut editor = env::var("GIT_EDITOR")
47+
.ok()
48+
.or_else(|| env::var("VISUAL").ok())
49+
.or_else(|| env::var("EDITOR").ok())
50+
.unwrap_or_else(|| String::from("vi"));
51+
editor.push_str(&format!(" {}", path.to_string_lossy()));
52+
53+
let mut editor = editor.split_whitespace();
54+
55+
let command = editor.next().ok_or_else(|| {
56+
anyhow!("unable to read editor command")
57+
})?;
58+
59+
Command::new(command)
60+
.args(editor)
61+
.status()
62+
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;
63+
64+
Ok(())
65+
}
3266
}
3367

3468
impl DrawableComponent for ExternalEditorComponent {

src/main.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ use scopeguard::defer;
4343
use scopetime::scope_time;
4444
use simplelog::{Config, LevelFilter, WriteLogger};
4545
use spinner::Spinner;
46-
use std::process::Command;
4746
use std::{
4847
env, fs,
4948
fs::File,
5049
io::{self, Write},
5150
panic,
52-
path::{Path, PathBuf},
51+
path::PathBuf,
5352
process,
5453
time::{Duration, Instant},
5554
};
@@ -242,28 +241,6 @@ fn migrate_config() -> Result<()> {
242241
Ok(())
243242
}
244243

245-
fn open_file_in_editor(path: &Path) -> Result<()> {
246-
let mut editor = env::var("GIT_EDITOR")
247-
.ok()
248-
.or_else(|| env::var("VISUAL").ok())
249-
.or_else(|| env::var("EDITOR").ok())
250-
.unwrap_or_else(|| String::from("vi"));
251-
editor.push_str(&format!(" {}", path.to_string_lossy()));
252-
253-
let mut editor = editor.split_whitespace();
254-
255-
let command = editor
256-
.next()
257-
.ok_or_else(|| anyhow!("unable to read editor command"))?;
258-
259-
Command::new(command)
260-
.args(editor)
261-
.status()
262-
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;
263-
264-
Ok(())
265-
}
266-
267244
fn get_app_cache_path() -> Result<PathBuf> {
268245
let mut path = dirs::cache_dir()
269246
.ok_or_else(|| anyhow!("failed to find os cache dir."))?;

0 commit comments

Comments
 (0)