Skip to content

Commit 21b8234

Browse files
authored
Merge pull request #55 from ynqa/revert-36-feature/custom-writer
Revert "Add custom writer"
2 parents 6abbf72 + 14a4471 commit 21b8234

File tree

10 files changed

+31
-116
lines changed

10 files changed

+31
-116
lines changed

promkit/src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,13 +224,12 @@ pub trait Renderer: Finalizer {
224224
/// event handling, and result production for a prompt.
225225
pub struct Prompt<T: Renderer> {
226226
pub renderer: T,
227-
pub writer: Box<dyn io::Write>,
228227
}
229228

230229
impl<T: Renderer> Drop for Prompt<T> {
231230
fn drop(&mut self) {
232231
execute!(
233-
self.writer,
232+
io::stdout(),
234233
cursor::Show,
235234
event::DisableMouseCapture,
236235
cursor::MoveToNextLine(1),
@@ -252,19 +251,23 @@ impl<T: Renderer> Prompt<T> {
252251
/// Returns a `Result` containing the produced result or an error.
253252
pub fn run(&mut self) -> anyhow::Result<T::Return> {
254253
enable_raw_mode()?;
255-
execute!(self.writer, cursor::Hide)?;
254+
execute!(io::stdout(), cursor::Hide)?;
256255

257256
let size = crossterm::terminal::size()?;
258257
let panes = self.renderer.create_panes(size.0, size.1);
259-
let mut terminal = Terminal::start_session(&panes, &mut self.writer)?;
258+
let mut terminal = Terminal::start_session(&panes)?;
260259
terminal.draw(&panes)?;
261260

262261
loop {
263262
let ev = event::read()?;
264263

265264
match &ev {
266265
Event::Resize(_, _) => {
267-
terminal.on_resize()?;
266+
terminal.position = (0, 0);
267+
crossterm::execute!(
268+
io::stdout(),
269+
crossterm::terminal::Clear(crossterm::terminal::ClearType::Purge),
270+
)?;
268271
}
269272
_ => {
270273
if self.renderer.evaluate(&ev)? == PromptSignal::Quit {

promkit/src/preset/checkbox.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, fmt::Display, io};
1+
use std::{cell::RefCell, fmt::Display};
22

33
use crate::{
44
checkbox,
@@ -20,8 +20,6 @@ pub struct Checkbox {
2020
title_state: text::State,
2121
/// State for the checkbox list itself.
2222
checkbox_state: checkbox::State,
23-
/// Writer to which promptkit write its contents
24-
writer: Box<dyn io::Write>,
2523
}
2624

2725
impl Checkbox {
@@ -51,7 +49,6 @@ impl Checkbox {
5149
lines: Default::default(),
5250
},
5351
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
54-
writer: Box::new(io::stdout()),
5552
}
5653
}
5754

@@ -74,7 +71,6 @@ impl Checkbox {
7471
lines: Default::default(),
7572
},
7673
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
77-
writer: Box::new(io::stdout()),
7874
}
7975
}
8076

@@ -125,12 +121,6 @@ impl Checkbox {
125121
self
126122
}
127123

128-
/// Sets writer.
129-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
130-
self.writer = Box::new(writer);
131-
self
132-
}
133-
134124
/// Displays the checkbox prompt and waits for user input.
135125
/// Returns a `Result` containing the `Prompt` result,
136126
/// which is a list of selected options.
@@ -141,7 +131,6 @@ impl Checkbox {
141131
title_state: self.title_state,
142132
checkbox_state: self.checkbox_state,
143133
},
144-
writer: self.writer,
145134
})
146135
}
147136
}

promkit/src/preset/form.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, io};
1+
use std::cell::RefCell;
22

33
use crate::{
44
core::Cursor,
@@ -17,8 +17,6 @@ pub struct Form {
1717
text_editor_states: Vec<text_editor::State>,
1818
/// Overwrite the default styles of text editor states when unselected.
1919
overwrite_styles: Vec<render::Style>,
20-
/// Writer to which promptkit write its contents
21-
writer: Box<dyn io::Write>,
2220
}
2321

2422
impl Form {
@@ -44,16 +42,9 @@ impl Form {
4442
keymap: ActiveKeySwitcher::new("default", self::keymap::default as keymap::Keymap),
4543
text_editor_states,
4644
overwrite_styles,
47-
writer: Box::new(io::stdout()),
4845
}
4946
}
5047

51-
/// Sets writer.
52-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
53-
self.writer = Box::new(writer);
54-
self
55-
}
56-
5748
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
5849
let default_styles = self
5950
.text_editor_states
@@ -71,9 +62,6 @@ impl Form {
7162
overwrite_styles: self.overwrite_styles,
7263
};
7364
renderer.overwrite_styles();
74-
Ok(Prompt {
75-
renderer,
76-
writer: self.writer,
77-
})
65+
Ok(Prompt { renderer })
7866
}
7967
}

