55/// ```rust
66/// # use anstyle_progress::TermProgress;
77/// # use anstyle_progress::TermProgressStatus;
8- /// let mut progress = anstyle_progress::TermProgress::none()
9- /// .status(TermProgressStatus::Normal);
8+ /// let mut progress = TermProgress::start();
109///
11- /// let progress = progress.percent(Some(0) );
10+ /// let progress = progress.percent(0 );
1211/// println!("{progress}");
1312///
14- /// let progress = progress.percent(Some(50) );
13+ /// let progress = progress.percent(50 );
1514/// println!("{progress}");
1615///
17- /// let progress = progress.percent(Some( 100) );
16+ /// let progress = progress.percent(100);
1817/// println!("{progress}");
1918///
20- /// println!("{progress:#}");
19+ /// let progress = TermProgress::remove();
20+ /// println!("{progress}");
2121/// ```
2222#[ derive( Copy , Clone ) ]
2323pub struct TermProgress {
@@ -34,16 +34,35 @@ impl TermProgress {
3434 }
3535 }
3636
37- /// Change the reported status
38- pub fn status ( mut self , status : TermProgressStatus ) -> Self {
39- self . status = Some ( status) ;
37+ /// Start a progress indicator
38+ ///
39+ /// This starts in an indeterminate state
40+ pub fn start ( ) -> Self {
41+ Self :: none ( ) . status ( TermProgressStatus :: Normal )
42+ }
43+
44+ /// Start an error indicator
45+ pub fn error ( ) -> Self {
46+ Self :: none ( ) . status ( TermProgressStatus :: Error )
47+ }
48+
49+ /// Remove the indicator
50+ pub fn remove ( ) -> Self {
51+ Self :: none ( ) . status ( TermProgressStatus :: Removed )
52+ }
53+
54+ /// Set progress percentage (between `0..=100`)
55+ ///
56+ /// Without setting this, progress will be indeterminate
57+ pub fn percent ( mut self , percent : u8 ) -> Self {
58+ assert ! ( matches!( percent, 0 ..=100 ) ) ;
59+ self . percent = Some ( percent) ;
4060 self
4161 }
4262
43- /// Between `0..=100`
44- pub fn percent ( mut self , percent : Option < u8 > ) -> Self {
45- assert ! ( matches!( percent, Some ( 0 ..=100 ) | None ) ) ;
46- self . percent = percent;
63+ /// Change the reported status
64+ pub fn status ( mut self , status : TermProgressStatus ) -> Self {
65+ self . status = Some ( status) ;
4766 self
4867 }
4968}
@@ -58,6 +77,7 @@ impl Default for TermProgress {
5877#[ allow( missing_docs) ]
5978#[ derive( Copy , Clone ) ]
6079pub enum TermProgressStatus {
80+ Removed ,
6181 Normal ,
6282 /// Some terminals treat this as a Warning
6383 Paused ,
@@ -69,14 +89,17 @@ impl core::fmt::Display for TermProgress {
6989 let Some ( status) = self . status else {
7090 return Ok ( ( ) ) ;
7191 } ;
72- let st = match ( f . alternate ( ) , status, self . percent ) {
73- ( true , _, _ ) => 0 ,
74- ( false , TermProgressStatus :: Normal , Some ( _) ) => 1 ,
75- ( false , TermProgressStatus :: Error , _) => 2 ,
76- ( false , TermProgressStatus :: Normal , None ) => 3 ,
77- ( false , TermProgressStatus :: Paused , _) => 4 ,
92+ let ( st , pr ) = match ( status, self . percent ) {
93+ ( TermProgressStatus :: Removed , _) => ( 0 , None ) ,
94+ ( TermProgressStatus :: Normal , Some ( _) ) => ( 1 , self . percent ) ,
95+ ( TermProgressStatus :: Error , _) => ( 2 , self . percent ) ,
96+ ( TermProgressStatus :: Normal , None ) => ( 3 , None ) ,
97+ ( TermProgressStatus :: Paused , _) => ( 4 , self . percent ) ,
7898 } ;
79- let pr = self . percent . unwrap_or ( 0 ) ;
80- write ! ( f, "\x1b ]9;4;{st};{pr}\x1b \\ " )
99+ write ! ( f, "\x1b ]9;4;{st}" ) ?;
100+ if let Some ( pr) = pr {
101+ write ! ( f, ";{pr}" ) ?;
102+ }
103+ write ! ( f, "\x1b \\ " )
81104 }
82105}
0 commit comments