oxur-cli 0.2.1

CLI infrastructure and unified command-line tool for Oxur
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//! Styled table rendering for Oxur tools
//!
//! Provides a flexible table builder with TOML-based theming for terminal output.
//!
//! # Examples
//!
//! ## Basic Usage
//!
//! ```no_run
//! use oxur_cli::table::{OxurTable, Tabled};
//!
//! #[derive(Tabled)]
//! struct Employee {
//!     #[tabled(rename = "Name")]
//!     name: String,
//!     #[tabled(rename = "Age")]
//!     age: u32,
//!     #[tabled(rename = "Role")]
//!     role: String,
//! }
//!
//! let employees = vec![
//!     Employee { name: "Alice".into(), age: 30, role: "Engineer".into() },
//!     Employee { name: "Bob".into(), age: 25, role: "Designer".into() },
//! ];
//!
//! let table = OxurTable::new(employees).render();
//! println!("{}", table);
//! ```
//!
//! ## With Title and Footer
//!
//! ```no_run
//! use oxur_cli::table::{OxurTable, Tabled};
//!
//! #[derive(Tabled)]
//! struct Row {
//!     #[tabled(rename = "ID")]
//!     id: u32,
//!     #[tabled(rename = "Name")]
//!     name: String,
//! }
//!
//! let data = vec![
//!     Row { id: 1, name: "Alice".into() },
//!     Row { id: 2, name: "Bob".into() },
//! ];
//!
//! let table = OxurTable::new(data)
//!     .with_title("USER LIST")
//!     .with_footer()
//!     .render();
//! println!("{}", table);
//! ```

use tabled::Table;

pub mod config;
pub mod helpers;
mod themes;

pub use config::TableStyleConfig;
pub use tabled::Tabled; // Re-export for convenience

// Re-exports for advanced usage (manual table building and cell coloring)
pub use tabled::builder::Builder;
pub use tabled::settings::object::Cell;
pub use tabled::settings::Color as TabledColor;

/// A themed table builder for terminal output
///
/// Creates tables with the default Oxur theme (warm orange sunset colors).
/// Supports any data type that implements `Tabled`.
///
/// The table can optionally include:
/// - A **title row** at the top (styled with the theme's title colors)
/// - A **footer row** at the bottom (styled with the theme's footer colors)
pub struct OxurTable<T: Tabled> {
    data: Vec<T>,
    theme: TableStyleConfig,
    title: Option<String>,
    has_footer: bool,
}

impl<T: Tabled> OxurTable<T> {
    /// Create a new table with data, using the default Oxur theme
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use oxur_cli::table::{OxurTable, Tabled};
    ///
    /// #[derive(Tabled)]
    /// struct Row {
    ///     #[tabled(rename = "ID")]
    ///     id: u32,
    ///     #[tabled(rename = "Name")]
    ///     name: String,
    /// }
    ///
    /// let data = vec![
    ///     Row { id: 1, name: "Alice".into() },
    ///     Row { id: 2, name: "Bob".into() },
    /// ];
    ///
    /// let table = OxurTable::new(data);
    /// ```
    pub fn new(data: Vec<T>) -> Self {
        Self { data, theme: TableStyleConfig::default(), title: None, has_footer: false }
    }

    /// Add a title row at the top of the table
    ///
    /// The title will span all columns and be styled with the theme's title colors.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use oxur_cli::table::{OxurTable, Tabled};
    ///
    /// #[derive(Tabled)]
    /// struct Row { name: String }
    ///
    /// let data = vec![Row { name: "Test".into() }];
    /// let table = OxurTable::new(data)
    ///     .with_title("MY TABLE")
    ///     .render();
    /// ```
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Add a blank footer row at the bottom of the table
    ///
    /// The footer provides visual closure and is styled with the theme's footer colors.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use oxur_cli::table::{OxurTable, Tabled};
    ///
    /// #[derive(Tabled)]
    /// struct Row { name: String }
    ///
    /// let data = vec![Row { name: "Test".into() }];
    /// let table = OxurTable::new(data)
    ///     .with_footer()
    ///     .render();
    /// ```
    pub fn with_footer(mut self) -> Self {
        self.has_footer = true;
        self
    }

