From 4cb5927e7a4741e4d0533c13cf716f2191fba305 Mon Sep 17 00:00:00 2001 From: David Adam Date: Wed, 12 Feb 2025 13:36:58 +0800 Subject: [PATCH] rename curses module to terminal There's no actual use of curses here, just terminfo. --- src/builtins/set_color.rs | 6 +++--- src/env_dispatch.rs | 26 +++++++++++++------------- src/input.rs | 6 +++--- src/lib.rs | 2 +- src/output.rs | 8 ++++---- src/proc.rs | 2 +- src/screen.rs | 4 ++-- src/{curses.rs => terminal.rs} | 10 ++++------ 8 files changed, 31 insertions(+), 33 deletions(-) rename src/{curses.rs => terminal.rs} (98%) diff --git a/src/builtins/set_color.rs b/src/builtins/set_color.rs index 58727d03c..e4300a54e 100644 --- a/src/builtins/set_color.rs +++ b/src/builtins/set_color.rs @@ -3,8 +3,8 @@ use super::prelude::*; use crate::color::RgbColor; use crate::common::str2wcstring; -use crate::curses::{self, Term}; use crate::output::{self, Outputter}; +use crate::terminal::{self, Term}; #[allow(clippy::too_many_arguments)] fn print_modifiers( @@ -76,7 +76,7 @@ fn print_colors( &named_colors }; - let term = curses::term(); + let term = terminal::term(); for color_name in args { if streams.out_is_terminal() { if let Some(term) = term.as_ref() { @@ -217,7 +217,7 @@ pub fn set_color(parser: &Parser, streams: &mut IoStreams, argv: &mut [&wstr]) - // Test if we have at least basic support for setting fonts, colors and related bits - otherwise // just give up... - let Some(term) = curses::term() else { + let Some(term) = terminal::term() else { return STATUS_CMD_ERROR; }; let Some(exit_attribute_mode) = &term.exit_attribute_mode else { diff --git a/src/env_dispatch.rs b/src/env_dispatch.rs index 255f9554f..0977dab98 100644 --- a/src/env_dispatch.rs +++ b/src/env_dispatch.rs @@ -1,6 +1,5 @@ use crate::common::ToCString; use crate::complete::complete_invalidate_path; -use crate::curses::{self, Term}; use crate::env::{setenv_lock, unsetenv_lock, EnvMode, EnvStack, Environment}; use crate::env::{DEFAULT_READ_BYTE_LIMIT, READ_BYTE_LIMIT, TERM_HAS_XN}; use crate::flog::FLOG; @@ -14,6 +13,7 @@ }; use crate::screen::screen_set_midnight_commander_hack; use crate::screen::LAYOUT_CACHE_SHARED; +use crate::terminal::{self, Term}; use crate::wchar::prelude::*; use crate::wutil::fish_wcstoi; use std::borrow::Cow; @@ -50,7 +50,7 @@ } for name in CURSES_VARIABLES { - table.add_anon(name, handle_curses_change); + table.add_anon(name, handle_term_change); } table.add(L!("TZ"), handle_tz_change); @@ -299,9 +299,9 @@ fn handle_locale_change(vars: &EnvStack) { guess_emoji_width(vars); } -fn handle_curses_change(vars: &EnvStack) { +fn handle_term_change(vars: &EnvStack) { guess_emoji_width(vars); - init_curses(vars); + init_terminal(vars); } fn handle_fish_use_posix_spawn_change(vars: &EnvStack) { @@ -361,7 +361,7 @@ pub fn env_dispatch_init(vars: &EnvStack) { /// Runs the subset of dispatch functions that need to be called at startup. fn run_inits(vars: &EnvStack) { init_locale(vars); - init_curses(vars); + init_terminal(vars); guess_emoji_width(vars); update_wait_on_escape_ms(vars); update_wait_on_sequence_key_ms(vars); @@ -379,7 +379,7 @@ fn update_fish_color_support(vars: &EnvStack) { .get(L!("TERM")) .map(|v| v.as_string()) .unwrap_or_else(WString::new); - let max_colors = curses::term().and_then(|term| term.max_colors); + let max_colors = terminal::term().and_then(|term| term.max_colors); let mut supports_256color = false; let mut supports_24bit = false; @@ -494,7 +494,7 @@ fn update_fish_color_support(vars: &EnvStack) { crate::output::set_color_support(color_support); } -/// Apply any platform- or environment-specific hacks to our curses [`Term`] instance. +/// Apply any platform- or environment-specific hacks to our terminfo [`Term`] instance. fn apply_term_hacks(vars: &EnvStack, term: &mut Term) { if cfg!(target_os = "macos") { // Hack in missing italics and dim capabilities omitted from macOS xterm-256color terminfo. @@ -535,8 +535,8 @@ fn apply_non_term_hacks(vars: &EnvStack) { } } -// Initialize the curses subsystem -fn init_curses(vars: &EnvStack) { +// Initialize the terminal subsystem +fn init_terminal(vars: &EnvStack) { for var_name in CURSES_VARIABLES { if let Some(value) = vars .getf_unless_empty(var_name, EnvMode::EXPORT) @@ -550,7 +550,7 @@ fn init_curses(vars: &EnvStack) { } } - if curses::setup(None, |term| apply_term_hacks(vars, term)).is_none() { + if terminal::setup(None, |term| apply_term_hacks(vars, term)).is_none() { if is_interactive_session() { let term = vars.get_unless_empty(L!("TERM")).map(|v| v.as_string()); // We do not warn for xterm-256color at all, we know that one. @@ -569,14 +569,14 @@ fn init_curses(vars: &EnvStack) { } } - curses::setup_fallback_term(); + terminal::setup_fallback_term(); } - // Configure hacks that apply regardless of whether we successfully init curses or not. + // Configure hacks that apply regardless of whether we successfully init apply_non_term_hacks(vars); // Store some global variables that reflect the term's capabilities - if let Some(term) = curses::term() { + if let Some(term) = terminal::term() { TERM_HAS_XN.store(term.eat_newline_glitch, Ordering::Relaxed); } diff --git a/src/input.rs b/src/input.rs index 38636f09e..5b22b8e0a 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,5 +1,4 @@ use crate::common::{escape, get_by_sorted_name, str2wcstring, Named}; -use crate::curses; use crate::env::Environment; use crate::event; use crate::flog::FLOG; @@ -18,6 +17,7 @@ reader_reading_interrupted, reader_reset_interrupted, reader_schedule_prompt_repaint, Reader, }; use crate::signal::signal_clear_cancel; +use crate::terminal; use crate::threads::{assert_is_main_thread, iothread_service_main}; use crate::wchar::prelude::*; use once_cell::sync::{Lazy, OnceCell}; @@ -1022,8 +1022,8 @@ pub fn get<'a>( /// Create a list of terminfo mappings. fn create_input_terminfo() -> Box<[TerminfoMapping]> { - let Some(term) = curses::term() else { - // setupterm() failed so we can't reference any key definitions. + let Some(term) = terminal::term() else { + // loading terminfo failed so we can't reference any key definitions. return Box::new([]); }; diff --git a/src/lib.rs b/src/lib.rs index bbf688c32..09252c575 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,7 +34,6 @@ pub mod builtins; pub mod color; pub mod complete; -pub mod curses; pub mod editable_line; pub mod env; pub mod env_dispatch; @@ -84,6 +83,7 @@ pub mod redirection; pub mod screen; pub mod signal; +pub mod terminal; pub mod termsize; pub mod threads; pub mod timer; diff --git a/src/output.rs b/src/output.rs index c5d6421f0..27a4f0072 100644 --- a/src/output.rs +++ b/src/output.rs @@ -1,8 +1,8 @@ // Generic output functions. use crate::color::RgbColor; use crate::common::{self, wcs2string_appending}; -use crate::curses::{self, tparm1, Term}; use crate::env::EnvVar; +use crate::terminal::{self, tparm1, Term}; use crate::threads::MainThread; use crate::wchar::prelude::*; use bitflags::bitflags; @@ -164,7 +164,7 @@ fn maybe_flush(&mut self) { /// Unconditionally write the color string to the output. /// Exported for builtin_set_color's usage only. pub fn write_color(&mut self, color: RgbColor, is_fg: bool) -> bool { - let Some(term) = curses::term() else { + let Some(term) = terminal::term() else { return false; }; let term: &Term = &term; @@ -215,7 +215,7 @@ pub fn write_color(&mut self, color: RgbColor, is_fg: bool) -> bool { pub fn set_color(&mut self, mut fg: RgbColor, mut bg: RgbColor) { // Test if we have at least basic support for setting fonts, colors and related bits - otherwise // just give up... - let Some(term) = curses::term() else { + let Some(term) = terminal::term() else { return; }; let term: &Term = &term; @@ -351,7 +351,7 @@ pub fn set_color(&mut self, mut fg: RgbColor, mut bg: RgbColor) { if is_dim && !self.was_dim && self.tputs_if_some(enter_dim_mode) { self.was_dim = is_dim; } - // N.B. there is no exit_dim_mode in curses, it's handled by exit_attribute_mode above. + // N.B. there is no exit_dim_mode in terminfo, it's handled by exit_attribute_mode above. if is_reverse && !self.was_reverse { // Some terms do not have a reverse mode set, so standout mode is a fallback. diff --git a/src/proc.rs b/src/proc.rs index 3c59ad18b..e2911d340 100644 --- a/src/proc.rs +++ b/src/proc.rs @@ -7,7 +7,6 @@ charptr2wcstring, escape, is_windows_subsystem_for_linux, redirect_tty_output, scoped_push_replacer, timef, Timepoint, WSL, }; -use crate::curses::term; use crate::env::Statuses; use crate::event::{self, Event}; use crate::flog::{FLOG, FLOGF}; @@ -19,6 +18,7 @@ use crate::reader::{fish_is_unwinding_for_exit, reader_schedule_prompt_repaint}; use crate::redirection::RedirectionSpecList; use crate::signal::{signal_set_handlers_once, Signal}; +use crate::terminal::term; use crate::threads::MainThread; use crate::topic_monitor::{topic_monitor_principal, GenerationsList, Topic}; use crate::wait_handle::{InternalJobId, WaitHandle, WaitHandleRef, WaitHandleStore}; diff --git a/src/screen.rs b/src/screen.rs index 033a157e1..2f368cee2 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -28,7 +28,6 @@ has_working_tty_timestamps, shell_modes, str2wcstring, wcs2string, write_loop, ScopeGuard, ScopeGuarding, }; -use crate::curses::{term, tparm1}; use crate::env::{Environment, TERM_HAS_XN}; use crate::fallback::fish_wcwidth; use crate::flog::FLOGF; @@ -37,6 +36,7 @@ use crate::global_safety::RelaxedAtomicBool; use crate::highlight::{HighlightColorResolver, HighlightSpec}; use crate::output::Outputter; +use crate::terminal::{term, tparm1}; use crate::termsize::{termsize_last, Termsize}; use crate::wchar::prelude::*; use crate::wcstringutil::string_prefixes_string; @@ -1409,7 +1409,7 @@ pub struct LayoutCache { } // Singleton of the cached escape sequences seen in prompts and similar strings. -// Note this is deliberately exported so that init_curses can clear it. +// Note this is deliberately exported so that init_terminal can clear it. pub static LAYOUT_CACHE_SHARED: Mutex = Mutex::new(LayoutCache::new()); impl LayoutCache { diff --git a/src/curses.rs b/src/terminal.rs similarity index 98% rename from src/curses.rs rename to src/terminal.rs index 30ff53029..197cfc801 100644 --- a/src/curses.rs +++ b/src/terminal.rs @@ -25,7 +25,7 @@ pub static TERM: Mutex>> = Mutex::new(None); /// Returns a reference to the global [`Term`] singleton or `None` if not preceded by a successful -/// call to [`curses::setup()`]. +/// call to [`terminal::setup()`]. pub fn term() -> Option> { TERM.lock() .expect("Mutex poisoned!") @@ -33,7 +33,7 @@ pub fn term() -> Option> { .map(Arc::clone) } -/// The safe wrapper around curses functionality, initialized by a successful call to [`setup()`] +/// The safe wrapper around terminfo functionality, initialized by a successful call to [`setup()`] /// and obtained thereafter by calls to [`term()`]. #[allow(dead_code)] #[derive(Default)] @@ -183,7 +183,7 @@ pub struct Term { } impl Term { - /// Initialize a new `Term` instance, prepopulating the values of all the curses string + /// Initialize a new `Term` instance, prepopulating the values of all the terminfo string /// capabilities we care about in the process. fn new(db: terminfo::Database) -> Self { Term { @@ -337,13 +337,11 @@ fn new(db: terminfo::Database) -> Self { /// The `configure` parameter may be set to a callback that takes an `&mut Term` reference to /// override any capabilities before the `Term` is permanently made immutable. /// -/// Any existing references from `curses::term()` will be invalidated by this call! +/// Any existing references from `terminal::term()` will be invalidated by this call! pub fn setup(term: Option<&str>, configure: F) -> Option> where F: Fn(&mut Term), { - // For now, use the same TERM lock when using `cur_term` to prevent any race conditions in - // curses itself. We might split this to another lock in the future. let mut global_term = TERM.lock().expect("Mutex poisoned!"); let res = if let Some(term) = term {