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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
use super::app::AppState;
use ratatui::{
Frame,
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Line, Span, Text},
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
};
use unicode_width::UnicodeWidthStr;
pub fn draw(f: &mut Frame, state: &mut AppState) {
let area = f.area();
// Fill background
f.render_widget(Block::default().style(Style::default()), area);
let outer = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(1), // title bar
Constraint::Min(0), // body
Constraint::Length(1), // status bar
])
.split(area);
draw_titlebar(f, outer[0], state);
draw_body(f, outer[1], state);
draw_statusbar(f, outer[2], state);
}
fn draw_titlebar(f: &mut Frame, area: Rect, state: &AppState) {
let title = Line::from(vec![
Span::styled(
" ",
Style::default()
.fg(Color::Green)
.add_modifier(Modifier::BOLD),
),
Span::styled("speakez", Style::default().add_modifier(Modifier::BOLD)),
Span::styled(" │ ", Style::default()),
Span::styled(
&state.channel,
Style::default().add_modifier(Modifier::BOLD),
),
Span::styled(" │ ", Style::default()),
Span::styled(&state.nick, Style::default().fg(Color::Green)),
]);
f.render_widget(Paragraph::new(title).style(Style::default()), area);
}
fn draw_body(f: &mut Frame, area: Rect, state: &mut AppState) {
// Body: [chat (fill)] | [members (18)]
let cols = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Min(0), // centre: chat log + input
Constraint::Length(18), // right: member list
])
.split(area);
draw_center(f, cols[0], state);
draw_members_panel(f, cols[1], state);
}
fn draw_center(f: &mut Frame, area: Rect, state: &mut AppState) {
let inner_width = area.width.saturating_sub(2) as usize;
// Build the same Line that draw_input will render
let input_line = Line::from(vec![
Span::raw(state.input.clone()),
Span::raw(" "), // account for cursor character
]);
let wrapped = count_wrapped_lines(&[input_line], inner_width);
let input_height = (wrapped as u16 + 2).max(3); // +2 borders, min 3
let rows = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(input_height)])
.split(area);
draw_chat_log(f, rows[0], state);
draw_input(f, rows[1], state);
}
fn draw_chat_log(f: &mut Frame, area: Rect, state: &mut AppState) {
let lines: Vec<Line> = state
.messages
.iter()
.map(|msg| render_chat_line(msg))
.collect();
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(Style::default().fg(Color::Green))
.style(Style::default());
let inner_width = area.width.saturating_sub(2) as usize;
let inner_height = area.height.saturating_sub(2) as usize;
let total_wrapped = count_wrapped_lines(&lines, inner_width);
let (padded_lines, base_scroll) = if total_wrapped < inner_height {
let padding = inner_height - total_wrapped;
let mut padded = vec![Line::raw(""); padding];
padded.extend(lines);
(padded, 0u16)
} else {
let scroll = total_wrapped.saturating_sub(inner_height);
(lines, scroll as u16)
};
// Max scrollable lines upward from the natural bottom position
let max_offset = base_scroll as usize;
// Clamp the offset and write it back so app.rs stays in sync
state.scroll_offset = state.scroll_offset.clamp(0, max_offset);
let final_scroll = (base_scroll as i32 - state.scroll_offset as i32) as u16;
f.render_widget(
Paragraph::new(Text::from(padded_lines))
.block(block)
.wrap(Wrap { trim: false })
.scroll((final_scroll, 0)),
area,
);
}
fn count_wrapped_lines(lines: &[Line], width: usize) -> usize {
if width == 0 {
return lines.len();
}
lines
.iter()
.map(|line| {
let full_text: String = line
.spans
.iter()
.map(|span| span.content.as_ref())
.collect();
if full_text.is_empty() {
return 1;
}
let mut row_count = 1;
let mut current_width = 0;
for word in full_text.split_inclusive(' ') {
let word_width = UnicodeWidthStr::width(word);
if current_width + word_width > width {
row_count += 1;
current_width = word_width;
} else {
current_width += word_width;
}
}
row_count
})
.sum()
}
fn render_chat_line(msg: &super::app::ChatLine) -> Line<'static> {
if msg.is_system {
return Line::from(Span::styled(
format!(" ∙ {}", msg.text),
Style::default()
.fg(Color::DarkGray)
.add_modifier(Modifier::DIM),
));
}
let nick_style = if msg.is_notice {
Style::default().fg(Color::Green)
} else {
Style::default()
.fg(color_from_str(&msg.nick))
.add_modifier(Modifier::BOLD)
};
let nick = format!("{}", msg.nick);
Line::from(vec![
Span::styled(nick, nick_style),
Span::styled(" ", Style::default()),
Span::styled(msg.text.clone(), Style::default()),
])
}
fn color_from_str(s: &str) -> Color {
let sum: u32 = s.chars().map(|c| c as u32).sum();
match sum % 6 {
0 => Color::Red,
1 => Color::Green,
2 => Color::Yellow,
3 => Color::Blue,
4 => Color::Magenta,
5 => Color::Cyan,
_ => unreachable!(),
}
}
fn draw_input(f: &mut Frame, area: Rect, state: &AppState) {
// Show a blinking cursor indicator at the cursor position
let before = &state.input[..state.cursor];
let after = &state.input[state.cursor..];
let cursor_char = if after.is_empty() {
" "
} else {
&after[..after.chars().next().map(|c| c.len_utf8()).unwrap_or(1)]
};
let after_cursor = if after.is_empty() {
""
} else {
&after[cursor_char.len()..]
};
let line = Line::from(vec![
Span::styled(before.to_string(), Style::default()),
Span::styled(
cursor_char.to_string(),
Style::default()
.bg(Color::White)
.add_modifier(Modifier::BOLD),
),
Span::styled(after_cursor.to_string(), Style::default()),
]);
let block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(Style::default().fg(Color::Green))
.title(Span::styled(" message ", Style::default()));
f.render_widget(
Paragraph::new(line).block(block).wrap(Wrap { trim: false }),
area,
);
}
fn draw_members_panel(f: &mut Frame, area: Rect, state: &AppState) {
let items: Vec<ListItem> = state
.members
.iter()
.map(|nick| {
// Highlight ops (@) differently
let (sigil, rest) = if nick.starts_with('@') {
("@", &nick[1..])
} else if nick.starts_with('+') {
("+", &nick[1..])
} else {
("", nick.as_str())
};
let sigil_style = if sigil == "@" {
Style::default().fg(Color::Green)
} else {
Style::default()
};
ListItem::new(Line::from(vec![
Span::styled(sigil.to_string(), sigil_style),
Span::styled(
rest.to_string(),
Style::default()
.fg(color_from_str(nick.as_str()))
.add_modifier(Modifier::BOLD),
),
]))
})
.collect();
let title = format!(" users ({}) ", state.members.len());
let block = panel_block(&title);
f.render_widget(List::new(items).block(block), area);
}
fn draw_statusbar(f: &mut Frame, area: Rect, state: &AppState) {
let (status_text, status_style) = if state.connected {
("● connected", Style::default().fg(Color::LightGreen))
} else {
("○ connecting…", Style::default().fg(Color::Gray))
};
let line = Line::from(vec![
Span::styled(" ", Style::default()),
Span::styled(status_text, status_style),
Span::styled(" │ ", Style::default()),
Span::styled(&state.status, Style::default()),
Span::styled(" │ ", Style::default()),
Span::styled("Ctrl-C quit", Style::default()),
]);
f.render_widget(Paragraph::new(line).style(Style::default()), area);
}
/// Consistent panel block style
fn panel_block(title: &str) -> Block<'static> {
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Plain)
.border_style(Style::default().fg(Color::Green))
.title(Span::styled(format!(" {} ", title), Style::default()))
.style(Style::default())
}
|