quetty 0.1.9

Terminal-based Azure Service Bus queue manager with intuitive TUI interface
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
use crate::components::base_popup::PopupBuilder;
use crate::components::common::{Msg, QueueType};
use crate::components::messages::rendering::{
    build_table_from_messages, calculate_responsive_layout, format_delivery_count_responsive,
    get_state_color, get_state_display,
};
use crate::components::messages::selection::{format_pagination_status, format_queue_display};
use crate::theme::ThemeManager;
use quetty_server::bulk_operations::MessageIdentifier;
use quetty_server::model::MessageModel;
use tui_realm_stdlib::Table;
use tuirealm::command::{Cmd, CmdResult};
use tuirealm::event::{Key, KeyEvent};
use tuirealm::props::{Alignment, BorderType, Borders, Color, Style};
use tuirealm::ratatui::layout::{Alignment as RatatuiAlignment, Constraint, Rect};
use tuirealm::ratatui::style::{Color as RatatuiColor, Modifier, Style as RatatuiStyle};
use tuirealm::ratatui::widgets::{Cell, Paragraph, Row, Table as RatatuiTable, TableState};
use tuirealm::{
    AttrValue, Attribute, Component, Event, Frame, MockComponent, NoUserEvent, State, StateValue,
};

/// Information about message list pagination and queue state.
///
/// Contains all the necessary information for displaying pagination controls,
/// bulk selection status, and queue statistics in the message list component.
#[derive(Debug, Clone)]
pub struct PaginationInfo {
    /// Current page number (0-based)
    pub current_page: usize,
    /// Total number of pages loaded so far
    pub total_pages_loaded: usize,
    /// Total number of messages loaded
    pub total_messages_loaded: usize,
    /// Number of messages per page
    pub current_page_size: usize,
    /// Whether there are more pages available
    pub has_next_page: bool,
    /// Whether there are previous pages available
    pub has_previous_page: bool,
    /// Name of the current queue
    pub queue_name: Option<String>,
    /// Type of the current queue (Main or DeadLetter)
    pub queue_type: QueueType,
    /// Whether bulk selection mode is active
    pub bulk_mode: bool,
    /// Number of messages currently selected
    pub selected_count: usize,
    /// Total number of messages in the queue (if available)
    pub queue_total_messages: Option<u64>,
    /// Age of queue statistics in seconds (if available)
    pub queue_stats_age_seconds: Option<i64>,
}

pub struct Messages {
    component: Table,
    // Store data for direct rendering
    messages: Option<Vec<MessageModel>>,
    pagination_info: Option<PaginationInfo>,
    selected_messages: Vec<MessageIdentifier>,
    title: String,
    headers: Vec<String>,
    widths: Vec<u16>,
    is_focused: bool,    // Track focus state for border styling
    narrow_layout: bool, // Track if we're using narrow layout for responsive formatting
}

pub const CMD_RESULT_MESSAGE_SELECTED: &str = "MessageSelected";
pub const CMD_RESULT_MESSAGE_PREVIEW: &str = "MessagePreview";
pub const CMD_RESULT_QUEUE_UNSELECTED: &str = "QueueUnSelected";

/// Get current index from table state
fn get_current_index_from_state(state: &State) -> usize {
    match state {
        State::One(StateValue::Usize(index)) => *index,
        _ => 0,
    }
}

impl Messages {
    pub fn new(messages: Option<&Vec<MessageModel>>) -> Self {
        Self::new_with_pagination(messages, None)
    }

    pub fn new_with_pagination(
        messages: Option<&Vec<MessageModel>>,
        pagination_info: Option<PaginationInfo>,
    ) -> Self {
        Self::new_with_pagination_and_selections(messages, pagination_info, Vec::new())
    }

    pub fn new_with_pagination_and_selections(
        messages: Option<&Vec<MessageModel>>,
        pagination_info: Option<PaginationInfo>,
        selected_messages: Vec<MessageIdentifier>,
    ) -> Self {
        Self::new_with_pagination_selections_and_focus(
            messages,
            pagination_info,
            selected_messages,
            false,
        )
    }

    pub fn new_with_pagination_selections_and_focus(
        messages: Option<&Vec<MessageModel>>,
        pagination_info: Option<PaginationInfo>,
        selected_messages: Vec<MessageIdentifier>,
        is_focused: bool,
    ) -> Self {
        // Simplified title - just show queue info, no pagination details
        let title = if let Some(info) = &pagination_info {
            let queue_display = format_queue_display(info);
            format!(" {queue_display} ")
        } else {
            " Messages ".to_string()
        };

        let (headers, widths, use_narrow_layout) = calculate_responsive_layout(
            120, // Default width, will be recalculated in view()
            pagination_info.as_ref().is_some_and(|info| info.bulk_mode),
        );

        let component = {
            Table::default()
                .borders(
                    Borders::default()
                        .modifiers(BorderType::Rounded)
                        .color(ThemeManager::primary_accent()),
                )
                .background(Color::Reset)
                .foreground(ThemeManager::text_primary())
                .title(&title, Alignment::Center)
                .scroll(true)
                .highlighted_color(ThemeManager::selection_bg())
                .highlighted_str("")
                .rewind(false)
                .step(4)
                .row_height(1)
                .headers(headers.iter().map(|s| s.as_str()).collect::<Vec<_>>())
                .column_spacing(2)
                .widths(&widths)
                .table(build_table_from_messages(
                    messages,
                    pagination_info.as_ref(),
                    &selected_messages,
                    &widths,
                    use_narrow_layout,
                ))
        };

        Self {
            component,
            messages: messages.cloned(),
            pagination_info,
            selected_messages,
            title,
            headers,
            widths,
            is_focused,
            narrow_layout: use_narrow_layout,
        }
    }

    /// Get the current selection index
    pub fn current_index(&self) -> usize {
        get_current_index_from_state(&self.component.state())
    }

    /// Get the number of messages currently available
    pub fn message_count(&self) -> usize {
        self.messages.as_ref().map_or(0, |msgs| msgs.len())
    }

    /// Get pagination info for external access
    pub fn pagination_info(&self) -> &Option<PaginationInfo> {
        &self.pagination_info
    }

    /// Get component for internal operations (used by navigation module)
    pub(super) fn component_mut(&mut self) -> &mut Table {
        &mut self.component
    }

    /// Get component for read-only access
    pub(super) fn component(&self) -> &Table {
        &self.component
    }

    /// Get messages for internal operations
    pub(super) fn messages(&self) -> &Option<Vec<MessageModel>> {
        &self.messages
    }

    /// Get selected messages for internal operations
    pub(super) fn selected_messages(&self) -> &Vec<MessageIdentifier> {
        &self.selected_messages
    }

    /// Get headers for internal operations
    pub(super) fn headers(&self) -> &Vec<String> {
        &self.headers
    }

    /// Set headers for internal operations
    pub(super) fn set_headers(&mut self, headers: Vec<String>) {
        self.headers = headers;
    }

    /// Get widths for internal operations
    pub(super) fn widths(&self) -> &Vec<u16> {
        &self.widths
    }

    /// Set widths for internal operations
    pub(super) fn set_widths(&mut self, widths: Vec<u16>) {
        self.widths = widths;
    }

    /// Get title for internal operations
    pub(super) fn title(&self) -> &String {
        &self.title
    }

    /// Get focus state for internal operations
    pub(super) fn is_focused(&self) -> bool {
        self.is_focused
    }

    /// Set narrow layout flag for internal operations
    pub(super) fn set_narrow_layout(&mut self, narrow_layout: bool) {
        self.narrow_layout = narrow_layout;
    }
}

impl Component<Msg, NoUserEvent> for Messages {
    fn on(&mut self, ev: Event<NoUserEvent>) -> Option<Msg> {
        match ev {
            Event::Keyboard(KeyEvent { code: Key::Esc, .. }) => {
                // If no messages are selected, and we are in bulk mode, exit bulk mode
                if self
                    .pagination_info
                    .as_ref()
                    .is_some_and(|info| info.bulk_mode)
                    && self.selected_messages.is_empty()
                {
                    return Some(Msg::MessageActivity(
                        crate::components::common::MessageActivityMsg::ClearAllSelections,
                    ));
                }
                // If no messages are selected, and we are not in bulk mode, exit queue
                if self.selected_messages.is_empty() {
                    return Some(Msg::QueueActivity(
                        crate::components::common::QueueActivityMsg::ExitQueueConfirmation,
                    ));
                }
                // Otherwise, delegate to event handling module
                super::event_handling::handle_event(self, ev)
            }
            _ => super::event_handling::handle_event(self, ev),
        }
    }
}

