Port env_dispatch dependencies to rust

Either add rust wrappers for C++ functions called via ffi or port some pure code
from C++ to rust to provide support for the upcoming `env_dispatch` rewrite.
This commit is contained in:
Mahmoud Al-Qudsi 2023-05-16 14:46:16 -05:00
parent 8a549cbb15
commit c409b1a89c
9 changed files with 72 additions and 5 deletions

View File

@ -44,12 +44,14 @@ mod locale;
mod nix;
mod null_terminated_array;
mod operation_context;
mod output;
mod parse_constants;
mod parse_tree;
mod parse_util;
mod parser_keywords;
mod path;
mod re;
mod reader;
mod redirection;
mod signal;
mod smoke;

19
fish-rust/src/output.rs Normal file
View File

@ -0,0 +1,19 @@
use bitflags::bitflags;
bitflags! {
pub struct ColorSupport: u8 {
const NONE = 0;
const TERM_256COLOR = 1<<0;
const TERM_24BIT = 1<<1;
}
}
pub fn output_set_color_support(value: ColorSupport) {
extern "C" {
pub fn output_set_color_support(value: libc::c_int);
}
unsafe {
output_set_color_support(value.bits() as i32);
}
}

15
fish-rust/src/reader.rs Normal file
View File

@ -0,0 +1,15 @@
use crate::env::Environment;
use crate::wchar::L;
#[repr(u8)]
pub enum CursorSelectionMode {
Exclusive = 0,
Inclusive = 1,
}
pub fn check_autosuggestion_enabled(vars: &dyn Environment) -> bool {
vars.get(L!("fish_autosuggestion_enabled"))
.map(|v| v.as_string())
.map(|v| v != L!("0"))
.unwrap_or(true)
}

View File

@ -41,9 +41,10 @@ static bool term_supports_color_natively(unsigned int c) {
return static_cast<unsigned>(max_colors) >= c + 1;
}
color_support_t output_get_color_support() { return color_support; }
void output_set_color_support(color_support_t val) { color_support = val; }
extern "C" {
void output_set_color_support(color_support_t val) { color_support = val; }
color_support_t output_get_color_support() { return color_support; }
}
unsigned char index_for_color(rgb_color_t c) {
if (c.is_named() || !(output_get_color_support() & color_support_term256)) {

View File

@ -127,8 +127,10 @@ rgb_color_t parse_color(const env_var_t &var, bool is_background);
/// Sets what colors are supported.
enum { color_support_term256 = 1 << 0, color_support_term24bit = 1 << 1 };
using color_support_t = unsigned int;
color_support_t output_get_color_support();
void output_set_color_support(color_support_t val);
extern "C" {
color_support_t output_get_color_support();
void output_set_color_support(color_support_t val);
}
rgb_color_t best_color(const std::vector<rgb_color_t> &candidates, color_support_t support);

View File

@ -2922,6 +2922,10 @@ void reader_change_cursor_selection_mode(cursor_selection_mode_t selection_mode)
}
}
void reader_change_cursor_selection_mode(uint8_t selection_mode) {
reader_change_cursor_selection_mode((cursor_selection_mode_t) selection_mode);
}
static bool check_autosuggestion_enabled(const env_stack_t &vars) {
if (auto val = vars.get(L"fish_autosuggestion_enabled")) {
return val->as_string() != L"0";
@ -2942,6 +2946,18 @@ void reader_set_autosuggestion_enabled(const env_stack_t &vars) {
}
}
void reader_set_autosuggestion_enabled_ffi(bool enable) {
// We don't need to _change_ if we're not initialized yet.
reader_data_t *data = current_data_or_null();
if (data) {
if (data->conf.autosuggest_ok != enable) {
data->conf.autosuggest_ok = enable;
data->force_exec_prompt_and_repaint = true;
data->inputter.queue_char(readline_cmd_t::repaint);
}
}
}
/// Add a new reader to the reader stack.
/// \return a shared pointer to it.
static std::shared_ptr<reader_data_t> reader_push_ret(parser_t &parser,

View File

@ -168,11 +168,17 @@ enum class cursor_selection_mode_t : uint8_t {
inclusive,
};
#if INCLUDE_RUST_HEADERS
void reader_change_cursor_selection_mode(cursor_selection_mode_t selection_mode);
#else
void reader_change_cursor_selection_mode(uint8_t selection_mode);
#endif
/// Enable or disable autosuggestions based on the associated variable.
void reader_set_autosuggestion_enabled(const env_stack_t &vars);
void reader_set_autosuggestion_enabled_ffi(bool);
/// Write the title to the titlebar. This function is called just before a new application starts
/// executing and just after it finishes.
///

View File

@ -63,6 +63,10 @@ class scoped_buffer_t : noncopyable_t, nonmovable_t {
// Note this is deliberately exported so that init_curses can clear it.
layout_cache_t layout_cache_t::shared;
void screen_clear_layout_cache_ffi() {
layout_cache_t::shared.clear();
}
/// Tests if the specified narrow character sequence is present at the specified position of the
/// specified wide character string. All of \c seq must match, but str may be longer than seq.
static size_t try_sequence(const char *seq, const wchar_t *str) {

View File

@ -244,6 +244,8 @@ class screen_t {
/// Issues an immediate clr_eos.
void screen_force_clear_to_end();
void screen_clear_layout_cache_ffi();
// Information about the layout of a prompt.
struct prompt_layout_t {
std::vector<size_t> line_breaks; // line breaks when rendering the prompt