mirror of
https://github.com/gitui-org/gitui
synced 2026-05-24 09:28:21 +00:00
fix messing up users terminal after opening file in vi (related to #173)
This commit is contained in:
parent
0fd8a0e7e0
commit
1cd74ad02e
4 changed files with 47 additions and 42 deletions
|
|
@ -200,7 +200,11 @@ impl App {
|
||||||
self.external_editor_popup.hide();
|
self.external_editor_popup.hide();
|
||||||
if let InputState::Paused = polling_state {
|
if let InputState::Paused = polling_state {
|
||||||
let result = match self.file_to_open.take() {
|
let result = match self.file_to_open.take() {
|
||||||
Some(path) => crate::open_file_in_editor(&path),
|
Some(path) => {
|
||||||
|
ExternalEditorComponent::open_file_in_editor(
|
||||||
|
&path,
|
||||||
|
)
|
||||||
|
}
|
||||||
None => self.commit.show_editor(),
|
None => self.commit.show_editor(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
use super::{
|
use super::{
|
||||||
textinput::TextInputComponent, visibility_blocking,
|
textinput::TextInputComponent, visibility_blocking,
|
||||||
CommandBlocking, CommandInfo, Component, DrawableComponent,
|
CommandBlocking, CommandInfo, Component, DrawableComponent,
|
||||||
|
ExternalEditorComponent,
|
||||||
};
|
};
|
||||||
use crate::{
|
use crate::{
|
||||||
get_app_config_path, keys,
|
get_app_config_path, keys,
|
||||||
|
|
@ -13,15 +14,10 @@ use asyncgit::{
|
||||||
sync::{self, CommitId, HookResult},
|
sync::{self, CommitId, HookResult},
|
||||||
CWD,
|
CWD,
|
||||||
};
|
};
|
||||||
use crossterm::{
|
use crossterm::event::Event;
|
||||||
event::Event,
|
|
||||||
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
|
|
||||||
ExecutableCommand,
|
|
||||||
};
|
|
||||||
use scopeguard::defer;
|
|
||||||
use std::{
|
use std::{
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, Read, Write},
|
io::{Read, Write},
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
};
|
};
|
||||||
use tui::{backend::Backend, layout::Rect, Frame};
|
use tui::{backend::Backend, layout::Rect, Frame};
|
||||||
|
|
@ -144,11 +140,11 @@ impl CommitComponent {
|
||||||
|
|
||||||
pub fn show_editor(&mut self) -> Result<()> {
|
pub fn show_editor(&mut self) -> Result<()> {
|
||||||
const COMMIT_MSG_FILE_NAME: &str = "COMMITMSG_EDITOR";
|
const COMMIT_MSG_FILE_NAME: &str = "COMMITMSG_EDITOR";
|
||||||
|
//TODO: use a tmpfile here
|
||||||
let mut config_path: PathBuf = get_app_config_path()?;
|
let mut config_path: PathBuf = get_app_config_path()?;
|
||||||
config_path.push(COMMIT_MSG_FILE_NAME);
|
config_path.push(COMMIT_MSG_FILE_NAME);
|
||||||
|
|
||||||
{
|
{
|
||||||
//TODO: use a tmpfile here
|
|
||||||
let mut file = File::create(&config_path)?;
|
let mut file = File::create(&config_path)?;
|
||||||
file.write_fmt(format_args!(
|
file.write_fmt(format_args!(
|
||||||
"{}\n",
|
"{}\n",
|
||||||
|
|
@ -157,16 +153,10 @@ impl CommitComponent {
|
||||||
file.write_all(strings::COMMIT_EDITOR_MSG.as_bytes())?;
|
file.write_all(strings::COMMIT_EDITOR_MSG.as_bytes())?;
|
||||||
}
|
}
|
||||||
|
|
||||||
io::stdout().execute(LeaveAlternateScreen)?;
|
ExternalEditorComponent::open_file_in_editor(&config_path)?;
|
||||||
defer! {
|
|
||||||
io::stdout().execute(EnterAlternateScreen).expect("reset terminal");
|
|
||||||
}
|
|
||||||
|
|
||||||
crate::open_file_in_editor(&config_path)?;
|
|
||||||
|
|
||||||
let mut message = String::new();
|
let mut message = String::new();
|
||||||
|
|
||||||
//TODO: see above
|
|
||||||
let mut file = File::open(&config_path)?;
|
let mut file = File::open(&config_path)?;
|
||||||
file.read_to_string(&mut message)?;
|
file.read_to_string(&mut message)?;
|
||||||
drop(file);
|
drop(file);
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,14 @@ use crate::{
|
||||||
strings,
|
strings,
|
||||||
ui::{self, style::SharedTheme},
|
ui::{self, style::SharedTheme},
|
||||||
};
|
};
|
||||||
use anyhow::Result;
|
use anyhow::{anyhow, Result};
|
||||||
use crossterm::event::Event;
|
use crossterm::{
|
||||||
|
event::Event,
|
||||||
|
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
|
||||||
|
ExecutableCommand,
|
||||||
|
};
|
||||||
|
use scopeguard::defer;
|
||||||
|
use std::{env, io, path::Path, process::Command};
|
||||||
use tui::{
|
use tui::{
|
||||||
backend::Backend,
|
backend::Backend,
|
||||||
layout::Rect,
|
layout::Rect,
|
||||||
|
|
@ -29,6 +35,34 @@ impl ExternalEditorComponent {
|
||||||
theme,
|
theme,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
pub fn open_file_in_editor(path: &Path) -> Result<()> {
|
||||||
|
io::stdout().execute(LeaveAlternateScreen)?;
|
||||||
|
defer! {
|
||||||
|
io::stdout().execute(EnterAlternateScreen).expect("reset terminal");
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut editor = env::var("GIT_EDITOR")
|
||||||
|
.ok()
|
||||||
|
.or_else(|| env::var("VISUAL").ok())
|
||||||
|
.or_else(|| env::var("EDITOR").ok())
|
||||||
|
.unwrap_or_else(|| String::from("vi"));
|
||||||
|
editor.push_str(&format!(" {}", path.to_string_lossy()));
|
||||||
|
|
||||||
|
let mut editor = editor.split_whitespace();
|
||||||
|
|
||||||
|
let command = editor.next().ok_or_else(|| {
|
||||||
|
anyhow!("unable to read editor command")
|
||||||
|
})?;
|
||||||
|
|
||||||
|
Command::new(command)
|
||||||
|
.args(editor)
|
||||||
|
.status()
|
||||||
|
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DrawableComponent for ExternalEditorComponent {
|
impl DrawableComponent for ExternalEditorComponent {
|
||||||
|
|
|
||||||
25
src/main.rs
25
src/main.rs
|
|
@ -43,13 +43,12 @@ use scopeguard::defer;
|
||||||
use scopetime::scope_time;
|
use scopetime::scope_time;
|
||||||
use simplelog::{Config, LevelFilter, WriteLogger};
|
use simplelog::{Config, LevelFilter, WriteLogger};
|
||||||
use spinner::Spinner;
|
use spinner::Spinner;
|
||||||
use std::process::Command;
|
|
||||||
use std::{
|
use std::{
|
||||||
env, fs,
|
env, fs,
|
||||||
fs::File,
|
fs::File,
|
||||||
io::{self, Write},
|
io::{self, Write},
|
||||||
panic,
|
panic,
|
||||||
path::{Path, PathBuf},
|
path::PathBuf,
|
||||||
process,
|
process,
|
||||||
time::{Duration, Instant},
|
time::{Duration, Instant},
|
||||||
};
|
};
|
||||||
|
|
@ -242,28 +241,6 @@ fn migrate_config() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn open_file_in_editor(path: &Path) -> Result<()> {
|
|
||||||
let mut editor = env::var("GIT_EDITOR")
|
|
||||||
.ok()
|
|
||||||
.or_else(|| env::var("VISUAL").ok())
|
|
||||||
.or_else(|| env::var("EDITOR").ok())
|
|
||||||
.unwrap_or_else(|| String::from("vi"));
|
|
||||||
editor.push_str(&format!(" {}", path.to_string_lossy()));
|
|
||||||
|
|
||||||
let mut editor = editor.split_whitespace();
|
|
||||||
|
|
||||||
let command = editor
|
|
||||||
.next()
|
|
||||||
.ok_or_else(|| anyhow!("unable to read editor command"))?;
|
|
||||||
|
|
||||||
Command::new(command)
|
|
||||||
.args(editor)
|
|
||||||
.status()
|
|
||||||
.map_err(|e| anyhow!("\"{}\": {}", command, e))?;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_app_cache_path() -> Result<PathBuf> {
|
fn get_app_cache_path() -> Result<PathBuf> {
|
||||||
let mut path = dirs::cache_dir()
|
let mut path = dirs::cache_dir()
|
||||||
.ok_or_else(|| anyhow!("failed to find os cache dir."))?;
|
.ok_or_else(|| anyhow!("failed to find os cache dir."))?;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue