mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-14 14:03:58 +00:00
Factor out fetching electric variables into a separate function
This factors env_stack_t::get() a little better
This commit is contained in:
parent
f35f2fe110
commit
341799194e
1 changed files with 34 additions and 29 deletions
35
src/env.cpp
35
src/env.cpp
|
@ -836,19 +836,8 @@ std::shared_ptr<const wcstring_list_t> env_var_t::empty_list() {
|
|||
return result;
|
||||
}
|
||||
|
||||
maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
||||
const bool has_scope = mode & (ENV_LOCAL | ENV_GLOBAL | ENV_UNIVERSAL);
|
||||
const bool search_local = !has_scope || (mode & ENV_LOCAL);
|
||||
const bool search_global = !has_scope || (mode & ENV_GLOBAL);
|
||||
const bool search_universal = !has_scope || (mode & ENV_UNIVERSAL);
|
||||
|
||||
const bool search_exported = (mode & ENV_EXPORT) || !(mode & ENV_UNEXPORT);
|
||||
const bool search_unexported = (mode & ENV_UNEXPORT) || !(mode & ENV_EXPORT);
|
||||
|
||||
// Make the assumption that electric keys can't be shadowed elsewhere, since we currently block
|
||||
// that in env_stack_t::set().
|
||||
if (is_electric(key)) {
|
||||
if (!search_global) return none();
|
||||
/// \return a the value of a variable for \p key, which must be electric (computed).
|
||||
static maybe_t<env_var_t> get_electric(const wcstring &key, const environment_t &vars) {
|
||||
if (key == L"history") {
|
||||
// Big hack. We only allow getting the history on the main thread. Note that history_t
|
||||
// may ask for an environment variable, so don't take the lock here (we don't need it).
|
||||
|
@ -858,7 +847,7 @@ maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode)
|
|||
|
||||
history_t *history = reader_get_history();
|
||||
if (!history) {
|
||||
history = &history_t::history_with_name(history_session_id(*this));
|
||||
history = &history_t::history_with_name(history_session_id(vars));
|
||||
}
|
||||
wcstring_list_t result;
|
||||
if (history) history->get_history(result);
|
||||
|
@ -877,7 +866,23 @@ maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode)
|
|||
return env_var_t(L"umask", format_string(L"0%0.3o", get_umask()));
|
||||
}
|
||||
// We should never get here unless the electric var list is out of sync with the above code.
|
||||
DIE("unerecognized electric var name");
|
||||
DIE("unrecognized electric var name");
|
||||
}
|
||||
|
||||
maybe_t<env_var_t> env_stack_t::get(const wcstring &key, env_mode_flags_t mode) const {
|
||||
const bool has_scope = mode & (ENV_LOCAL | ENV_GLOBAL | ENV_UNIVERSAL);
|
||||
const bool search_local = !has_scope || (mode & ENV_LOCAL);
|
||||
const bool search_global = !has_scope || (mode & ENV_GLOBAL);
|
||||
const bool search_universal = !has_scope || (mode & ENV_UNIVERSAL);
|
||||
|
||||
const bool search_exported = (mode & ENV_EXPORT) || !(mode & ENV_UNEXPORT);
|
||||
const bool search_unexported = (mode & ENV_UNEXPORT) || !(mode & ENV_EXPORT);
|
||||
|
||||
// Make the assumption that electric keys can't be shadowed elsewhere, since we currently block
|
||||
// that in env_stack_t::set().
|
||||
if (is_electric(key)) {
|
||||
if (!search_global) return none();
|
||||
return get_electric(key, *this);
|
||||
}
|
||||
|
||||
if (search_local || search_global) {
|
||||
|
|
Loading…
Reference in a new issue