Move parser status vars to their own struct

Instead of using an enum + array, just use a struct and drop the getter and
setter methods from `parser_t`.
This commit is contained in:
Mahmoud Al-Qudsi 2022-10-26 12:12:04 -05:00
parent 125bcb8289
commit 7133285c88
4 changed files with 19 additions and 24 deletions

View File

@ -110,11 +110,11 @@ maybe_t<int> builtin_fg(parser_t &parser, io_streams_t &streams, const wchar_t *
wcstring ft = tok_command(job->command());
if (!ft.empty()) {
// Provide value for `status current-command`
parser.set_status_var(parser_status_var_t::current_command, ft);
parser.libdata().status_vars.command = ft;
// Also provide a value for the deprecated fish 2.0 $_ variable
parser.set_var_and_fire(L"_", ENV_EXPORT, std::move(ft));
// Provide value for `status current-commandline`
parser.set_status_var(parser_status_var_t::current_commandline, job->command());
parser.libdata().status_vars.commandline = job->command();
}
reader_write_title(job->command(), parser);

View File

@ -451,7 +451,7 @@ maybe_t<int> builtin_status(parser_t &parser, io_streams_t &streams, const wchar
}
case STATUS_CURRENT_CMD: {
CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd)
const auto &var = parser.get_status_var(parser_status_var_t::current_command);
const auto &var = parser.libdata().status_vars.command;
if (!var.empty()) {
streams.out.append(var);
streams.out.push_back(L'\n');
@ -463,7 +463,7 @@ maybe_t<int> builtin_status(parser_t &parser, io_streams_t &streams, const wchar
}
case STATUS_CURRENT_COMMANDLINE: {
CHECK_FOR_UNEXPECTED_STATUS_ARGS(opts.status_cmd)
const auto &var = parser.get_status_var(parser_status_var_t::current_commandline);
const auto &var = parser.libdata().status_vars.commandline;
streams.out.append(var);
streams.out.push_back(L'\n');
break;

View File

@ -221,6 +221,15 @@ struct library_data_t {
/// A file descriptor holding the current working directory, for use in openat().
/// This is never null and never invalid.
std::shared_ptr<const autoclose_fd_t> cwd_fd{};
/// Status variables set by the main thread as jobs are parsed and read by various consumers.
struct {
/// Used to get the head of the current job (not the current command, at least for now)
/// for `status current-command`.
wcstring command;
/// Used to get the full text of the current job for `status current-commandline`.
wcstring commandline;
} status_vars;
};
/// The result of parser_t::eval family.
@ -270,9 +279,6 @@ class parser_t : public std::enable_shared_from_this<parser_t> {
/// top down using range-based for loops.
std::deque<block_t> block_list;
/// Variables set by the single-threaded parser or reader and queryable by any consumer.
std::array<wcstring, (int) parser_status_var_t::count_> status_vars_;
/// The 'depth' of the fish call stack.
int eval_level = -1;
@ -398,17 +404,6 @@ class parser_t : public std::enable_shared_from_this<parser_t> {
statuses_t get_last_statuses() const { return vars().get_last_statuses(); }
void set_last_statuses(statuses_t s) { vars().set_last_statuses(std::move(s)); }
/// Get a parser status variable
const wcstring& get_status_var(parser_status_var_t var) const {
return status_vars_.at((int) var);
}
/// Set a parser status variable
void set_status_var(parser_status_var_t var, wcstring val) {
ASSERT_IS_MAIN_THREAD();
status_vars_[(int) var] = std::move(val);
}
/// Cover of vars().set(), which also fires any returned event handlers.
/// \return a value like ENV_OK.
int set_var_and_fire(const wcstring &key, env_mode_flags_t mode, wcstring val);

View File

@ -2495,7 +2495,7 @@ static void reader_interactive_init(parser_t &parser) {
termsize_container_t::shared().invalidate_tty();
// Provide value for `status current-command`
parser.set_status_var(parser_status_var_t::current_command, L"fish");
parser.libdata().status_vars.command = L"fish";
// Also provide a value for the deprecated fish 2.0 $_ variable
parser.vars().set_one(L"_", ENV_GLOBAL, L"fish");
}
@ -2632,8 +2632,8 @@ static eval_res_t reader_run_command(parser_t &parser, const wcstring &cmd) {
// Provide values for `status current-command` and `status current-commandline`
if (!ft.empty()) {
parser.set_status_var(parser_status_var_t::current_command, ft);
parser.set_status_var(parser_status_var_t::current_commandline, cmd);
parser.libdata().status_vars.command = ft;
parser.libdata().status_vars.commandline = cmd;
// Also provide a value for the deprecated fish 2.0 $_ variable
parser.vars().set_one(L"_", ENV_GLOBAL, ft);
}
@ -2647,7 +2647,7 @@ static eval_res_t reader_run_command(parser_t &parser, const wcstring &cmd) {
auto eval_res = parser.eval(cmd, io_chain_t{});
job_reap(parser, true);
// update the execution duration iff a command is requested for execution
// Update the execution duration iff a command is requested for execution
// issue - #4926
if (!ft.empty()) {
timepoint_t time_after = timef();
@ -2659,11 +2659,11 @@ static eval_res_t reader_run_command(parser_t &parser, const wcstring &cmd) {
term_steal();
// Provide value for `status current-command`
parser.set_status_var(parser_status_var_t::current_command, program_name);
parser.libdata().status_vars.command = program_name;
// Also provide a value for the deprecated fish 2.0 $_ variable
parser.vars().set_one(L"_", ENV_GLOBAL, program_name);
// Provide value for `status current-commandline`
parser.set_status_var(parser_status_var_t::current_commandline, L"");
parser.libdata().status_vars.commandline = L"";
if (have_proc_stat()) {
proc_update_jiffies(parser);