Skip to content

Commit be424c0

Browse files
committed
Add TermProgress for emitting OSC 9;4 codes
1 parent 5f46878 commit be424c0

2 files changed

Lines changed: 95 additions & 1 deletion

File tree

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub use crate::utils::{
9898
set_colors_enabled_stderr, set_progress_integration, set_progress_integration_stderr,
9999
set_true_colors_enabled, set_true_colors_enabled_stderr, style, true_colors_enabled,
100100
true_colors_enabled_stderr, truncate_str, Alignment, Attribute, Color, Emoji, Style,
101-
StyledObject,
101+
StyledObject, TermProgress,
102102
};
103103

104104
#[cfg(all(feature = "ansi-parsing", feature = "alloc"))]

src/utils.rs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,100 @@ pub fn pad_str_with<'a>(
10761076
Cow::Owned(rv)
10771077
}
10781078

1079+
#[derive(Clone)]
1080+
pub struct TermProgress {
1081+
inner: anstyle_progress::TermProgress,
1082+
force: Option<bool>,
1083+
for_stderr: bool,
1084+
}
1085+
1086+
impl Default for TermProgress {
1087+
fn default() -> Self {
1088+
Self::new()
1089+
}
1090+
}
1091+
1092+
impl TermProgress {
1093+
pub const fn new() -> Self {
1094+
Self {
1095+
inner: anstyle_progress::TermProgress::none(),
1096+
force: None,
1097+
for_stderr: false,
1098+
}
1099+
}
1100+
1101+
/// Forces terminal-integrated progress on or off.
1102+
///
1103+
/// This overrides the automatic detection.
1104+
#[inline]
1105+
pub const fn force_styling(mut self, value: bool) -> Self {
1106+
self.force = Some(value);
1107+
self
1108+
}
1109+
1110+
/// Specifies that terminal-integrated progress is being written on stderr.
1111+
#[inline]
1112+
pub const fn for_stderr(mut self) -> Self {
1113+
self.for_stderr = true;
1114+
self
1115+
}
1116+
1117+
/// Specifies that terminal-integrated progress is being written on stdout.
1118+
///
1119+
/// This is the default behaviour.
1120+
#[inline]
1121+
pub const fn for_stdout(mut self) -> Self {
1122+
self.for_stderr = false;
1123+
self
1124+
}
1125+
1126+
/// Start a progress indicator
1127+
///
1128+
/// This starts in an indeterminate state
1129+
pub const fn start(mut self) -> Self {
1130+
self.inner = self
1131+
.inner
1132+
.status(anstyle_progress::TermProgressStatus::Normal);
1133+
self
1134+
}
1135+
1136+
/// Set progress percentage (between `0..=100`)
1137+
///
1138+
/// Without setting this, progress will be indeterminate
1139+
pub const fn percent(mut self, percent: u8) -> Self {
1140+
self.inner = self.inner.percent(percent);
1141+
self
1142+
}
1143+
1144+
/// Start an error indicator
1145+
pub const fn error(mut self) -> Self {
1146+
self.inner = self
1147+
.inner
1148+
.status(anstyle_progress::TermProgressStatus::Error);
1149+
self
1150+
}
1151+
1152+
/// Remove the indicator
1153+
pub const fn remove(mut self) -> Self {
1154+
self.inner = self
1155+
.inner
1156+
.status(anstyle_progress::TermProgressStatus::Removed);
1157+
self
1158+
}
1159+
}
1160+
1161+
impl fmt::Display for TermProgress {
1162+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1163+
if self.force.unwrap_or_else(|| match self.for_stderr {
1164+
true => progress_integration_stderr(),
1165+
false => progress_integration(),
1166+
}) {
1167+
self.inner.fmt(f)?;
1168+
}
1169+
Ok(())
1170+
}
1171+
}
1172+
10791173
#[test]
10801174
fn test_text_width() {
10811175
let s = style("foo")

0 commit comments

Comments
 (0)