From 7840334908611dc6b0e37bf6fa22db7a03fbb035 Mon Sep 17 00:00:00 2001 From: Rajas Paranjpe <52586855+ChocolateLoverRaj@users.noreply.github.com> Date: Sun, 8 Jun 2025 11:01:14 -0700 Subject: [PATCH] Add features to work with no_std, and with alloc in no_std Always include std feature in make tests I would assume all of the tests require std since this crate originally was std only Add clippy warnings to always use `core` and `alloc` instead of `std` Fix clippy by replacing std with core and alloc when possible Some changes Add --lib when testing without default features Make windows and wasm modules require std feature. --- Cargo.toml | 28 +++++++++++++++++++++++++--- Makefile | 8 +++++--- src/ansi.rs | 6 ++++-- src/kb.rs | 2 ++ src/lib.rs | 26 +++++++++++++++++++++----- src/term.rs | 6 +++--- src/unix_term.rs | 4 +--- src/utils.rs | 11 ++++++----- 8 files changed, 67 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 81989828..4e15a84a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,13 +13,15 @@ readme = "README.md" rust-version = "1.66" [features] -default = ["unicode-width", "ansi-parsing"] +default = ["unicode-width", "ansi-parsing", "std"] +std = ["dep:libc", "dep:once_cell", "alloc"] +alloc = [] windows-console-colors = ["ansi-parsing"] ansi-parsing = [] [dependencies] -libc = "0.2.99" -once_cell = "1.8" +libc = { version = "0.2.99", optional = true } +once_cell = { version = "1.8", optional = true } unicode-width = { version = "0.2", optional = true } [target.'cfg(windows)'.dependencies] @@ -43,6 +45,26 @@ proptest = { version = "1.0.0", default-features = false, features = [ ] } regex = "1.4.2" +[[example]] +name = "colors" +required-features = ["std"] + +[[example]] +name = "colors256" +required-features = ["std"] + +[[example]] +name = "cursor_at" +required-features = ["std"] + +[[example]] +name = "keyboard" +required-features = ["std"] + +[[example]] +name = "term" +required-features = ["std"] + ## These are currently disabled. If you want to play around with the benchmarks ## uncommit this. #criterion = "0.3.5" diff --git a/Makefile b/Makefile index 73607960..cb534e7c 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,11 @@ test: @echo "CARGO TESTS" @cargo test @cargo test --all-features - @cargo test --no-default-features - @cargo test --no-default-features --features ansi-parsing - @cargo test --no-default-features --features unicode-width + @cargo test --lib --no-default-features + @cargo test --lib --no-default-features --features alloc + @cargo test --no-default-features --features std + @cargo test --no-default-features --features std,ansi-parsing + @cargo test --no-default-features --features std,unicode-width check-minver: @echo "MINVER CHECK" diff --git a/src/ansi.rs b/src/ansi.rs index 371ca541..4bd6d2b8 100644 --- a/src/ansi.rs +++ b/src/ansi.rs @@ -1,5 +1,6 @@ -use std::{ - borrow::Cow, +#[cfg(feature = "alloc")] +use alloc::{borrow::Cow, string::String}; +use core::{ iter::{FusedIterator, Peekable}, str::CharIndices, }; @@ -186,6 +187,7 @@ fn find_ansi_code_exclusive(it: &mut Peekable) -> Option<(usize, us } /// Helper function to strip ansi codes. +#[cfg(feature = "alloc")] pub fn strip_ansi_codes(s: &str) -> Cow { let mut char_it = s.char_indices().peekable(); match find_ansi_code_exclusive(&mut char_it) { diff --git a/src/kb.rs b/src/kb.rs index 2a0f61ed..101d77e7 100644 --- a/src/kb.rs +++ b/src/kb.rs @@ -1,3 +1,5 @@ +use alloc::vec::Vec; + /// Key mapping /// /// This is an incomplete mapping of keys that are supported for reading diff --git a/src/lib.rs b/src/lib.rs index 2d3114b0..4246d309 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,30 +76,46 @@ //! for stripping and taking ansi escape codes into account for length //! calculations). -#![warn(unreachable_pub)] +#![warn( + unreachable_pub, + clippy::std_instead_of_core, + clippy::std_instead_of_alloc +)] +#![cfg_attr(not(feature = "std"), no_std)] +#[cfg(feature = "alloc")] +extern crate alloc; +#[cfg(feature = "alloc")] pub use crate::kb::Key; +#[cfg(feature = "std")] pub use crate::term::{ user_attended, user_attended_stderr, Term, TermFamily, TermFeatures, TermTarget, }; +#[cfg(feature = "std")] pub use crate::utils::{ colors_enabled, colors_enabled_stderr, measure_text_width, pad_str, pad_str_with, set_colors_enabled, set_colors_enabled_stderr, style, truncate_str, Alignment, Attribute, Color, Emoji, Style, StyledObject, }; +#[cfg(all(feature = "ansi-parsing", feature = "alloc"))] +pub use crate::ansi::strip_ansi_codes; #[cfg(feature = "ansi-parsing")] -pub use crate::ansi::{strip_ansi_codes, AnsiCodeIterator}; +pub use crate::ansi::AnsiCodeIterator; +#[cfg(feature = "std")] mod common_term; +#[cfg(feature = "alloc")] mod kb; +#[cfg(feature = "std")] mod term; -#[cfg(all(unix, not(target_arch = "wasm32")))] +#[cfg(all(unix, not(target_arch = "wasm32"), feature = "std"))] mod unix_term; +#[cfg(feature = "std")] mod utils; -#[cfg(target_arch = "wasm32")] +#[cfg(all(feature = "std", target_arch = "wasm32"))] mod wasm_term; -#[cfg(windows)] +#[cfg(all(feature = "std", windows))] mod windows_term; #[cfg(feature = "ansi-parsing")] diff --git a/src/term.rs b/src/term.rs index 2d6c040b..87f5a9ae 100644 --- a/src/term.rs +++ b/src/term.rs @@ -1,11 +1,11 @@ -use std::fmt::{Debug, Display}; +use alloc::sync::Arc; +use core::fmt::{Debug, Display}; use std::io::{self, Read, Write}; -use std::sync::{Arc, Mutex, RwLock}; - #[cfg(any(unix, all(target_os = "wasi", target_env = "p1")))] use std::os::fd::{AsRawFd, RawFd}; #[cfg(windows)] use std::os::windows::io::{AsRawHandle, RawHandle}; +use std::sync::{Mutex, RwLock}; use crate::{kb::Key, utils::Style}; diff --git a/src/unix_term.rs b/src/unix_term.rs index c37d7780..2e50c3e0 100644 --- a/src/unix_term.rs +++ b/src/unix_term.rs @@ -1,10 +1,8 @@ +use core::{fmt::Display, mem, str}; use std::env; -use std::fmt::Display; use std::fs; use std::io::{self, BufRead, BufReader}; -use std::mem; use std::os::fd::{AsRawFd, RawFd}; -use std::str; #[cfg(not(target_os = "macos"))] use once_cell::sync::Lazy; diff --git a/src/utils.rs b/src/utils.rs index 0a64be0a..f813ce4a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,8 +1,9 @@ -use std::borrow::Cow; +use alloc::borrow::Cow; +use core::{ + fmt::{self, Debug, Formatter}, + sync::atomic::{AtomicBool, Ordering}, +}; use std::env; -use std::fmt; -use std::fmt::{Debug, Formatter}; -use std::sync::atomic::{AtomicBool, Ordering}; use once_cell::sync::Lazy; @@ -820,7 +821,7 @@ pub fn truncate_str<'a>(s: &'a str, width: usize, tail: &str) -> Cow<'a, str> { #[cfg(feature = "ansi-parsing")] { - use std::cmp::Ordering; + use core::cmp::Ordering; let mut iter = AnsiCodeIterator::new(s); let mut length = 0; let mut rv = None;