impl MockComponent for Messages {
    fn view(&mut self, frame: &mut Frame, area: Rect) {
        // Recalculate responsive layout based on actual available width
        let bulk_mode = self
            .pagination_info()
            .as_ref()
            .is_some_and(|info| info.bulk_mode);

        let (headers, widths, narrow_layout) = calculate_responsive_layout(area.width, bulk_mode);

        // Update stored layout info
        self.set_headers(headers);
        self.set_widths(widths.clone());
        self.set_narrow_layout(narrow_layout);

        // Get the current selection from the internal component
        let table_state = self.component().state();
        let selected_index = get_current_index_from_state(&table_state);

        let mut rows = Vec::new();
        if let Some(messages) = self.messages() {
            for (i, msg) in messages.iter().enumerate() {
                let mut cells = Vec::new();

                if bulk_mode {
                    // Add checkbox column in bulk mode
                    let message_id = MessageIdentifier::from_message(msg);
                    let checkbox_text = if self.selected_messages().contains(&message_id) {
                        ""
                    } else {
                        ""
                    };
                    cells.push(Cell::from(checkbox_text));
                }

                // Add the message data cells with proper theming
                cells.push(
                    Cell::from(msg.sequence.to_string())
                        .style(RatatuiStyle::default().fg(ThemeManager::message_sequence())),
                );
                cells.push(
                    Cell::from(msg.id.to_string())
                        .style(RatatuiStyle::default().fg(ThemeManager::message_id())),
                );
                cells.push(
                    Cell::from(msg.enqueued_at.to_string())
                        .style(RatatuiStyle::default().fg(ThemeManager::message_timestamp())),
                );
                cells.push(
                    Cell::from(get_state_display(&msg.state))
                        .style(RatatuiStyle::default().fg(get_state_color(&msg.state))),
                );

                let delivery_width = widths[if bulk_mode { 5 } else { 4 }];
                cells.push(
                    Cell::from(format_delivery_count_responsive(
                        msg.delivery_count,
                        delivery_width as usize,
                    ))
                    .style(RatatuiStyle::default().fg(ThemeManager::message_delivery_count())),
                );

                let mut row = Row::new(cells);

                // Apply selection highlighting
                if i == selected_index {
                    row = row.style(
                        RatatuiStyle::default()
                            .bg(ThemeManager::selection_bg())
                            .fg(ThemeManager::selection_fg()),
                    );
                }

                rows.push(row);
            }
        }

        // Create the table headers with proper theming
        let header_cells: Vec<Cell> = self
            .headers()
            .iter()
            .map(|h| {
                Cell::from(h.as_str()).style(
                    RatatuiStyle::default()
                        .fg(ThemeManager::header_accent()) // Always yellow to match line numbers
                        .add_modifier(Modifier::BOLD),
                )
            })
            .collect();

        let header = Row::new(header_cells).height(1);

        // Create the table widget with proper theming
        let table = RatatuiTable::new(
            rows,
            &self
                .widths()
                .iter()
                .map(|&w| Constraint::Length(w))
                .collect::<Vec<_>>(),
        )
        .header(header)
        .block(
            // Use PopupBuilder for consistent styling with conditional focus state
            PopupBuilder::new("Messages Table").create_conditional_block(
                self.title().as_str(),
                self.is_focused(),
                ThemeManager::primary_accent(), // Teal when focused
                RatatuiColor::White,            // White when not focused
            ),
        )
        .column_spacing(2)
        .row_highlight_style(
            RatatuiStyle::default()
                .bg(ThemeManager::selection_bg())
                .fg(ThemeManager::selection_fg())
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol("");

        // Create table state for selection
        let mut table_state = TableState::default();
        table_state.select(Some(selected_index));

        // Render the table
        frame.render_stateful_widget(table, area, &mut table_state);

        // Create status bar overlay at the bottom with pagination info
        if let Some(info) = self.pagination_info() {
            let status_text = format_pagination_status(info);
            let status_bar = Paragraph::new(status_text)
                .style(
                    Style::default().fg(if self.is_focused() {
                        ThemeManager::primary_accent() // Teal text when focused
                    } else {
                        RatatuiColor::White // White text when not focused
                    }), // No background - clean and transparent
                )
                .alignment(RatatuiAlignment::Center);

            // Position status bar at the exact same height as table bottom border
            let status_area = Rect {
                x: area.x,
                y: area.y + area.height.saturating_sub(1),
                width: area.width,
                height: 1,
            };

            frame.render_widget(status_bar, status_area);
        }
    }

    fn query(&self, attr: Attribute) -> Option<AttrValue> {
        self.component().query(attr)
    }

    fn attr(&mut self, attr: Attribute, value: AttrValue) {
        match attr {
            Attribute::Custom("cursor_position") => {
                if let AttrValue::Number(position) = value {
                    log::debug!("Received cursor position attribute: {position}");
                    let target_position = position as usize;
                    let max_index = self.message_count().saturating_sub(1);

                    // Ensure target position is within bounds
                    let bounded_position = target_position.min(max_index);

                    // Reset to beginning first
                    let current = self.current_index();
                    for _ in 0..current {
                        self.move_up();
                    }

                    // Move to target position using bounds-checked movement
                    for _ in 0..bounded_position {
                        self.move_down();
                    }

                    log::debug!(
                        "Moved cursor to position: {bounded_position} (requested: {target_position})"
                    );
                }
            }
            _ => {
                self.component_mut().attr(attr, value);
            }
        }
    }

    fn state(&self) -> State {
        self.component().state()
    }

    fn perform(&mut self, cmd: Cmd) -> CmdResult {
        self.component_mut().perform(cmd)
    }
}