1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 06:54:45 +03:00

allow setting default values for environment variables

This is useful for setting up a reasonable initial environment.
For example, on Windows you might want to set the `prompt` environment
so that some basic shell integration is enabled; this will cause new
tabs to open with the same cwd as the current tab:

```
set_environment_variables = { "prompt"="$E]7;file://localhost/$P$E\\$P$G" }
```

This setting is intended to apply only to the local domain.

refs: https://github.com/wez/wezterm/issues/146
This commit is contained in:
Wez Furlong 2020-02-09 12:33:57 -08:00
parent 15e875f765
commit 65707aba56
3 changed files with 54 additions and 8 deletions

View File

@ -227,21 +227,51 @@ impl CommandBuilder {
/// adds/replaces the environment that was specified via the
/// `env` methods.
pub(crate) fn environment_block(&self) -> Vec<u16> {
// Holds an entry with its preferred key case; the environment
// has case insensitive variable names on windows, so we need
// to take care to avoid confusing things with conflicting
// entries, and we'd also like to preserve the original case.
struct Entry {
key: OsString,
value: OsString,
}
// Best-effort lowercase transformation of an os string
fn lowerkey(k: &OsStr) -> OsString {
if let Some(s) = k.to_str() {
s.to_lowercase().into()
} else {
k.to_os_string()
}
}
// Use a btreemap for a nicer sorted order if you review the
// environment via `set`.
let mut env_hash = std::collections::BTreeMap::new();
// Take the current environment as the base
let mut env_hash: std::collections::HashMap<_, _> = std::env::vars_os().collect();
for (key, value) in std::env::vars_os() {
env_hash.insert(lowerkey(&key), Entry { key, value });
}
// override with the specified values
for (k, v) in &self.envs {
env_hash.insert(k.to_owned(), v.to_owned());
for (key, value) in &self.envs {
env_hash.insert(
lowerkey(&key),
Entry {
key: key.clone(),
value: value.clone(),
},
);
}
// and now encode it as wide characters
let mut block = vec![];
for (k, v) in env_hash {
block.extend(k.encode_wide());
for entry in env_hash.values() {
block.extend(entry.key.encode_wide());
block.push(b'=' as u16);
block.extend(v.encode_wide());
block.extend(entry.value.encode_wide());
block.push(0);
}
// and a final terminator for CreateProcessW

View File

@ -300,6 +300,12 @@ pub struct Config {
/// as the positional arguments to that command.
pub default_prog: Option<Vec<String>>,
/// Specifies a map of environment variables that should be set
/// when spawning commands in the local domain.
/// This is not used when working with remote domains.
#[serde(default)]
pub set_environment_variables: HashMap<String, String>,
/// Specifies the height of a new window, expressed in character cells.
#[serde(default = "default_initial_rows")]
pub initial_rows: u16,
@ -797,10 +803,17 @@ impl Config {
}
};
cmd.env("TERM", &self.term);
self.apply_cmd_defaults(&mut cmd);
Ok(cmd)
}
pub fn apply_cmd_defaults(&self, cmd: &mut CommandBuilder) {
for (k, v) in &self.set_environment_variables {
cmd.env(k, v);
}
cmd.env("TERM", &self.term);
}
}
fn default_ratelimit_line_prefetches_per_second() -> u32 {

View File

@ -91,7 +91,10 @@ impl Domain for LocalDomain {
) -> Result<Rc<dyn Tab>, Error> {
let config = configuration();
let mut cmd = match command {
Some(c) => c,
Some(mut cmd) => {
config.apply_cmd_defaults(&mut cmd);
cmd
}
None => config.build_prog(None)?,
};
if let Some(dir) = command_dir {