1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
use ratatui::{
buffer::Buffer,
layout::Rect,
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Clear, Paragraph, Widget},
};
use super::widgets::panel_contrast_fg;
use crate::{
app::state::{CopyFeedback, Palette},
config::ToastClipboardPosition,
};
pub(crate) fn copy_feedback_rect(
area: Rect,
feedback: &CopyFeedback,
offset_rows: u16,
position: ToastClipboardPosition,
) -> Rect {
if area.is_empty() {
return Rect::default();
}
let content_width = feedback.message.len() as u16 + 4;
let width = content_width.min(area.width);
let height = 3u16.min(area.height);
let x = match position {
ToastClipboardPosition::TopLeft | ToastClipboardPosition::BottomLeft => area.x,
ToastClipboardPosition::TopCenter | ToastClipboardPosition::BottomCenter => {
area.x + area.width.saturating_sub(width) / 2
}
ToastClipboardPosition::TopRight | ToastClipboardPosition::BottomRight => {
area.x + area.width.saturating_sub(width)
}
};
let y = match position {
ToastClipboardPosition::TopLeft
| ToastClipboardPosition::TopCenter
| ToastClipboardPosition::TopRight => area.y + offset_rows.min(area.height),
ToastClipboardPosition::BottomLeft
| ToastClipboardPosition::BottomCenter
| ToastClipboardPosition::BottomRight => {
area.y + area.height.saturating_sub(height + offset_rows)
}
};
Rect::new(x, y, width, height)
}
pub(crate) fn render_copy_feedback_buffer(
buffer: &mut Buffer,
area: Rect,
feedback: &CopyFeedback,
offset_rows: u16,
position: ToastClipboardPosition,
palette: &Palette,
) -> Rect {
let feedback_area = copy_feedback_rect(area, feedback, offset_rows, position);
if feedback_area.is_empty() {
return feedback_area;
}
Clear.render(feedback_area, buffer);
let block = Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(palette.green))
.style(Style::default().bg(palette.panel_bg));
let inner = block.inner(feedback_area);
block.render(feedback_area, buffer);
if inner.height == 0 {
return feedback_area;
}
let text = Line::from(vec![
Span::styled("●", Style::default().fg(palette.green).bg(palette.panel_bg)),
Span::raw(" "),
Span::styled(
&feedback.message,
Style::default()
.fg(palette.text)
.bg(palette.panel_bg)
.add_modifier(Modifier::BOLD),
),
]);
Paragraph::new(text).render(inner, buffer);
feedback_area
}
pub(crate) fn render_config_diagnostic_buffer(
buffer: &mut Buffer,
area: Rect,
message: &str,
palette: &Palette,
mut covered: impl FnMut(Rect),
) -> u16 {
let style = Style::default()
.fg(panel_contrast_fg(palette))
.bg(palette.yellow)
.add_modifier(Modifier::BOLD);
let mut rendered_rows = 0u16;
for (row, line) in message
.lines()
.filter(|line| !line.trim().is_empty())
.take(area.height as usize)
.enumerate()
{
let text = format!(" {line} ");
let width = (text.len() as u16).min(area.width);
let diagnostic_area = Rect::new(
area.x + area.width.saturating_sub(width),
area.y + row as u16,
width,
1,
);
covered(diagnostic_area);
Clear.render(diagnostic_area, buffer);
Paragraph::new(Span::styled(text, style)).render(diagnostic_area, buffer);
rendered_rows = rendered_rows.saturating_add(1);
}
rendered_rows
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn copy_feedback_rect_uses_configured_position() {
let area = Rect::new(10, 20, 100, 40);
let feedback = CopyFeedback {
message: "copied to clipboard".to_owned(),
};
let top = copy_feedback_rect(area, &feedback, 0, ToastClipboardPosition::TopCenter);
assert_eq!(top.y, area.y);
assert_eq!(top.x, area.x + area.width.saturating_sub(top.width) / 2);
let bottom = copy_feedback_rect(area, &feedback, 0, ToastClipboardPosition::BottomCenter);
assert_eq!(bottom.bottom(), area.bottom());
assert_eq!(
bottom.x,
area.x + area.width.saturating_sub(bottom.width) / 2
);
}
}
|