-
Notifications
You must be signed in to change notification settings - Fork 347
Expand file tree
/
Copy pathfinalization.rs
More file actions
129 lines (114 loc) Β· 3.89 KB
/
finalization.rs
File metadata and controls
129 lines (114 loc) Β· 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is dual-licensed under either the MIT license found in the
* LICENSE-MIT file in the root directory of this source tree or the Apache
* License, Version 2.0 found in the LICENSE-APACHE file in the root directory
* of this source tree. You may select, at your option, one of the
* above-listed licenses.
*/
//! Example that demonstrates finalization.
use std::convert::Infallible;
use std::time::Duration;
use derive_more::Display;
use superconsole::Dimensions;
use superconsole::Lines;
use superconsole::SuperConsole;
use superconsole::components::Component;
use superconsole::components::DrawMode;
use tokio::time;
/// A component representing a store greeter.
struct Greeter<'a> {
name: &'a str,
store_name: &'a StoreName,
customers: &'a [CustomerName],
correct_num: usize,
}
#[derive(Display)]
struct StoreName(String);
#[derive(Display)]
struct CustomerName(String);
impl Component for Greeter<'_> {
type Error = Infallible;
fn draw_unchecked(&self, _dimensions: Dimensions, mode: DrawMode) -> Result<Lines, Infallible> {
Ok(match mode {
DrawMode::Normal => {
// Prints a greeting to the current customer.
let store_name = self.store_name;
let customers = self.customers;
let identification = vec![format!("Hello my name is {}!", self.name)]
.try_into()
.unwrap();
let mut messages = vec![identification];
for customer_name in customers {
let greeting = vec![format!("Welcome to {}, {}!", store_name, customer_name)]
.try_into()
.unwrap();
messages.push(greeting);
}
Lines(messages)
}
DrawMode::Final => {
// Prints a message about the employee when he or she leaves for the day.
let store_name = self.store_name;
let total_customers = self.correct_num;
let farewell = vec![format!("{} is leaving {}", self.name, store_name)]
.try_into()
.unwrap();
let exit_stats =
format!("{} greeted {} customers today", self.name, total_customers);
Lines(vec![farewell, vec![exit_stats].try_into().unwrap()])
}
})
}
}
#[tokio::main]
async fn main() {
let name = "Alex";
let mut console = SuperConsole::new().unwrap();
let people = [
"Joseph", "Janet", "Bob", "Christie", "Raj", "Sasha", "Rayna", "Veronika", "Russel",
"David",
];
let store_names = [
"Target",
"Target",
"Target",
"TJ",
"TJ",
"Walmart",
"Wendys",
"Wendys",
"Uwajimaya",
"DSW",
];
let mut timer = time::interval(Duration::from_secs_f32(0.5));
let mut last = None;
for (i, store_name) in store_names.iter().enumerate() {
console.emit(Lines(vec![vec![i.to_string()].try_into().unwrap()]));
let customers = (i..std::cmp::min(10, i + 2))
.map(|x| CustomerName(people[x].to_owned()))
.collect::<Vec<_>>();
let store_name = StoreName((*store_name).to_owned());
let correct_num = i + 1;
console
.render(&Greeter {
name,
store_name: &store_name,
customers: &customers,
correct_num,
})
.unwrap();
last = Some((store_name, customers, correct_num));
timer.tick().await;
}
let (store_name, customers, correct_num) = last.unwrap();
console
.finalize(&Greeter {
name,
store_name: &store_name,
customers: &customers,
correct_num,
})
.unwrap();
}