pub struct ProgressBar { /* private fields */ }Implementations§
Source§impl ProgressBar
impl ProgressBar
Sourcepub fn new(total: u64, message: impl Into<String>) -> (Self, ProgressHandle)
pub fn new(total: u64, message: impl Into<String>) -> (Self, ProgressHandle)
Examples found in repository?
examples/progress_basic.rs (line 8)
5fn main() {
6 println!("=== ProgressBar Component Demo ===\n");
7
8 let (bar, handle) = ProgressBar::new(100, "Downloading...");
9
10 // 后台任务
11 let task = thread::spawn(move || {
12 for i in 0..100 {
13 thread::sleep(Duration::from_millis(50));
14 handle.inc(1);
15
16 // 在中途更新消息
17 if i == 50 {
18 handle.set_message("Processing...");
19 }
20 }
21 handle.finish();
22 });
23
24 // 运行进度条(阻塞主线程)
25 if let Err(e) = bar.run() {
26 eprintln!("Error: {}", e);
27 }
28
29 // 等待任务完成
30 task.join().unwrap();
31
32 println!("Done!");
33}More examples
examples/p1_demo.rs (line 36)
5fn main() {
6 println!("=== P1 Components Demo ===\n");
7
8 // 1. MultiSelect
9 let languages = match MultiSelect::new("Select languages you know:")
10 .option("Rust", "rust")
11 .option("Python", "python")
12 .option("JavaScript", "javascript")
13 .option("Go", "go")
14 .run()
15 {
16 Ok(langs) => langs,
17 Err(e) => {
18 eprintln!("Error: {}", e);
19 return;
20 }
21 };
22
23 println!("\nSelected: {:?}\n", languages);
24
25 // 2. Spinner
26 let (spinner, handle) = Spinner::new("Analyzing your selections...");
27 let task = thread::spawn(move || {
28 thread::sleep(Duration::from_secs(2));
29 handle.finish();
30 });
31 spinner.run().unwrap();
32 task.join().unwrap();
33 println!("✓ Analysis complete\n");
34
35 // 3. ProgressBar
36 let (bar, handle) = ProgressBar::new(100, "Generating recommendations...");
37 let task = thread::spawn(move || {
38 for _ in 0..100 {
39 thread::sleep(Duration::from_millis(30));
40 handle.inc(1);
41 }
42 handle.finish();
43 });
44 bar.run().unwrap();
45 task.join().unwrap();
46 println!("✓ Recommendations ready\n");
47
48 // 4. Confirm
49 let save = match Confirm::new("Save your profile?").default(true).run() {
50 Ok(s) => s,
51 Err(e) => {
52 eprintln!("Error: {}", e);
53 return;
54 }
55 };
56
57 if save {
58 println!("\n✓ Profile saved!");
59 } else {
60 println!("\n✗ Profile not saved");
61 }
62}Sourcepub fn run(self) -> Result<()>
pub fn run(self) -> Result<()>
Examples found in repository?
examples/progress_basic.rs (line 25)
5fn main() {
6 println!("=== ProgressBar Component Demo ===\n");
7
8 let (bar, handle) = ProgressBar::new(100, "Downloading...");
9
10 // 后台任务
11 let task = thread::spawn(move || {
12 for i in 0..100 {
13 thread::sleep(Duration::from_millis(50));
14 handle.inc(1);
15
16 // 在中途更新消息
17 if i == 50 {
18 handle.set_message("Processing...");
19 }
20 }
21 handle.finish();
22 });
23
24 // 运行进度条(阻塞主线程)
25 if let Err(e) = bar.run() {
26 eprintln!("Error: {}", e);
27 }
28
29 // 等待任务完成
30 task.join().unwrap();
31
32 println!("Done!");
33}More examples
examples/p1_demo.rs (line 44)
5fn main() {
6 println!("=== P1 Components Demo ===\n");
7
8 // 1. MultiSelect
9 let languages = match MultiSelect::new("Select languages you know:")
10 .option("Rust", "rust")
11 .option("Python", "python")
12 .option("JavaScript", "javascript")
13 .option("Go", "go")
14 .run()
15 {
16 Ok(langs) => langs,
17 Err(e) => {
18 eprintln!("Error: {}", e);
19 return;
20 }
21 };
22
23 println!("\nSelected: {:?}\n", languages);
24
25 // 2. Spinner
26 let (spinner, handle) = Spinner::new("Analyzing your selections...");
27 let task = thread::spawn(move || {
28 thread::sleep(Duration::from_secs(2));
29 handle.finish();
30 });
31 spinner.run().unwrap();
32 task.join().unwrap();
33 println!("✓ Analysis complete\n");
34
35 // 3. ProgressBar
36 let (bar, handle) = ProgressBar::new(100, "Generating recommendations...");
37 let task = thread::spawn(move || {
38 for _ in 0..100 {
39 thread::sleep(Duration::from_millis(30));
40 handle.inc(1);
41 }
42 handle.finish();
43 });
44 bar.run().unwrap();
45 task.join().unwrap();
46 println!("✓ Recommendations ready\n");
47
48 // 4. Confirm
49 let save = match Confirm::new("Save your profile?").default(true).run() {
50 Ok(s) => s,
51 Err(e) => {
52 eprintln!("Error: {}", e);
53 return;
54 }
55 };
56
57 if save {
58 println!("\n✓ Profile saved!");
59 } else {
60 println!("\n✗ Profile not saved");
61 }
62}Auto Trait Implementations§
impl Freeze for ProgressBar
impl RefUnwindSafe for ProgressBar
impl Send for ProgressBar
impl !Sync for ProgressBar
impl Unpin for ProgressBar
impl UnwindSafe for ProgressBar
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more