summaryrefslogtreecommitdiffstats
path: root/alacritty_terminal
diff options
context:
space:
mode:
authorDavid Hewitt <1939362+davidhewitt@users.noreply.github.com>2020-01-02 11:49:27 +0000
committerChristian Duerr <contact@christianduerr.com>2020-01-02 11:49:27 +0000
commitf632919134414e31ffd3ae9b5732d673deb0adf5 (patch)
tree59e74ceabba1d08a5d5966502dda113b11143fc2 /alacritty_terminal
parent77acb26d7328f678b26c8aff2ee5706d78949c44 (diff)
Clean up Windows PTY string handling
Removes widestring and dunce dependencies, reduces some code duplication and corrects a few typos.
Diffstat (limited to 'alacritty_terminal')
-rw-r--r--alacritty_terminal/Cargo.toml2
-rw-r--r--alacritty_terminal/src/config/mod.rs6
-rw-r--r--alacritty_terminal/src/panic.rs12
-rw-r--r--alacritty_terminal/src/tty/mod.rs2
-rw-r--r--alacritty_terminal/src/tty/unix.rs2
-rw-r--r--alacritty_terminal/src/tty/windows/conpty.rs27
-rw-r--r--alacritty_terminal/src/tty/windows/mod.rs21
-rw-r--r--alacritty_terminal/src/tty/windows/winpty.rs20
8 files changed, 41 insertions, 51 deletions
diff --git a/alacritty_terminal/Cargo.toml b/alacritty_terminal/Cargo.toml
index 385c3cbd..947d94e4 100644
--- a/alacritty_terminal/Cargo.toml
+++ b/alacritty_terminal/Cargo.toml
@@ -34,12 +34,10 @@ signal-hook = { version = "0.1", features = ["mio-support"] }
winpty = { path = "../winpty" }
mio-named-pipes = "0.1"
miow = "0.3"
-dunce = "1.0"
winapi = { version = "0.3.7", features = [
"impl-default", "basetsd", "libloaderapi", "minwindef", "ntdef", "processthreadsapi", "winbase",
"wincon", "wincontypes", "winerror", "winnt", "winuser",
]}
-widestring = "0.4"
mio-anonymous-pipes = "0.1"
[target.'cfg(target_os = "macos")'.dependencies]
diff --git a/alacritty_terminal/src/config/mod.rs b/alacritty_terminal/src/config/mod.rs
index 3c8a85a4..f3257b7b 100644
--- a/alacritty_terminal/src/config/mod.rs
+++ b/alacritty_terminal/src/config/mod.rs
@@ -15,7 +15,7 @@
use std::borrow::Cow;
use std::collections::HashMap;
use std::fmt::Display;
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use log::error;
use serde::{Deserialize, Deserializer};
@@ -209,8 +209,8 @@ impl<T> Config<T> {
}
#[inline]
- pub fn working_directory(&self) -> &Option<PathBuf> {
- &self.working_directory
+ pub fn working_directory(&self) -> Option<&Path> {
+ self.working_directory.as_ref().map(|path_buf| path_buf.as_path())
}
#[inline]
diff --git a/alacritty_terminal/src/panic.rs b/alacritty_terminal/src/panic.rs
index 4d3524ed..7f19c59a 100644
--- a/alacritty_terminal/src/panic.rs
+++ b/alacritty_terminal/src/panic.rs
@@ -13,6 +13,8 @@
// limitations under the License.
//
//! ANSI Terminal Stream Parsing
+#[cfg(windows)]
+use crate::tty::windows::win32_string;
// Use the default behavior of the other platforms.
#[cfg(not(windows))]
@@ -41,13 +43,3 @@ pub fn attach_handler() {
}
}));
}
-
-// Converts the string slice into a Windows-standard representation for "W"-
-// suffixed function variants, which accept UTF-16 encoded string values.
-#[cfg(windows)]
-fn win32_string(value: &str) -> Vec<u16> {
- use std::ffi::OsStr;
- use std::iter::once;
- use std::os::windows::ffi::OsStrExt;
- OsStr::new(value).encode_wide().chain(once(0)).collect()
-}
diff --git a/alacritty_terminal/src/tty/mod.rs b/alacritty_terminal/src/tty/mod.rs
index 40d019f5..425ec4b0 100644
--- a/alacritty_terminal/src/tty/mod.rs
+++ b/alacritty_terminal/src/tty/mod.rs
@@ -26,7 +26,7 @@ mod unix;
pub use self::unix::*;
#[cfg(windows)]
-mod windows;
+pub mod windows;
#[cfg(windows)]
pub use self::windows::*;
diff --git a/alacritty_terminal/src/tty/unix.rs b/alacritty_terminal/src/tty/unix.rs
index 9419ead0..ab34d33a 100644
--- a/alacritty_terminal/src/tty/unix.rs
+++ b/alacritty_terminal/src/tty/unix.rs
@@ -209,7 +209,7 @@ pub fn new<C>(config: &Config<C>, size: &SizeInfo, window_id: Option<usize>) ->
// Handle set working directory option
if let Some(ref dir) = config.working_directory() {
- builder.current_dir(dir.as_path());
+ builder.current_dir(dir);
}
// Prepare signal handling before spawning child
diff --git a/alacritty_terminal/src/tty/windows/conpty.rs b/alacritty_terminal/src/tty/windows/conpty.rs
index 44b3662f..28cdf4c4 100644
--- a/alacritty_terminal/src/tty/windows/conpty.rs
+++ b/alacritty_terminal/src/tty/windows/conpty.rs
@@ -18,10 +18,8 @@ use std::mem;
use std::os::windows::io::IntoRawHandle;
use std::ptr;
-use dunce::canonicalize;
use mio_anonymous_pipes::{EventedAnonRead, EventedAnonWrite};
use miow;
-use widestring::U16CString;
use winapi::shared::basetsd::{PSIZE_T, SIZE_T};
use winapi::shared::minwindef::{BYTE, DWORD};
use winapi::shared::ntdef::{HANDLE, HRESULT, LPWSTR};
@@ -34,11 +32,11 @@ use winapi::um::processthreadsapi::{
use winapi::um::winbase::{EXTENDED_STARTUPINFO_PRESENT, STARTF_USESTDHANDLES, STARTUPINFOEXW};
use winapi::um::wincontypes::{COORD, HPCON};
-use crate::config::{Config, Shell};
+use crate::config::Config;
use crate::event::OnResize;
use crate::term::SizeInfo;
use crate::tty::windows::child::ChildExitWatcher;
-use crate::tty::windows::Pty;
+use crate::tty::windows::{cmdline, win32_string, Pty};
// TODO: Replace with winapi's implementation. This cannot be
// done until a safety net is in place for versions of Windows
@@ -141,8 +139,7 @@ pub fn new<C>(config: &Config<C>, size: &SizeInfo, _window_id: Option<usize>) ->
let mut startup_info_ex: STARTUPINFOEXW = Default::default();
- let title = config.window.title.clone();
- let title = U16CString::from_str(title).unwrap();
+ let title = win32_string(&config.window.title);
startup_info_ex.StartupInfo.lpTitle = title.as_ptr() as LPWSTR;
startup_info_ex.StartupInfo.cb = mem::size_of::<STARTUPINFOEXW>() as u32;
@@ -205,19 +202,11 @@ pub fn new<C>(config: &Config<C>, size: &SizeInfo, _window_id: Option<usize>) ->
}
}
- // Get process commandline
- let default_shell = &Shell::new("powershell");
- let shell = config.shell.as_ref().unwrap_or(default_shell);
- let mut cmdline = shell.args.clone();
- cmdline.insert(0, shell.program.to_string());
-
- // Warning, here be borrow hell
- let cwd = config.working_directory().as_ref().map(|dir| canonicalize(dir).unwrap());
- let cwd = cwd.as_ref().map(|dir| dir.to_str().unwrap());
-
- // Create the client application, using startup info containing ConPTY info
- let cmdline = U16CString::from_str(&cmdline.join(" ")).unwrap();
- let cwd = cwd.map(|s| U16CString::from_str(&s).unwrap());
+ let cmdline = win32_string(&cmdline(&config));
+ let cwd = config
+ .working_directory()
+ .map(|dir| dir.canonicalize().unwrap())
+ .map(|path| win32_string(&path));
let mut proc_info: PROCESS_INFORMATION = Default::default();
unsafe {
diff --git a/alacritty_terminal/src/tty/windows/mod.rs b/alacritty_terminal/src/tty/windows/mod.rs
index f5e0e61f..03e2a3cd 100644
--- a/alacritty_terminal/src/tty/windows/mod.rs
+++ b/alacritty_terminal/src/tty/windows/mod.rs
@@ -12,7 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+use std::ffi::OsStr;
use std::io::{self, Read, Write};
+use std::iter::once;
+use std::os::windows::ffi::OsStrExt;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::mpsc::TryRecvError;
@@ -22,7 +25,7 @@ use mio_named_pipes::NamedPipe;
use log::info;
-use crate::config::Config;
+use crate::config::{Config, Shell};
use crate::event::OnResize;
use crate::term::SizeInfo;
use crate::tty::windows::child::ChildExitWatcher;
@@ -298,3 +301,19 @@ impl OnResize for Pty {
}
}
}
+
+fn cmdline<C>(config: &Config<C>) -> String {
+ let default_shell = Shell::new("powershell");
+ let shell = config.shell.as_ref().unwrap_or(&default_shell);
+
+ once(shell.program.as_ref())
+ .chain(shell.args.iter().map(|a| a.as_ref()))
+ .collect::<Vec<_>>()
+ .join(" ")
+}
+
+/// Converts the string slice into a Windows-standard representation for "W"-
+/// suffixed function variants, which accept UTF-16 encoded string values.
+pub fn win32_string<S: AsRef<OsStr> + ?Sized>(value: &S) -> Vec<u16> {
+ OsStr::new(value).encode_wide().chain(once(0)).collect()
+}
diff --git a/alacritty_terminal/src/tty/windows/winpty.rs b/alacritty_terminal/src/tty/windows/winpty.rs
index 0d2421b0..c56ad8f6 100644
--- a/alacritty_terminal/src/tty/windows/winpty.rs
+++ b/alacritty_terminal/src/tty/windows/winpty.rs
@@ -17,17 +17,16 @@ use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::io::{FromRawHandle, IntoRawHandle};
use std::u16;
-use dunce::canonicalize;
use log::info;
use mio_named_pipes::NamedPipe;
use winapi::um::winbase::FILE_FLAG_OVERLAPPED;
use winpty::{Config as WinptyConfig, ConfigFlags, MouseMode, SpawnConfig, SpawnFlags, Winpty};
-use crate::config::{Config, Shell};
+use crate::config::Config;
use crate::event::OnResize;
use crate::term::SizeInfo;
use crate::tty::windows::child::ChildExitWatcher;
-use crate::tty::windows::Pty;
+use crate::tty::windows::{cmdline, Pty};
pub use winpty::Winpty as Agent;
@@ -42,22 +41,15 @@ pub fn new<C>(config: &Config<C>, size: &SizeInfo, _window_id: Option<usize>) ->
let mut agent = Winpty::open(&wconfig).unwrap();
let (conin, conout) = (agent.conin_name(), agent.conout_name());
- // Get process commandline
- let default_shell = &Shell::new("powershell");
- let shell = config.shell.as_ref().unwrap_or(default_shell);
- let mut cmdline = shell.args.clone();
- cmdline.insert(0, shell.program.to_string());
-
- // Warning, here be borrow hell
- let cwd = config.working_directory().as_ref().map(|dir| canonicalize(dir).unwrap());
- let cwd = cwd.as_ref().map(|dir| dir.to_str().unwrap());
+ let cmdline = cmdline(&config);
+ let cwd = config.working_directory().map(|dir| dir.canonicalize().unwrap());
// Spawn process
let spawnconfig = SpawnConfig::new(
SpawnFlags::AUTO_SHUTDOWN | SpawnFlags::EXIT_AFTER_SHUTDOWN,
None, // appname
- Some(&cmdline.join(" ")),
- cwd,
+ Some(&cmdline),
+ cwd.as_ref().map(|p| p.as_ref()),
None, // Env
)
.unwrap();