promkit/src/preset/json.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, io};
1+
use std::cell::RefCell;
22

33
use crate::{
44
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
@@ -18,8 +18,6 @@ pub struct Json {
1818
keymap: ActiveKeySwitcher<keymap::Keymap>,
1919
title_state: text::State,
2020
json_state: jsonstream::State,
21-
/// Writer to which promptkit write its contents
22-
writer: Box<dyn io::Write>,
2321
}
2422

2523
impl Json {
@@ -53,7 +51,6 @@ impl Json {
5351
lines: Default::default(),
5452
},
5553
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
56-
writer: Box::new(io::stdout()),
5754
}
5855
}
5956

@@ -98,12 +95,6 @@ impl Json {
9895
self
9996
}
10097

101-
/// Sets writer.
102-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
103-
self.writer = Box::new(writer);
104-
self
105-
}
106-
10798
/// Creates a prompt based on the current configuration of the `Json` instance.
10899
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
109100
Ok(Prompt {
@@ -112,7 +103,6 @@ impl Json {
112103
title_state: self.title_state,
113104
json_state: self.json_state,
114105
},
115-
writer: self.writer,
116106
})
117107
}
118108
}

promkit/src/preset/listbox.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, fmt::Display, io};
1+
use std::{cell::RefCell, fmt::Display};
22

33
use crate::{
44
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
@@ -19,8 +19,6 @@ pub struct Listbox {
1919
title_state: text::State,
2020
/// State for the selectable list itself.
2121
listbox_state: listbox::State,
22-
/// Writer to which promptkit write its contents
23-
writer: Box<dyn io::Write>,
2422
}
2523

2624
impl Listbox {
@@ -48,7 +46,6 @@ impl Listbox {
4846
lines: Default::default(),
4947
},
5048
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
51-
writer: Box::new(io::stdout()),
5249
}
5350
}
5451

@@ -93,12 +90,6 @@ impl Listbox {
9390
self
9491
}
9592

96-
/// Sets writer.
97-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
98-
self.writer = Box::new(writer);
99-
self
100-
}
101-
10293
/// Displays the select prompt and waits for user input.
10394
/// Returns a `Result` containing the `Prompt` result,
10495
/// which is the selected option.
@@ -109,7 +100,6 @@ impl Listbox {
109100
title_state: self.title_state,
110101
listbox_state: self.listbox_state,
111102
},
112-
writer: self.writer,
113103
})
114104
}
115105
}

promkit/src/preset/query_selector.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, fmt::Display, io};
1+
use std::{cell::RefCell, fmt::Display};
22

33
use crate::{
44
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
@@ -28,8 +28,6 @@ pub struct QuerySelector {
2828
/// A filter function to apply to the list box items
2929
/// based on the text editor input.
3030
filter: render::Filter,
31-
/// Writer to which promptkit write its contents
32-
writer: Box<dyn io::Write>,
3331
}
3432

3533
impl QuerySelector {
@@ -77,7 +75,6 @@ impl QuerySelector {
7775
},
7876
keymap: ActiveKeySwitcher::new("default", self::keymap::default),
7977
filter,
80-
writer: Box::new(io::stdout()),
8178
}
8279
}
8380

@@ -158,12 +155,6 @@ impl QuerySelector {
158155
self
159156
}
160157

