Skip to content

Commit 0f17b94

Browse files
committed
Add maximum number of lines for input.
1 parent 260b3ce commit 0f17b94

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

src/buffer/input_view.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use iced::widget::{
2222
self, button, column, container, operation, row, rule, text_editor,
2323
};
2424
use iced::{Alignment, Length, Task, clipboard, event, keyboard, padding};
25+
use itertools::Itertools;
2526
use tokio::time;
2627

2728
use self::completion::Completion;
@@ -34,6 +35,8 @@ use crate::{Theme, font, theme, window};
3435

3536
mod completion;
3637

38+
const MAX_LINE_COUNT: usize = 20;
39+
3740
pub enum Event {
3841
InputSent {
3942
history_task: Task<history::manager::Message>,
@@ -348,7 +351,8 @@ pub fn view<'a>(
348351
iced::keyboard::Key::Named(
349352
iced::keyboard::key::Named::Enter,
350353
) if key_press.modifiers.shift() => {
351-
Some(text_editor::Binding::Enter)
354+
(state.input_content.line_count() < MAX_LINE_COUNT)
355+
.then_some(text_editor::Binding::Enter)
352356
}
353357
// Send
354358
iced::keyboard::Key::Named(
@@ -1074,7 +1078,33 @@ impl State {
10741078
(task, None)
10751079
}
10761080
Message::Action(action) => {
1077-
self.input_content.perform(action.clone());
1081+
if let text_editor::Action::Edit(text_editor::Edit::Paste(
1082+
clipboard,
1083+
)) = &action
1084+
{
1085+
println!(
1086+
"MAX_LINE_COUNT {MAX_LINE_COUNT:?} self.input_content.line_count() {:?} take {:?}",
1087+
self.input_content.line_count(),
1088+
MAX_LINE_COUNT
1089+
.saturating_sub(self.input_content.line_count(),)
1090+
+ 1
1091+
);
1092+
let truncated_clipboard = clipboard
1093+
.lines()
1094+
.take(
1095+
MAX_LINE_COUNT.saturating_sub(
1096+
self.input_content.line_count(),
1097+
) + 1,
1098+
)
1099+
.join("\n");
1100+
let action =
1101+
text_editor::Action::Edit(text_editor::Edit::Paste(
1102+
std::sync::Arc::new(truncated_clipboard),
1103+
));
1104+
self.input_content.perform(action);
1105+
} else {
1106+
self.input_content.perform(action.clone());
1107+
}
10781108

10791109
match &action {
10801110
text_editor::Action::Edit(_) => {

0 commit comments

Comments
 (0)