    /// Render the table as a styled string for terminal output
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use oxur_cli::table::{OxurTable, Tabled};
    ///
    /// #[derive(Tabled)]
    /// struct Row {
    ///     name: String,
    /// }
    ///
    /// let data = vec![Row { name: "Test".into() }];
    /// let output = OxurTable::new(data).render();
    /// println!("{}", output);
    /// ```
    pub fn render(self) -> String {
        // If no title and no footer, use the simple path
        if self.title.is_none() && !self.has_footer {
            let mut table = Table::new(&self.data);
            self.theme.apply_to_table::<T>(&mut table);
            return table.to_string();
        }

        // Use Builder to add title and/or footer rows to the data
        let col_count = T::headers().len();
        let mut builder = Builder::default();

        // Add title row (first column has title, rest are empty)
        if let Some(ref title) = self.title {
            let mut title_row = vec![title.clone()];
            title_row.extend(std::iter::repeat_n(String::new(), col_count.saturating_sub(1)));
            builder.push_record(title_row);
        }

        // Add header row (column names from Tabled)
        let headers: Vec<String> = T::headers().iter().map(|h| h.to_string()).collect();
        builder.push_record(headers);

        // Add data rows
        for item in &self.data {
            let fields: Vec<String> = T::fields(item).iter().map(|f| f.to_string()).collect();
            builder.push_record(fields);
        }

        // Add footer row (all empty)
        if self.has_footer {
            let footer_row: Vec<String> = std::iter::repeat_n(String::new(), col_count).collect();
            builder.push_record(footer_row);
        }

        let mut table = builder.build();
        self.theme.apply_to_table::<T>(&mut table);
        table.to_string()
    }
}

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

    #[derive(Tabled)]
    struct TestRow {
        #[tabled(rename = "ID")]
        id: u32,
        #[tabled(rename = "Name")]
        name: String,
        #[tabled(rename = "Status")]
        status: String,
    }

    // ===== OxurTable::new tests =====

    #[test]
    fn test_new_creates_table_with_default_theme() {
        let data = vec![TestRow { id: 1, name: "Alice".into(), status: "Active".into() }];

        let table = OxurTable::new(data);

        // Verify the table was created (we can't easily inspect internals)
        // Just ensure it doesn't panic
        assert_eq!(table.data.len(), 1);
    }

    #[test]
    fn test_new_with_empty_data() {
        let data: Vec<TestRow> = vec![];
        let table = OxurTable::new(data);
        assert_eq!(table.data.len(), 0);
    }

    #[test]
    fn test_new_with_multiple_rows() {
        let data = vec![
            TestRow { id: 1, name: "Alice".into(), status: "Active".into() },
            TestRow { id: 2, name: "Bob".into(), status: "Inactive".into() },
            TestRow { id: 3, name: "Charlie".into(), status: "Active".into() },
        ];

        let table = OxurTable::new(data);
        assert_eq!(table.data.len(), 3);
    }

    // ===== OxurTable::render tests =====

    #[test]
    fn test_render_produces_output() {
        let data = vec![TestRow { id: 1, name: "Alice".into(), status: "Active".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        // Verify output is not empty and contains data
        assert!(!output.is_empty());
        assert!(output.contains("Alice"));
        assert!(output.contains("Active"));
    }

    #[test]
    fn test_render_empty_data() {
        let data: Vec<TestRow> = vec![];
        let table = OxurTable::new(data);
        let output = table.render();

        // Should still produce some output (at least headers)
        assert!(!output.is_empty());
    }

    #[test]
    fn test_render_multiple_rows() {
        let data = vec![
            TestRow { id: 1, name: "Alice".into(), status: "Active".into() },
            TestRow { id: 2, name: "Bob".into(), status: "Inactive".into() },
        ];

        let table = OxurTable::new(data);
        let output = table.render();

        assert!(output.contains("Alice"));
        assert!(output.contains("Bob"));
        assert!(output.contains("Active"));
        assert!(output.contains("Inactive"));
    }

    #[test]
    fn test_render_includes_headers() {
        let data = vec![TestRow { id: 1, name: "Test".into(), status: "OK".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        // Headers from #[tabled(rename = "...")] should be present
        assert!(output.contains("ID"));
        assert!(output.contains("Name"));
        assert!(output.contains("Status"));
    }

    #[test]
    fn test_render_contains_ansi_codes() {
        let data = vec![TestRow { id: 1, name: "Test".into(), status: "OK".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        // Should contain ANSI escape codes for colors
        // (The theme applies colors, so there should be escape sequences)
        assert!(output.contains("\x1b[") || output.contains("\u{001b}["));
    }

    // ===== Integration tests =====

    #[test]
    fn test_table_with_special_characters() {
        let data = vec![TestRow { id: 1, name: "Test & \"Special\"".into(), status: "OK".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        assert!(output.contains("Test & \"Special\""));
    }

    #[test]
    fn test_table_with_unicode() {
        let data = vec![TestRow { id: 1, name: "Ñoño 日本語".into(), status: "✓".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        assert!(output.contains("Ñoño"));
        assert!(output.contains("日本語"));
        assert!(output.contains("✓"));
    }

    #[test]
    fn test_table_with_long_text() {
        let long_name = "A".repeat(100);
        let data = vec![TestRow { id: 1, name: long_name.clone(), status: "OK".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        // Long text should be in the output (might be wrapped or truncated by tabled)
        assert!(output.contains(&long_name[..50])); // At least first 50 chars
    }

    #[test]
    fn test_table_with_empty_strings() {
        let data = vec![TestRow { id: 1, name: "".into(), status: "".into() }];

        let table = OxurTable::new(data);
        let output = table.render();

        // Should handle empty strings gracefully
        assert!(!output.is_empty());
        assert!(output.contains("ID")); // Headers should still be present
    }

    #[test]
    fn test_different_struct_type() {
        #[derive(Tabled)]
        struct DifferentRow {
            #[tabled(rename = "Col1")]
            col1: String,
            #[tabled(rename = "Col2")]
            col2: i32,
        }

        let data = vec![DifferentRow { col1: "Test".into(), col2: 42 }];

        let table = OxurTable::new(data);
        let output = table.render();

        assert!(output.contains("Test"));
        assert!(output.contains("42"));
        assert!(output.contains("Col1"));
        assert!(output.contains("Col2"));
    }

    // ===== with_title tests =====

    #[test]
    fn test_with_title_adds_title_row() {
        let data = vec![TestRow { id: 1, name: "Alice".into(), status: "Active".into() }];

        let table = OxurTable::new(data).with_title("MY TABLE");
        let output = table.render();

        // Title should appear in the output
        assert!(output.contains("MY TABLE"));
        // Data should still be present
        assert!(output.contains("Alice"));
    }

    #[test]
    fn test_with_title_string_slice() {
        let data = vec![TestRow { id: 1, name: "Test".into(), status: "OK".into() }];

        let table = OxurTable::new(data).with_title("TITLE FROM &str");
        let output = table.render();

        assert!(output.contains("TITLE FROM &str"));
    }

    #[test]
    fn test_with_title_owned_string() {
        let data = vec![TestRow { id: 1, name: "Test".into(), status: "OK".into() }];
        let title = String::from("OWNED TITLE");

        let table = OxurTable::new(data).with_title(title);
        let output = table.render();

        assert!(output.contains("OWNED TITLE"));
    }

    // ===== with_footer tests =====

    #[test]
    fn test_with_footer_adds_footer_row() {
        let data = vec![TestRow { id: 1, name: "Alice".into(), status: "Active".into() }];

        let table = OxurTable::new(data).with_footer();
        let output = table.render();

        // Data should be present
        assert!(output.contains("Alice"));
        // Output should be longer than without footer (footer adds a row)
        let without_footer =
            OxurTable::new(vec![TestRow { id: 1, name: "Alice".into(), status: "Active".into() }])
                .render();
        assert!(output.len() > without_footer.len());
    }

    // ===== with_title and with_footer combined tests =====

    #[test]
    fn test_with_title_and_footer() {
        let data = vec![
            TestRow { id: 1, name: "Alice".into(), status: "Active".into() },
            TestRow { id: 2, name: "Bob".into(), status: "Inactive".into() },
        ];

        let table = OxurTable::new(data).with_title("USER LIST").with_footer();
        let output = table.render();

        // Title should appear
        assert!(output.contains("USER LIST"));
        // Data should be present
        assert!(output.contains("Alice"));
        assert!(output.contains("Bob"));
        // Headers should be present
        assert!(output.contains("ID"));
        assert!(output.contains("Name"));
        assert!(output.contains("Status"));
    }

    #[test]
    fn test_chaining_order_does_not_matter() {
        let data1 = vec![TestRow { id: 1, name: "Test".into(), status: "OK".into() }];
        let data2 = vec![TestRow { id: 1, name: "Test".into(), status: "OK".into() }];

        let output1 = OxurTable::new(data1).with_title("TITLE").with_footer().render();
        let output2 = OxurTable::new(data2).with_footer().with_title("TITLE").render();

        // Both should produce the same result
        assert_eq!(output1, output2);
    }

    #[test]
    fn test_empty_data_with_title_and_footer() {
        let data: Vec<TestRow> = vec![];

        let table = OxurTable::new(data).with_title("EMPTY TABLE").with_footer();
        let output = table.render();

        // Title should appear
        assert!(output.contains("EMPTY TABLE"));
        // Headers should be present
        assert!(output.contains("ID"));
    }
}