Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[ISSUE #1984]♻️Develop SearchInputWidget for tui🚀 #1985

Merged
merged 1 commit into from
Jan 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions rocketmq-tui/src/rocketmq_tui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,20 @@
use ratatui::Frame;
use tokio_stream::StreamExt;

use crate::ui::search_input_widget::SearchInputWidget;

#[derive(Default)]
pub struct RocketmqTuiApp {
should_quit: bool,
search_input: SearchInputWidget,
}

impl RocketmqTuiApp {
pub fn new() -> Self {
Self { should_quit: false }
Self {
should_quit: false,
search_input: Default::default(),
}

Check warning on line 44 in rocketmq-tui/src/rocketmq_tui_app.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/rocketmq_tui_app.rs#L41-L44

Added lines #L41 - L44 were not covered by tests
}

pub fn should_quit(&self) -> bool {
Expand Down Expand Up @@ -115,12 +121,7 @@
let command_args = middle_right_chunks[1];
let execute_command_result = middle_right_chunks[2];

frame.render_widget(
Block::default()
.borders(ratatui::widgets::Borders::ALL)
.title("Search"),
search,
);
frame.render_widget(&self.search_input, search);

Check warning on line 124 in rocketmq-tui/src/rocketmq_tui_app.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/rocketmq_tui_app.rs#L124

Added line #L124 was not covered by tests
frame.render_widget(
Block::default()
.borders(ratatui::widgets::Borders::ALL)
Expand Down
2 changes: 1 addition & 1 deletion rocketmq-tui/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
mod search_input_widget;
pub(crate) mod search_input_widget;
89 changes: 86 additions & 3 deletions rocketmq-tui/src/ui/search_input_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,95 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use crossterm::event::KeyCode;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Color;
use ratatui::style::Style;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::widgets::Block;
use ratatui::widgets::Paragraph;
use ratatui::widgets::Widget;

pub(crate) struct SearchInputWidget;
#[derive(Default)]
pub(crate) struct SearchInputWidget {
// Whether the widget is focused
focused: bool,

impl Widget for &mut SearchInputWidget {
fn render(self, area: Rect, buf: &mut Buffer) {}
// The input string
input: String,
}

impl SearchInputWidget {
pub fn new() -> Self {
Self {
focused: false,
input: String::new(),
}
}

Check warning on line 43 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L38-L43

Added lines #L38 - L43 were not covered by tests

pub fn set_input(&mut self, input: String) {
self.input = input;
}

Check warning on line 47 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L45-L47

Added lines #L45 - L47 were not covered by tests

pub fn get_input(&self) -> &str {
&self.input
}

Check warning on line 51 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L49-L51

Added lines #L49 - L51 were not covered by tests

pub fn get_input_mut(&mut self) -> &mut String {
&mut self.input
}

Check warning on line 55 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L53-L55

Added lines #L53 - L55 were not covered by tests

pub fn set_focus(&mut self, focused: bool) {
self.focused = focused;
}

Check warning on line 59 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L57-L59

Added lines #L57 - L59 were not covered by tests

pub fn is_focused(&self) -> bool {
self.focused
}

Check warning on line 63 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L61-L63

Added lines #L61 - L63 were not covered by tests

pub fn handle_key_event(&mut self, key: KeyCode) {
match key {
KeyCode::Char(c) => {
self.input.push(c);
}
KeyCode::Backspace => {
self.input.pop();
}
KeyCode::Enter => {
// Do nothing
}
_ => {}

Check warning on line 76 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L65-L76

Added lines #L65 - L76 were not covered by tests
}
}

Check warning on line 78 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L78

Added line #L78 was not covered by tests
}

impl Widget for &SearchInputWidget {
fn render(self, area: Rect, buf: &mut Buffer) {
let style = if self.focused {
Style::default().fg(Color::Yellow).bg(Color::Black)

Check warning on line 84 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L82-L84

Added lines #L82 - L84 were not covered by tests
} else {
Style::default().fg(Color::White).bg(Color::Black)

Check warning on line 86 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L86

Added line #L86 was not covered by tests
};

let block = Block::default()
.borders(ratatui::widgets::Borders::ALL)
.title("Search[Press s/S to focus]")
.border_style(style);
block.render(area, buf);

let inner_area = Rect {
x: area.x + 1,
y: area.y + 1,
width: area.width - 2,
height: area.height - 2,
};

let paragraph = Paragraph::new(Line::from(Span::styled(
self.input.as_str(),
Style::default().fg(Color::White).bg(Color::Black),
)));
paragraph.render(inner_area, buf);
}

Check warning on line 107 in rocketmq-tui/src/ui/search_input_widget.rs

View check run for this annotation

Codecov / codecov/patch

rocketmq-tui/src/ui/search_input_widget.rs#L89-L107

Added lines #L89 - L107 were not covered by tests
}
Loading