chore: add StyledStr::destyle

This commit is contained in:
Shunsuke Shibayama 2024-10-11 21:27:32 +09:00
parent e1adb8d118
commit f6144b29a2
2 changed files with 36 additions and 1 deletions

View file

@ -162,6 +162,20 @@ impl Color {
Color::White => WHITE,
}
}
pub fn decolor(colored: &str) -> &str {
colored
.trim_end_matches(RESET)
.trim_start_matches(BLACK)
.trim_start_matches(BLUE)
.trim_start_matches(CYAN)
.trim_start_matches(GRAY)
.trim_start_matches(GREEN)
.trim_start_matches(MAGENTA)
.trim_start_matches(RED)
.trim_start_matches(YELLOW)
.trim_start_matches(WHITE)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)]
@ -181,6 +195,14 @@ impl Attribute {
Attribute::Reversed => REVERSED,
}
}
pub fn deattr(attributed: &str) -> &str {
attributed
.trim_end_matches(ATTR_RESET)
.trim_start_matches(UNDERLINE)
.trim_start_matches(BOLD)
.trim_start_matches(REVERSED)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
@ -347,6 +369,10 @@ impl<'a> StyledStr<'a> {
attribute,
}
}
pub fn destyle(styled: &str) -> &str {
Attribute::deattr(Color::decolor(styled))
}
}
impl std::fmt::Display for StyledStr<'_> {

View file

@ -1,9 +1,18 @@
.available_zones: Set Str, _
.available_timezones: () -> Set(Str, _)
.reset_tzpath!: (to := Sequence(Str) or PathLike) => NoneType
.TZPATH: [Str; _]
.ZoneInfoNotFoundError: ClassType
.ZoneInfoNotFoundError <: Exception
.InvalidTZPathWarning: ClassType
.InvalidTZPathWarning <: Warning
.ZoneInfo: ClassType
.ZoneInfo.
key: Str
__call__: (key: Str) -> .ZoneInfo
from_file: (fobj: FileLike, key := Str) -> .ZoneInfo
no_cache: (key: Str) -> .ZoneInfo
clear_cache!: () => NoneType
__str__: (self: .ZoneInfo) -> Str