Skip to content
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
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions src/code_block_popup.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#[derive(Clone)]
pub struct CodeBlockPopup {
pub code_blocks: Vec<CodeBlock>,
pub filtered_blocks: Vec<CodeBlock>,
pub selected_index: usize,
pub visible: bool,
pub scroll_offset: usize,
}

#[derive(Clone)]
pub struct CodeBlock {
pub content: String,
pub language: String,
pub start_line: usize,
pub end_line: usize,
}

impl CodeBlock {
pub fn new(content: String, language: String, start_line: usize, end_line: usize) -> Self {
Self {
content,
language,
start_line,
end_line,
}
}
}

impl Default for CodeBlockPopup {
fn default() -> Self {
Self::new()
}
}

impl CodeBlockPopup {
pub fn new() -> Self {
CodeBlockPopup {
code_blocks: Vec::new(),
filtered_blocks: Vec::new(),
selected_index: 0,
visible: false,
scroll_offset: 0,
}
}

pub fn set_code_blocks(&mut self, code_blocks: Vec<CodeBlock>) {
self.code_blocks = code_blocks;
self.filtered_blocks = self.code_blocks.clone();
self.selected_index = 0;
self.scroll_offset = 0;
}

pub fn move_selection_up(&mut self, visible_items: usize) {
if self.filtered_blocks.is_empty() {
return;
}

if self.selected_index > 0 {
self.selected_index -= 1;
} else {
self.selected_index = self.filtered_blocks.len() - 1;
}

if self.selected_index <= self.scroll_offset {
self.scroll_offset = self.selected_index;
}
if self.selected_index == self.filtered_blocks.len() - 1 {
self.scroll_offset = self.filtered_blocks.len().saturating_sub(visible_items);
}
}

pub fn move_selection_down(&mut self, visible_items: usize) {
if self.filtered_blocks.is_empty() {
return;
}

if self.selected_index < self.filtered_blocks.len() - 1 {
self.selected_index += 1;
} else {
self.selected_index = 0;
self.scroll_offset = 0;
}

let max_scroll = self.filtered_blocks.len().saturating_sub(visible_items);
if self.selected_index >= self.scroll_offset + visible_items {
self.scroll_offset = (self.selected_index + 1).saturating_sub(visible_items);
if self.scroll_offset > max_scroll {
self.scroll_offset = max_scroll;
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_new_code_block_popup() {
let popup = CodeBlockPopup::new();
assert!(popup.code_blocks.is_empty());
assert_eq!(popup.selected_index, 0);
assert!(!popup.visible);
}

#[test]
fn test_set_code_blocks() {
let mut popup = CodeBlockPopup::new();
let blocks = vec![
CodeBlock::new("fn main() {}".to_string(), "rust".to_string(), 0, 2),
CodeBlock::new("def hello(): pass".to_string(), "python".to_string(), 3, 5),
];
popup.set_code_blocks(blocks);
assert_eq!(popup.code_blocks.len(), 2);
assert_eq!(popup.code_blocks[0].language, "rust");
assert_eq!(popup.code_blocks[1].language, "python");
}

#[test]
fn test_move_selection() {
let mut popup = CodeBlockPopup::new();
let blocks = vec![
CodeBlock::new("Block 1".to_string(), "rust".to_string(), 0, 2),
CodeBlock::new("Block 2".to_string(), "python".to_string(), 3, 5),
CodeBlock::new("Block 3".to_string(), "js".to_string(), 6, 8),
];
popup.set_code_blocks(blocks);

popup.move_selection_down(2);
assert_eq!(popup.selected_index, 1);

popup.move_selection_up(2);
assert_eq!(popup.selected_index, 0);

popup.move_selection_up(2);
assert_eq!(popup.selected_index, 2);

popup.move_selection_down(2);
assert_eq!(popup.selected_index, 0);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod cli;
pub mod clipboard;
pub mod code_block_popup;
pub mod config;
pub mod formatter;
pub mod markdown_renderer;
Expand All @@ -13,6 +14,7 @@ pub mod utils;

pub use clipboard::ClipboardTrait;
pub use clipboard::EditorClipboard;
pub use code_block_popup::CodeBlockPopup;
pub use config::{ThemeMode, ThothConfig};
use dirs::home_dir;
pub use formatter::{format_json, format_markdown};
Expand Down
82 changes: 81 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::code_block_popup::CodeBlockPopup;
use crate::{ThemeColors, TitlePopup, TitleSelectPopup, BORDER_PADDING_SIZE, DARK_MODE_COLORS};
use ratatui::style::Color;
use ratatui::widgets::Wrap;
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Modifier, Style},
Expand Down Expand Up @@ -133,6 +136,7 @@ pub fn render_header(f: &mut Frame, area: Rect, is_edit_mode: bool, theme: &Them
"^n:Add",
"^d:Del",
"^y:Copy",
"^c:Copy Code",
"^v:Paste",
"Enter:Edit",
"^f:Focus",
Expand Down Expand Up @@ -239,6 +243,82 @@ pub fn render_title_popup(f: &mut Frame, popup: &TitlePopup, theme: &ThemeColors
f.render_widget(text, area);
}

pub fn render_code_block_popup(f: &mut Frame, popup: &CodeBlockPopup, theme: &ThemeColors) {
if !popup.visible || popup.filtered_blocks.is_empty() {
return;
}

let area = centered_rect(80, 80, f.size());
f.render_widget(ratatui::widgets::Clear, area);

let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Length(3), Constraint::Min(1)])
.split(area);

let title_area = chunks[0];
let code_area = chunks[1];

let title_block = Block::default()
.borders(Borders::TOP | Borders::LEFT | Borders::RIGHT)
.border_style(Style::default().fg(theme.primary))
.title(format!(
"Code Block {}/{} [{}]",
popup.selected_index + 1,
popup.filtered_blocks.len(),
if !popup.filtered_blocks.is_empty() {
popup.filtered_blocks[popup.selected_index].language.clone()
} else {
String::new()
}
));

let title_text = vec![Line::from(vec![
Span::raw(" "),
Span::styled("↑/↓", Style::default().fg(theme.accent)),
Span::raw(": Navigate "),
Span::styled("Enter", Style::default().fg(theme.accent)),
Span::raw(": Copy "),
Span::styled("Esc", Style::default().fg(theme.accent)),
Span::raw(": Cancel"),
])];

let title_paragraph = Paragraph::new(title_text).block(title_block);

f.render_widget(title_paragraph, title_area);

if !popup.filtered_blocks.is_empty() {
let selected_block = &popup.filtered_blocks[popup.selected_index];

let code_content = selected_block.content.clone();
let _language = &selected_block.language;

let lines: Vec<Line> = code_content
.lines()
.enumerate()
.map(|(i, line)| {
Line::from(vec![
Span::styled(
format!("{:3} │ ", i + 1),
Style::default().fg(Color::DarkGray),
),
Span::raw(line),
])
})
.collect();

let code_block = Block::default()
.borders(Borders::LEFT | Borders::RIGHT | Borders::BOTTOM)
.border_style(Style::default().fg(theme.primary));

let code_paragraph = Paragraph::new(lines)
.block(code_block)
.wrap(Wrap { trim: false });

f.render_widget(code_paragraph, code_area);
}
}

pub fn render_title_select_popup(f: &mut Frame, popup: &TitleSelectPopup, theme: &ThemeColors) {
let area = centered_rect(80, 80, f.size());
f.render_widget(ratatui::widgets::Clear, area);
Expand Down Expand Up @@ -312,7 +392,7 @@ pub fn render_ui_popup(f: &mut Frame, popup: &UiPopup, theme: &ThemeColors) {
.border_style(Style::default().fg(theme.error))
.title(format!("{} - Esc to exit", popup.popup_title)),
)
.wrap(ratatui::widgets::Wrap { trim: true }); // Enable text wrapping
.wrap(ratatui::widgets::Wrap { trim: true });

f.render_widget(text, area);
}
Expand Down
Loading
Loading