Skip to content

Commit

Permalink
got select and copy working!
Browse files Browse the repository at this point in the history
  • Loading branch information
cosmikwolf committed Apr 6, 2024
1 parent ed81943 commit 9dfe5b3
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 23 deletions.
2 changes: 1 addition & 1 deletion sazid-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,8 @@ impl Application {
);

// session must be pushed after input in order for input not to overwrite style changes made in session
compositor.push(Box::new(markdown_session));
compositor.push(Box::new(input));
compositor.push(Box::new(markdown_session));

#[cfg(windows)]
let signals = futures_util::stream::empty();
Expand Down
90 changes: 81 additions & 9 deletions sazid-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ fn quit(cx: &mut Context) {
fn toggle_layer_order(cx: &mut Context) {
cx.callback.push(Box::new(
move |compositor: &mut Compositor, cx: &mut compositor::Context| {
cx.focus.toggle();
return;
match compositor.pop() {
Some(component_a) => match compositor.pop() {
Some(component_b) => {
Expand All @@ -635,8 +637,8 @@ fn toggle_layer_order(cx: &mut Context) {
);
cx.focus.toggle();

compositor.push(component_a);
compositor.push(component_b);
// compositor.push(component_a);
// compositor.push(component_b);
compositor.need_full_redraw();
},
None => {
Expand All @@ -648,7 +650,7 @@ fn toggle_layer_order(cx: &mut Context) {
None => {
log::info!("toggle_layer_order: no last component found");
},
}
};
},
))
}
Expand Down Expand Up @@ -4254,18 +4256,88 @@ fn commit_undo_checkpoint(cx: &mut Context) {
// Yank / Paste

fn yank(cx: &mut Context) {
yank_impl(cx.editor, cx.register.unwrap_or('"'));
exit_select_mode(cx);
log::info!("yank to clipboard");
let ctx_reg = cx.register.unwrap_or('"');
match cx.focus {
ContextFocus::SessionView => cx.callback.push(Box::new(
move |compositor: &mut Compositor, cx: &mut compositor::Context| {
let session =
compositor.find::<ui::SessionView<ChatMessageItem>>().unwrap();
yank_session_impl(session, cx.editor, ctx_reg);
// exit_select_mode(cx);
},
)),
ContextFocus::EditorView => {
yank_impl(cx.editor, cx.register.unwrap_or('"'));
exit_select_mode(cx);
},
}
}

fn yank_to_clipboard(cx: &mut Context) {
yank_impl(cx.editor, '+');
exit_select_mode(cx);
log::info!("yank to clipboard");
match cx.focus {
ContextFocus::SessionView => cx.callback.push(Box::new(
move |compositor: &mut Compositor, cx: &mut compositor::Context| {
let session =
compositor.find::<ui::SessionView<ChatMessageItem>>().unwrap();
yank_session_impl(session, cx.editor, '+');
// exit_select_mode(cx);
},
)),
ContextFocus::EditorView => {
yank_impl(cx.editor, '+');
exit_select_mode(cx);
},
}
}

fn yank_to_primary_clipboard(cx: &mut Context) {
yank_impl(cx.editor, '*');
exit_select_mode(cx);
match cx.focus {
ContextFocus::SessionView => cx.callback.push(Box::new(
move |compositor: &mut Compositor, cx: &mut compositor::Context| {
let session =
compositor.find::<ui::SessionView<ChatMessageItem>>().unwrap();
yank_session_impl(session, cx.editor, '*');
// exit_select_mode(cx);
},
)),
ContextFocus::EditorView => {
yank_impl(cx.editor, '*');
exit_select_mode(cx);
},
}
}

fn yank_session_impl(
session: &mut ui::SessionView<ChatMessageItem>,
editor: &mut Editor,
register: char,
) {
let text = session.get_messages_plaintext();
let range = session.selection.primary();
let (head, anchor) = if range.head < range.anchor {
(range.head, range.anchor)
} else {
(range.anchor, range.head)
};
let values: Vec<String> =
text.slice(head..anchor).lines().map(String::from).collect();
match editor.registers.write(register, values.clone()) {
Ok(_) => {
log::info!(
"session -yanked selection to register {}\n{:?}",
register,
values
);
editor.set_status(format!("yanked selection to register {register}",))
},
Err(err) => editor.set_error(err.to_string()),
}

if editor.mode == Mode::Select {
editor.mode = Mode::Normal;
}
}

fn yank_impl(editor: &mut Editor, register: char) {
Expand Down
2 changes: 1 addition & 1 deletion sazid-term/src/compositor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ impl Compositor {
cx: &mut Context,
) {
// reversed layer rendering so that the editor draws popup menus over the session view
for layer in &mut self.layers.iter_mut().rev() {
for layer in &mut self.layers.iter_mut() {
layer.render(area, surface, cx);
}
}
Expand Down
3 changes: 2 additions & 1 deletion sazid-term/src/keymap/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ pub fn minimal() -> HashMap<Mode, KeyTrie> {
"s" => save_session,
"l" => load_session_picker,
"t" => toggle_layer_order,

// "F" => file_picker_in_current_directory,
// "b" => buffer_picker,
// "j" => jumplist_picker,
Expand Down Expand Up @@ -279,7 +280,7 @@ pub fn minimal() -> HashMap<Mode, KeyTrie> {
"C-v" | "v" => vsplit_new,
},
},
// "y" => yank_to_clipboard,
"y" => yank_to_clipboard,
// "Y" => yank_main_selection_to_clipboard,
// "p" => paste_clipboard_after,
// "P" => paste_clipboard_before,
Expand Down
2 changes: 1 addition & 1 deletion sazid-term/src/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub fn session_move_horizontally(
Direction::Forward => pos + count,
Direction::Backward => pos.saturating_sub(count),
}
.clamp(original_row_start, original_row_end);
.clamp(0, all_messages_text.len_chars() - 1);

log::warn!("move_horizontally original_pos: {}, new_pos: {}", pos, new_pos);
// Compute the final new range.
Expand Down
12 changes: 2 additions & 10 deletions sazid-term/src/ui/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,19 +793,11 @@ impl<T: MarkdownItem + 'static> SessionView<T> {
.with_block(Block::default())
.with_char_index(message.start_idx);

let debug_msg_ct = message.start_idx.to_string();
let index_cell = MessageCell::new(MessageType::Text(debug_msg_ct))
let msg_idx = msg_idx.to_string();
let index_cell = MessageCell::new(MessageType::Text(msg_idx))
.centered()
.with_block(Block::default().borders(Borders::RIGHT));

// log::info!(
// "message length: chars: {} lines: {} idx: {} start_idx: {}",
// message.plain_text.len_chars(),
// message.plain_text.len_lines(),
// msg_idx,
// start_idx
// );
//
Row::new(vec![index_cell, message_cell])
.height(message.plain_text.len_lines() as u16)
})
Expand Down

0 comments on commit 9dfe5b3

Please sign in to comment.