@@ -22,7 +22,6 @@ enum DoneStatus {
22
22
}
23
23
24
24
pub struct WatchState < ' a > {
25
- stdout : StdoutLock < ' a > ,
26
25
app_state : & ' a mut AppState ,
27
26
output : Vec < u8 > ,
28
27
show_hint : bool ,
@@ -32,11 +31,7 @@ pub struct WatchState<'a> {
32
31
33
32
impl < ' a > WatchState < ' a > {
34
33
pub fn new ( app_state : & ' a mut AppState , manual_run : bool ) -> Self {
35
- // TODO: Take stdout as arg.
36
- let stdout = io:: stdout ( ) . lock ( ) ;
37
-
38
34
Self {
39
- stdout,
40
35
app_state,
41
36
output : Vec :: with_capacity ( OUTPUT_CAPACITY ) ,
42
37
show_hint : false ,
@@ -45,16 +40,11 @@ impl<'a> WatchState<'a> {
45
40
}
46
41
}
47
42
48
- #[ inline]
49
- pub fn into_writer ( self ) -> StdoutLock < ' a > {
50
- self . stdout
51
- }
52
-
53
- pub fn run_current_exercise ( & mut self ) -> Result < ( ) > {
43
+ pub fn run_current_exercise ( & mut self , stdout : & mut StdoutLock ) -> Result < ( ) > {
54
44
self . show_hint = false ;
55
45
56
46
writeln ! (
57
- self . stdout,
47
+ stdout,
58
48
"\n Checking the exercise `{}`. Please wait…" ,
59
49
self . app_state. current_exercise( ) . name,
60
50
) ?;
@@ -78,11 +68,15 @@ impl<'a> WatchState<'a> {
78
68
self . done_status = DoneStatus :: Pending ;
79
69
}
80
70
81
- self . render ( ) ?;
71
+ self . render ( stdout ) ?;
82
72
Ok ( ( ) )
83
73
}
84
74
85
- pub fn handle_file_change ( & mut self , exercise_ind : usize ) -> Result < ( ) > {
75
+ pub fn handle_file_change (
76
+ & mut self ,
77
+ exercise_ind : usize ,
78
+ stdout : & mut StdoutLock ,
79
+ ) -> Result < ( ) > {
86
80
// Don't skip exercises on file changes to avoid confusion from missing exercises.
87
81
// Skipping exercises must be explicit in the interactive list.
88
82
// But going back to an earlier exercise on file change is fine.
@@ -91,118 +85,113 @@ impl<'a> WatchState<'a> {
91
85
}
92
86
93
87
self . app_state . set_current_exercise_ind ( exercise_ind) ?;
94
- self . run_current_exercise ( )
88
+ self . run_current_exercise ( stdout )
95
89
}
96
90
97
91
/// Move on to the next exercise if the current one is done.
98
- pub fn next_exercise ( & mut self ) -> Result < ExercisesProgress > {
92
+ pub fn next_exercise ( & mut self , stdout : & mut StdoutLock ) -> Result < ExercisesProgress > {
99
93
if self . done_status == DoneStatus :: Pending {
100
94
return Ok ( ExercisesProgress :: CurrentPending ) ;
101
95
}
102
96
103
- self . app_state . done_current_exercise ( & mut self . stdout )
97
+ self . app_state . done_current_exercise ( stdout)
104
98
}
105
99
106
- fn show_prompt ( & mut self ) -> io:: Result < ( ) > {
100
+ fn show_prompt ( & self , stdout : & mut StdoutLock ) -> io:: Result < ( ) > {
107
101
if self . manual_run {
108
- self . stdout . queue ( SetAttribute ( Attribute :: Bold ) ) ?;
109
- self . stdout . write_all ( b"r" ) ?;
110
- self . stdout . queue ( ResetColor ) ?;
111
- self . stdout . write_all ( b":run / " ) ?;
102
+ stdout. queue ( SetAttribute ( Attribute :: Bold ) ) ?;
103
+ stdout. write_all ( b"r" ) ?;
104
+ stdout. queue ( ResetColor ) ?;
105
+ stdout. write_all ( b":run / " ) ?;
112
106
}
113
107
114
108
if self . done_status != DoneStatus :: Pending {
115
- self . stdout . queue ( SetAttribute ( Attribute :: Bold ) ) ?;
116
- self . stdout . write_all ( b"n" ) ?;
117
- self . stdout . queue ( ResetColor ) ?;
118
- self . stdout . write_all ( b":" ) ?;
119
- self . stdout . queue ( SetAttribute ( Attribute :: Underlined ) ) ?;
120
- self . stdout . write_all ( b"next" ) ?;
121
- self . stdout . queue ( ResetColor ) ?;
122
- self . stdout . write_all ( b" / " ) ?;
109
+ stdout. queue ( SetAttribute ( Attribute :: Bold ) ) ?;
110
+ stdout. write_all ( b"n" ) ?;
111
+ stdout. queue ( ResetColor ) ?;
112
+ stdout. write_all ( b":" ) ?;
113
+ stdout. queue ( SetAttribute ( Attribute :: Underlined ) ) ?;
114
+ stdout. write_all ( b"next" ) ?;
115
+ stdout. queue ( ResetColor ) ?;
116
+ stdout. write_all ( b" / " ) ?;
123
117
}
124
118
125
119
if !self . show_hint {
126
- self . stdout . queue ( SetAttribute ( Attribute :: Bold ) ) ?;
127
- self . stdout . write_all ( b"h" ) ?;
128
- self . stdout . queue ( ResetColor ) ?;
129
- self . stdout . write_all ( b":hint / " ) ?;
120
+ stdout. queue ( SetAttribute ( Attribute :: Bold ) ) ?;
121
+ stdout. write_all ( b"h" ) ?;
122
+ stdout. queue ( ResetColor ) ?;
123
+ stdout. write_all ( b":hint / " ) ?;
130
124
}
131
125
132
- self . stdout . queue ( SetAttribute ( Attribute :: Bold ) ) ?;
133
- self . stdout . write_all ( b"l" ) ?;
134
- self . stdout . queue ( ResetColor ) ?;
135
- self . stdout . write_all ( b":list / " ) ?;
126
+ stdout. queue ( SetAttribute ( Attribute :: Bold ) ) ?;
127
+ stdout. write_all ( b"l" ) ?;
128
+ stdout. queue ( ResetColor ) ?;
129
+ stdout. write_all ( b":list / " ) ?;
136
130
137
- self . stdout . queue ( SetAttribute ( Attribute :: Bold ) ) ?;
138
- self . stdout . write_all ( b"q" ) ?;
139
- self . stdout . queue ( ResetColor ) ?;
140
- self . stdout . write_all ( b":quit ? " ) ?;
131
+ stdout. queue ( SetAttribute ( Attribute :: Bold ) ) ?;
132
+ stdout. write_all ( b"q" ) ?;
133
+ stdout. queue ( ResetColor ) ?;
134
+ stdout. write_all ( b":quit ? " ) ?;
141
135
142
- self . stdout . flush ( )
136
+ stdout. flush ( )
143
137
}
144
138
145
- pub fn render ( & mut self ) -> io:: Result < ( ) > {
139
+ pub fn render ( & self , stdout : & mut StdoutLock ) -> io:: Result < ( ) > {
146
140
// Prevent having the first line shifted if clearing wasn't successful.
147
- self . stdout . write_all ( b"\n " ) ?;
148
- clear_terminal ( & mut self . stdout ) ?;
141
+ stdout. write_all ( b"\n " ) ?;
142
+ clear_terminal ( stdout) ?;
149
143
150
- self . stdout . write_all ( & self . output ) ?;
144
+ stdout. write_all ( & self . output ) ?;
151
145
152
146
if self . show_hint {
153
- self . stdout
147
+ stdout
154
148
. queue ( SetAttributes (
155
149
Attributes :: from ( Attribute :: Bold ) . with ( Attribute :: Underlined ) ,
156
150
) ) ?
157
151
. queue ( SetForegroundColor ( Color :: Cyan ) ) ?;
158
- self . stdout . write_all ( b"Hint\n " ) ?;
159
- self . stdout . queue ( ResetColor ) ?;
152
+ stdout. write_all ( b"Hint\n " ) ?;
153
+ stdout. queue ( ResetColor ) ?;
160
154
161
- self . stdout
162
- . write_all ( self . app_state . current_exercise ( ) . hint . as_bytes ( ) ) ?;
163
- self . stdout . write_all ( b"\n \n " ) ?;
155
+ stdout. write_all ( self . app_state . current_exercise ( ) . hint . as_bytes ( ) ) ?;
156
+ stdout. write_all ( b"\n \n " ) ?;
164
157
}
165
158
166
159
if self . done_status != DoneStatus :: Pending {
167
- self . stdout
160
+ stdout
168
161
. queue ( SetAttribute ( Attribute :: Bold ) ) ?
169
162
. queue ( SetForegroundColor ( Color :: Green ) ) ?;
170
- self . stdout . write_all ( "Exercise done ✓\n " . as_bytes ( ) ) ?;
171
- self . stdout . queue ( ResetColor ) ?;
163
+ stdout. write_all ( "Exercise done ✓\n " . as_bytes ( ) ) ?;
164
+ stdout. queue ( ResetColor ) ?;
172
165
173
166
if let DoneStatus :: DoneWithSolution ( solution_path) = & self . done_status {
174
- solution_link_line ( & mut self . stdout , solution_path) ?;
167
+ solution_link_line ( stdout, solution_path) ?;
175
168
}
176
169
177
170
writeln ! (
178
- self . stdout,
171
+ stdout,
179
172
"When done experimenting, enter `n` to move on to the next exercise 🦀\n " ,
180
173
) ?;
181
174
}
182
175
183
176
let line_width = terminal:: size ( ) ?. 0 ;
184
177
progress_bar (
185
- & mut self . stdout ,
178
+ stdout,
186
179
self . app_state . n_done ( ) ,
187
180
self . app_state . exercises ( ) . len ( ) as u16 ,
188
181
line_width,
189
182
) ?;
190
183
191
- self . stdout . write_all ( b"\n Current exercise: " ) ?;
192
- terminal_file_link (
193
- & mut self . stdout ,
194
- self . app_state . current_exercise ( ) . path ,
195
- Color :: Blue ,
196
- ) ?;
197
- self . stdout . write_all ( b"\n \n " ) ?;
184
+ stdout. write_all ( b"\n Current exercise: " ) ?;
185
+ terminal_file_link ( stdout, self . app_state . current_exercise ( ) . path , Color :: Blue ) ?;
186
+ stdout. write_all ( b"\n \n " ) ?;
198
187
199
- self . show_prompt ( ) ?;
188
+ self . show_prompt ( stdout ) ?;
200
189
201
190
Ok ( ( ) )
202
191
}
203
192
204
- pub fn show_hint ( & mut self ) -> io:: Result < ( ) > {
193
+ pub fn show_hint ( & mut self , stdout : & mut StdoutLock ) -> io:: Result < ( ) > {
205
194
self . show_hint = true ;
206
- self . render ( )
195
+ self . render ( stdout )
207
196
}
208
197
}
0 commit comments