161-
/// Sets writer.
162-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
163-
self.writer = Box::new(writer);
164-
self
165-
}
166-
167158
/// Displays the query select prompt and waits for user input.
168159
/// Returns a `Result` containing the `Prompt` result,
169160
/// which is the selected option.
@@ -176,7 +167,6 @@ impl QuerySelector {
176167
listbox_snapshot: Snapshot::<listbox::State>::new(self.listbox_state),
177168
filter: self.filter,
178169
},
179-
writer: self.writer,
180170
})
181171
}
182172
}

promkit/src/preset/readline.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, collections::HashSet, io};
1+
use std::{cell::RefCell, collections::HashSet};
22

33
use crate::{
44
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
@@ -34,8 +34,6 @@ pub struct Readline {
3434
validator: Option<ValidatorManager<str>>,
3535
/// State for displaying error messages based on input validation.
3636
error_message_state: text::State,
37-
/// Writer to which promptkit write its contents
38-
writer: Box<dyn io::Write>,
3937
}
4038

4139
impl Default for Readline {
@@ -84,7 +82,6 @@ impl Default for Readline {
8482
.build(),
8583
lines: None,
8684
},
87-
writer: Box::new(io::stdout()),
8885
}
8986
}
9087
}
@@ -177,12 +174,6 @@ impl Readline {
177174
self
178175
}
179176

180-
/// Sets writer.
181-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
182-
self.writer = Box::new(writer);
183-
self
184-
}
185-
186177
/// Initiates the prompt process,
187178
/// displaying the configured UI elements and handling user input.
188179
pub fn prompt(self) -> anyhow::Result<Prompt<render::Renderer>> {
@@ -196,7 +187,6 @@ impl Readline {
196187
validator: self.validator,
197188
error_message_snapshot: Snapshot::<text::State>::new(self.error_message_state),
198189
},
199-
writer: self.writer,
200190
})
201191
}
202192
}

promkit/src/preset/text.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, io};
1+
use std::cell::RefCell;
22

33
use crossterm::style::ContentStyle;
44

@@ -10,8 +10,6 @@ pub mod render;
1010
pub struct Text {
1111
keymap: ActiveKeySwitcher<keymap::Keymap>,
1212
text_state: text::State,
13-
/// Writer to which promptkit write its contents
14-
writer: Box<dyn io::Write>,
1513
}
1614

1715
impl Text {
@@ -23,7 +21,6 @@ impl Text {
2321
style: Default::default(),
2422
lines: None,
2523
},
26-
writer: Box::new(io::stdout()),
2724
}
2825
}
2926

@@ -38,7 +35,6 @@ impl Text {
3835
keymap: RefCell::new(self.keymap),
3936
text_state: self.text_state,
4037
},
41-
writer: self.writer,
4238
})
4339
}
4440
}

promkit/src/preset/tree.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, io};
1+
use std::cell::RefCell;
22

33
use crate::{
44
crossterm::style::{Attribute, Attributes, Color, ContentStyle},
@@ -20,8 +20,6 @@ pub struct Tree {
2020
title_state: text::State,
2121
/// State for the tree itself.
2222
tree_state: tree::State,
23-
/// Writer to which promptkit write its contents
24-
writer: Box<dyn io::Write>,
2523
}
2624

2725
impl Tree {
@@ -49,7 +47,6 @@ impl Tree {
4947
lines: Default::default(),
5048
indent: 2,
5149
},
52-
writer: Box::new(io::stdout()),
5350
}
5451
}
5552

@@ -106,12 +103,6 @@ impl Tree {
106103
self
107104
}
108105

109-
/// Sets writer.
110-
pub fn writer<W: io::Write + 'static>(mut self, writer: W) -> Self {
111-
self.writer = Box::new(writer);
112-
self
113-
}
114-
115106
/// Displays the tree prompt and waits for user input.
116107
/// Returns a `Result` containing the `Prompt` result,
117108
/// which is a list of selected options.
@@ -122,7 +113,6 @@ impl Tree {
122113
title_state: self.title_state,
123114
tree_state: self.tree_state,
124115
},
125-
writer: self.writer,
126116
})
127117
}
128118
}

0 commit comments

Comments
 (0)