Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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"
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions src/ansi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
borrow::Cow,
#[cfg(feature = "alloc")]
use alloc::{borrow::Cow, string::String};
use core::{
iter::{FusedIterator, Peekable},
str::CharIndices,
};
Expand Down Expand Up @@ -186,6 +187,7 @@ fn find_ansi_code_exclusive(it: &mut Peekable<CharIndices>) -> Option<(usize, us
}

/// Helper function to strip ansi codes.
#[cfg(feature = "alloc")]
pub fn strip_ansi_codes(s: &str) -> Cow<str> {
let mut char_it = s.char_indices().peekable();
match find_ansi_code_exclusive(&mut char_it) {
Expand Down
2 changes: 2 additions & 0 deletions src/kb.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use alloc::vec::Vec;

/// Key mapping
///
/// This is an incomplete mapping of keys that are supported for reading
Expand Down
26 changes: 21 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
6 changes: 3 additions & 3 deletions src/term.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
4 changes: 1 addition & 3 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
11 changes: 6 additions & 5 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Loading