@@ -10,12 +10,8 @@ use std::{
10
10
Arc ,
11
11
} ,
12
12
thread:: { self , JoinHandle } ,
13
- time:: Duration ,
14
13
} ;
15
14
16
- /// Timeout used for [`IndicatorHandle`]'s `priority_mailbox`.
17
- const PRIORITY_MAIL_TIMEOUT : Duration = Duration :: from_nanos ( 1 ) ;
18
-
19
15
/// Responsible for displying the progress indicator. This struct will be owned by a separate
20
16
/// thread that is responsible for displaying the progress text whereas the [`IndicatorHandle`]
21
17
/// is how the outside world will interact with it.
@@ -28,13 +24,9 @@ pub struct Indicator<'a> {
28
24
/// This struct is how the outside world will inform the [`Indicator`] about the progress of the
29
25
/// program. The `join_handle` returns the handle to the thread that owns the [`Indicator`] and the
30
26
/// `mailbox` is the [`SyncSender`] channel that allows [`Message`]s to be sent to [`Indicator`].
31
- ///
32
- /// The `priority_mailbox` is used to prematurely terminate the [`Indicator`] in the case of say a
33
- /// `SIGINT` signal.
34
27
pub struct IndicatorHandle {
35
28
pub join_handle : Option < JoinHandle < Result < ( ) , Error > > > ,
36
29
mailbox : SyncSender < Message > ,
37
- priority_mailbox : SyncSender < ( ) > ,
38
30
}
39
31
40
32
/// The different messages that could be sent to the thread that owns the [`Indicator`].
@@ -49,6 +41,9 @@ pub enum Message {
49
41
/// Message that indicates that the output is ready to be flushed and that we should cleanup
50
42
/// the [`Indicator`] as well as the screen.
51
43
RenderReady ,
44
+
45
+ /// Tear down the progress indicator for whatever reason.
46
+ Finish ,
52
47
}
53
48
54
49
/// All of the different states the [`Indicator`] can be in during its life cycle.
@@ -73,7 +68,7 @@ pub enum Error {
73
68
Io ( #[ from] io:: Error ) ,
74
69
75
70
#[ error( "#{0}" ) ]
76
- Send ( #[ from] SendError < ( ) > ) ,
71
+ Send ( #[ from] SendError < Message > ) ,
77
72
}
78
73
79
74
impl Default for Indicator < ' _ > {
@@ -92,12 +87,10 @@ impl IndicatorHandle {
92
87
pub fn new (
93
88
join_handle : Option < JoinHandle < Result < ( ) , Error > > > ,
94
89
mailbox : SyncSender < Message > ,
95
- priority_mailbox : SyncSender < ( ) > ,
96
90
) -> Self {
97
91
Self {
98
92
join_handle,
99
93
mailbox,
100
- priority_mailbox,
101
94
}
102
95
}
103
96
@@ -106,15 +99,10 @@ impl IndicatorHandle {
106
99
self . mailbox . clone ( )
107
100
}
108
101
109
- /// Getter for a cloned `priority_mailbox` wherewith to send [`Message`]s to the [`Indicator`].
110
- pub fn priority_mailbox ( & self ) -> SyncSender < ( ) > {
111
- self . priority_mailbox . clone ( )
112
- }
113
-
114
102
/// Send a message through to the `priority_mailbox` tear down the [`Indicator`].
115
103
pub fn terminate ( this : Option < Arc < Self > > ) -> Result < ( ) , Error > {
116
104
if let Some ( mut handle) = this {
117
- handle. priority_mailbox ( ) . send ( ( ) ) ?;
105
+ handle. mailbox ( ) . send ( Message :: Finish ) ?;
118
106
119
107
if let Some ( hand) = Arc :: get_mut ( & mut handle) {
120
108
hand. join_handle
@@ -134,7 +122,6 @@ impl<'a> Indicator<'a> {
134
122
/// outside world to send messages to the worker thread and ultimately to the [`Indicator`].
135
123
pub fn measure ( ) -> IndicatorHandle {
136
124
let ( tx, rx) = mpsc:: sync_channel ( 1024 ) ;
137
- let ( ptx, prx) = mpsc:: sync_channel ( 1 ) ;
138
125
139
126
let join_handle = thread:: spawn ( move || {
140
127
let mut indicator = Self :: default ( ) ;
@@ -143,17 +130,12 @@ impl<'a> Indicator<'a> {
143
130
indicator. stdout . execute ( cursor:: Hide ) ?;
144
131
145
132
while let Ok ( msg) = rx. recv ( ) {
146
- if prx. recv_timeout ( PRIORITY_MAIL_TIMEOUT ) . is_ok ( ) {
147
- indicator. update_state ( IndicatorState :: Done ) ?;
148
- return Ok ( ( ) ) ;
149
- }
150
-
151
133
match msg {
152
134
Message :: Index => indicator. index ( ) ?,
153
135
Message :: DoneIndexing => {
154
136
indicator. update_state ( IndicatorState :: Rendering ) ?;
155
137
} ,
156
- Message :: RenderReady => {
138
+ Message :: RenderReady | Message :: Finish => {
157
139
indicator. update_state ( IndicatorState :: Done ) ?;
158
140
return Ok ( ( ) ) ;
159
141
} ,
@@ -165,7 +147,7 @@ impl<'a> Indicator<'a> {
165
147
Ok ( ( ) )
166
148
} ) ;
167
149
168
- IndicatorHandle :: new ( Some ( join_handle) , tx, ptx )
150
+ IndicatorHandle :: new ( Some ( join_handle) , tx)
169
151
}
170
152
171
153
/// Updates the `state` of the [`Indicator`] to `new_state`, immediately running an associated
0 commit comments