Support exposing some env vars as part of the mode information

This should implement what #5131 proposed in a different way.

Closes #5131
This commit is contained in:
Maxime Coste 2024-03-31 21:38:51 +11:00
parent 7d0f25a99c
commit 303d879785
3 changed files with 27 additions and 18 deletions

View File

@ -165,11 +165,12 @@ DisplayLine Client::generate_mode_line() const
DisplayLine modeline;
try
{
ModeInfo mode_info = context().client().input_handler().mode_info();
const String& modelinefmt = context().options()["modelinefmt"].get<String>();
HashMap<String, DisplayLine> atoms{{ "mode_info", context().client().input_handler().mode_line() },
HashMap<String, DisplayLine> atoms{{ "mode_info", mode_info.display_line},
{ "context_info", {generate_context_info(context()),
context().faces()["Information"]}}};
auto expanded = expand(modelinefmt, context(), ShellContext{},
auto expanded = expand(modelinefmt, context(), {{}, mode_info.env_vars},
[](String s) { return escape(s, '{', '\\'); });
modeline = parse_display_line(expanded, context().faces(), atoms);
}

View File

@ -42,7 +42,7 @@ public:
bool enabled() const { return &m_input_handler.current_mode() == this; }
Context& context() const { return m_input_handler.context(); }
virtual DisplayLine mode_line() const = 0;
virtual ModeInfo mode_info() const = 0;
virtual KeymapMode keymap_mode() const = 0;
@ -370,7 +370,7 @@ public:
m_idle_timer.set_next_date(Clock::now() + get_idle_timeout(context()));
}
DisplayLine mode_line() const override
ModeInfo mode_info() const override
{
AtomList atoms;
auto num_sel = context().selections().size();
@ -390,7 +390,7 @@ public:
atoms.emplace_back(" reg=", context().faces()["StatusLineInfo"]);
atoms.emplace_back(StringView(m_params.reg).str(), context().faces()["StatusLineValue"]);
}
return atoms;
return {atoms, {{"count", to_string(m_params.count)}, {"register", StringView(m_params.reg).str()}}};
}
KeymapMode keymap_mode() const override { return KeymapMode::Normal; }
@ -951,9 +951,9 @@ public:
}
}
DisplayLine mode_line() const override
ModeInfo mode_info() const override
{
return { "prompt", context().faces()["StatusLineMode"] };
return {{ "prompt", context().faces()["StatusLineMode"] }, {}};
}
KeymapMode keymap_mode() const override { return KeymapMode::Prompt; }
@ -1126,9 +1126,9 @@ public:
m_callback(key, context());
}
DisplayLine mode_line() const override
ModeInfo mode_info() const override
{
return { "enter key", context().faces()["StatusLineMode"] };
return {{ "enter key", context().faces()["StatusLineMode"] }, {}};
}
KeymapMode keymap_mode() const override { return m_keymap_mode; }
@ -1376,15 +1376,16 @@ public:
m_idle_timer.set_next_date(Clock::now() + get_idle_timeout(context()));
}
DisplayLine mode_line() const override
ModeInfo mode_info() const override
{
auto num_sel = context().selections().size();
auto main_index = context().selections().main_index();
return {AtomList{ { "insert", context().faces()["StatusLineMode"] },
return {{AtomList{ { "insert", context().faces()["StatusLineMode"] },
{ " ", context().faces()["StatusLine"] },
{ num_sel == 1 ? format("{} sel", num_sel)
: format("{} sels ({})", num_sel, main_index + 1),
context().faces()["StatusLineInfo"] } }};
context().faces()["StatusLineInfo"] } }},
{}};
}
KeymapMode keymap_mode() const override { return KeymapMode::Insert; }
@ -1707,9 +1708,9 @@ void InputHandler::stop_recording()
m_recording_level = -1;
}
DisplayLine InputHandler::mode_line() const
ModeInfo InputHandler::mode_info() const
{
return current_mode().mode_line();
return current_mode().mode_info();
}
std::pair<CursorMode, DisplayCoord> InputHandler::get_cursor_info() const

View File

@ -4,6 +4,7 @@
#include "completion.hh"
#include "constexpr_utils.hh"
#include "context.hh"
#include "env_vars.hh"
#include "face.hh"
#include "normal.hh"
#include "keys.hh"
@ -52,6 +53,12 @@ enum class InsertMode : unsigned
OpenLineAbove
};
struct ModeInfo
{
DisplayLine display_line;
EnvVarMap env_vars;
};
class InputHandler : public SafeCountable
{
public:
@ -97,7 +104,7 @@ public:
Context& context() { return m_context; }
const Context& context() const { return m_context; }
DisplayLine mode_line() const;
ModeInfo mode_info() const;
std::pair<CursorMode, DisplayCoord> get_cursor_info() const;