2 Commits

Author SHA1 Message Date
e499f76c3f more claude changes 2026-03-03 15:00:08 -06:00
97df57aba5 add claude's changes 2026-03-03 14:51:43 -06:00
6 changed files with 955 additions and 1183 deletions

1318
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,9 +1,15 @@
[package] [package]
name = "mmtui" name = "mmtui"
version = "0.1.0" version = "0.1.0"
edition = "2024" edition = "2021"
[[bin]]
name = "mmtui"
path = "src/main.rs"
[dependencies] [dependencies]
chrono = "0.4.44" ratatui = "0.29"
rand = "0.10.0" crossterm = "0.28"
ratatui = "0.30.0" chrono = "0.4"
rand = "0.9"
anyhow = "1"

View File

@@ -0,0 +1,338 @@
use crate::row::{make_checksum, Row};
// ── Field indices for global inputs ──────────────────────────────────────────
pub const GLOBAL_HUT_ID: usize = 0;
pub const GLOBAL_CHECKSUM: usize = 1;
pub const GLOBAL_TARE: usize = 2;
pub const GLOBAL_LOT_STATUS: usize = 3;
pub const GLOBAL_PATH: usize = 4;
pub const GLOBAL_HUT_TYPE: usize = 5;
pub const GLOBAL_COUNT: usize = 6;
pub const LOT_STATUS_OPTIONS: &[&str] = &["Unrestricted", "Blocked", "Quality"];
// Field indices for per-row inputs
pub const ROW_MAT_ID: usize = 0;
pub const ROW_QUANTITY: usize = 1;
pub const ROW_UOM: usize = 2;
pub const ROW_COUNT: usize = 3;
/// Which section of the UI the cursor is in
#[derive(Clone, PartialEq)]
pub enum Focus {
GlobalField(usize),
LotRow(usize), // focused on the row itself (for deletion etc.)
LotField(usize, usize), // (row_index, field_index)
}
#[derive(Clone, PartialEq)]
pub enum ModalKind {
ConfirmClose,
ConfirmQuit,
}
pub struct LotEntry {
/// raw string buffers for each field
pub fields: [String; ROW_COUNT],
}
impl LotEntry {
pub fn new() -> Self {
Self {
fields: [
String::new(), // mat_id
String::new(), // quantity
String::new(), // uom
],
}
}
pub fn mat_id(&self) -> &str {
&self.fields[ROW_MAT_ID]
}
pub fn quantity(&self) -> f64 {
self.fields[ROW_QUANTITY].parse().unwrap_or(0.0)
}
pub fn uom(&self) -> &str {
&self.fields[ROW_UOM]
}
}
pub struct App {
/// Global field string buffers
pub global: [String; GLOBAL_COUNT],
pub lots: Vec<LotEntry>,
pub focus: Focus,
pub modal: Option<ModalKind>,
/// Custom user-defined properties: (header, type, value)
pub custom_props: Vec<(String, String, String)>,
}
impl App {
pub fn new() -> Self {
let mut global: [String; GLOBAL_COUNT] = Default::default();
// sensible defaults
global[GLOBAL_LOT_STATUS] = LOT_STATUS_OPTIONS[0].to_string();
Self {
global,
lots: Vec::new(),
focus: Focus::GlobalField(0),
modal: None,
custom_props: Vec::new(),
}
}
// ── navigation ────────────────────────────────────────────────────────────
pub fn focus_next(&mut self) {
self.focus = match &self.focus {
Focus::GlobalField(i) => {
let next = i + 1;
if next < GLOBAL_COUNT {
Focus::GlobalField(next)
} else if !self.lots.is_empty() {
Focus::LotField(0, 0)
} else {
Focus::GlobalField(0)
}
}
Focus::LotField(row, field) => {
let next_field = field + 1;
if next_field < ROW_COUNT {
Focus::LotField(*row, next_field)
} else {
let next_row = row + 1;
if next_row < self.lots.len() {
Focus::LotField(next_row, 0)
} else {
Focus::GlobalField(0)
}
}
}
Focus::LotRow(row) => {
let next = row + 1;
if next < self.lots.len() {
Focus::LotRow(next)
} else {
Focus::LotRow(*row)
}
}
};
}
pub fn focus_prev(&mut self) {
self.focus = match &self.focus {
Focus::GlobalField(i) => {
if *i == 0 {
// wrap to last lot field or stay
if !self.lots.is_empty() {
let last_row = self.lots.len() - 1;
Focus::LotField(last_row, ROW_COUNT - 1)
} else {
Focus::GlobalField(GLOBAL_COUNT - 1)
}
} else {
Focus::GlobalField(i - 1)
}
}
Focus::LotField(row, field) => {
if *field == 0 {
if *row == 0 {
Focus::GlobalField(GLOBAL_COUNT - 1)
} else {
Focus::LotField(row - 1, ROW_COUNT - 1)
}
} else {
Focus::LotField(*row, field - 1)
}
}
Focus::LotRow(row) => {
if *row == 0 {
Focus::LotRow(0)
} else {
Focus::LotRow(row - 1)
}
}
};
}
/// Move focus to the same field in the next lot row (Down arrow)
pub fn focus_next_row(&mut self) {
match &self.focus {
Focus::LotField(row, field) => {
let next = row + 1;
if next < self.lots.len() {
let f = *field;
self.focus = Focus::LotField(next, f);
}
}
Focus::LotRow(row) => {
let next = row + 1;
if next < self.lots.len() {
self.focus = Focus::LotRow(next);
}
}
_ => {}
}
}
/// Move focus to the same field in the previous lot row (Up arrow)
pub fn focus_prev_row(&mut self) {
match self.focus.clone() {
Focus::LotField(row, field) => {
if row == 0 {
self.focus = Focus::GlobalField(0);
} else {
self.focus = Focus::LotField(row - 1, field);
}
}
Focus::LotRow(row) => {
if row == 0 {
self.focus = Focus::GlobalField(0);
} else {
self.focus = Focus::LotRow(row - 1);
}
}
_ => {}
}
}
// ── editing ───────────────────────────────────────────────────────────────
pub fn type_char(&mut self, c: char) {
match self.focus.clone() {
// GLOBAL_CHECKSUM is toggled via Space, not typed
// GLOBAL_LOT_STATUS is cycled via Space/←/→, not typed
Focus::GlobalField(i) if i == GLOBAL_CHECKSUM || i == GLOBAL_LOT_STATUS => {}
Focus::GlobalField(i) => {
self.global[i].push(c);
}
Focus::LotField(row, field) => {
self.lots[row].fields[field].push(c);
}
_ => {}
}
}
pub fn backspace(&mut self) {
match self.focus.clone() {
Focus::GlobalField(i) if i == GLOBAL_CHECKSUM || i == GLOBAL_LOT_STATUS => {}
Focus::GlobalField(i) => {
self.global[i].pop();
}
Focus::LotField(row, field) => {
self.lots[row].fields[field].pop();
}
_ => {}
}
}
pub fn toggle_checksum(&mut self) {
let val = &self.global[GLOBAL_CHECKSUM];
self.global[GLOBAL_CHECKSUM] = if val == "true" {
"false".into()
} else {
"true".into()
};
}
// ── lot management ────────────────────────────────────────────────────────
pub fn add_lot(&mut self) {
self.lots.push(LotEntry::new());
let new_row = self.lots.len() - 1;
self.focus = Focus::LotField(new_row, 0);
}
pub fn remove_focused_lot(&mut self) {
let row = match &self.focus {
Focus::LotRow(r) | Focus::LotField(r, _) => *r,
_ => return,
};
if row < self.lots.len() {
self.lots.remove(row);
if self.lots.is_empty() {
self.focus = Focus::GlobalField(0);
} else {
let new_row = row.min(self.lots.len() - 1);
self.focus = Focus::LotField(new_row, 0);
}
}
}
// ── CSV generation ────────────────────────────────────────────────────────
pub fn generate_csv(&self) -> String {
let checksum = self.global[GLOBAL_CHECKSUM] == "true";
let hut_id = &self.global[GLOBAL_HUT_ID];
let tare: f64 = self.global[GLOBAL_TARE].parse().unwrap_or(0.0);
let lot_status = &self.global[GLOBAL_LOT_STATUS];
let path = &self.global[GLOBAL_PATH];
let hut_type = &self.global[GLOBAL_HUT_TYPE];
// Build header rows
let custom_headers: Vec<String> = self
.custom_props
.iter()
.map(|(h, _, _)| h.clone())
.collect();
let custom_types: Vec<String> = self
.custom_props
.iter()
.map(|(_, t, _)| t.clone())
.collect();
let header_row = format!(
",,,,,,,,,,,LOTdtCreationDate,LOTdtBornOnDate,LOTdtExpiryDate{}",
if custom_headers.is_empty() {
String::new()
} else {
format!(",{}", custom_headers.join(","))
}
);
let type_row = format!(
",,,,,,,,,,,DATETIME,DATETIME,DATETIME{}",
if custom_types.is_empty() {
String::new()
} else {
format!(",{}", custom_types.join(","))
}
);
let mut lines = vec![header_row, type_row];
for entry in &self.lots {
let name = if checksum {
make_checksum()
} else {
hut_id.to_ascii_uppercase()
};
let row = Row::new(
&name,
hut_type,
path,
tare,
entry.mat_id(),
entry.quantity(),
entry.uom(),
lot_status,
false, // checksum already applied to name above
);
let custom_values: Vec<String> = self
.custom_props
.iter()
.map(|(_, _, v)| v.clone())
.collect();
let line = if custom_values.is_empty() {
format!("{}", row)
} else {
format!("{},{}", row, custom_values.join(","))
};
lines.push(line);
}
lines.join("\n")
}
}

View File

@@ -1,7 +1,142 @@
mod app; mod app;
mod csv; mod row;
mod ui; mod ui;
fn main() { use app::{App, Focus, ModalKind, GLOBAL_CHECKSUM};
println!("Hello, world!"); use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{backend::CrosstermBackend, Terminal};
use std::io;
fn main() -> anyhow::Result<()> {
// ── terminal setup ───────────────────────────────────────────────────────
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let result = run(&mut terminal);
// ── restore terminal ─────────────────────────────────────────────────────
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
// Output CSV to stdout AFTER restoring terminal so it's clean
match result {
Ok(Some(csv)) => {
print!("{}", csv);
}
Ok(None) => {}
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1);
}
}
Ok(())
}
/// Returns `Some(csv)` when the user confirms close, `None` on quit.
fn run<B: ratatui::backend::Backend>(terminal: &mut Terminal<B>) -> anyhow::Result<Option<String>> {
let mut app = App::new();
loop {
terminal.draw(|f| ui::draw(f, &app))?;
if let Event::Key(key) = event::read()? {
// ── modal active ─────────────────────────────────────────────────
if app.modal.is_some() {
match key.code {
KeyCode::Char('y') | KeyCode::Char('Y') => {
let kind = app.modal.take().unwrap();
match kind {
ModalKind::ConfirmClose => {
return Ok(Some(app.generate_csv()));
}
ModalKind::ConfirmQuit => {
return Ok(None);
}
}
}
KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
app.modal = None;
}
_ => {}
}
continue;
}
// ── global shortcuts ─────────────────────────────────────────────
match key.code {
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
// Ctrl-C hard quit without modal
return Ok(None);
}
KeyCode::Char('c') => {
app.modal = Some(ModalKind::ConfirmClose);
continue;
}
KeyCode::Char('q') => {
app.modal = Some(ModalKind::ConfirmQuit);
continue;
}
KeyCode::Char('+') | KeyCode::Char('=') => {
app.add_lot();
continue;
}
KeyCode::Char('-') => {
app.remove_focused_lot();
continue;
}
KeyCode::Tab => {
app.focus_next();
continue;
}
KeyCode::BackTab => {
app.focus_prev();
continue;
}
KeyCode::Up => {
if matches!(app.focus, Focus::LotField(_, _) | Focus::LotRow(_)) {
app.focus_prev_row();
} else {
app.focus_prev();
}
continue;
}
KeyCode::Down => {
if matches!(app.focus, Focus::LotField(_, _) | Focus::LotRow(_)) {
app.focus_next_row();
} else {
app.focus_next();
}
continue;
}
_ => {}
}
// ── field-specific input ─────────────────────────────────────────
match app.focus.clone() {
Focus::GlobalField(i) if i == GLOBAL_CHECKSUM => {
if key.code == KeyCode::Char(' ') {
app.toggle_checksum();
}
}
_ => match key.code {
KeyCode::Backspace => app.backspace(),
KeyCode::Char(c) => app.type_char(c),
_ => {}
},
}
}
}
} }

View File

@@ -1,19 +1,19 @@
pub struct Row { pub struct Row {
name: String, pub name: String,
hut_type: String, pub hut_type: String,
path: String, pub path: String,
tare: f64, pub tare: f64,
mat_id: String, pub mat_id: String,
quantity: f64, pub quantity: f64,
lot_id: String, pub lot_id: String,
uom: String, pub uom: String,
lot_status: String, pub lot_status: String,
born: chrono::DateTime<chrono::Local>, pub born: chrono::DateTime<chrono::Local>,
expire: chrono::DateTime<chrono::Local>, pub expire: chrono::DateTime<chrono::Local>,
} }
impl Row { impl Row {
const DF: &str = "%Y-%m-%d %H:%M"; pub const DF: &'static str = "%Y-%m-%d %H:%M";
pub fn new( pub fn new(
name: &str, name: &str,
@@ -30,9 +30,10 @@ impl Row {
let expire = born let expire = born
.checked_add_days(chrono::Days::new(365)) .checked_add_days(chrono::Days::new(365))
.expect("A valid date time"); .expect("A valid date time");
let name = match checksum { let name = if checksum {
true => make_checksum(), make_checksum()
false => name.to_ascii_uppercase(), } else {
name.to_ascii_uppercase()
}; };
Self { Self {
name: name.clone(), name: name.clone(),
@@ -50,10 +51,9 @@ impl Row {
} }
} }
fn make_checksum() -> String { pub fn make_checksum() -> String {
let base = rand::random_range(10000..99999).to_string(); let base = rand::random_range(10000..99999u32).to_string();
let split_expect = "a valid character in the base checksum"; let split_expect = "a valid character in the base checksum";
let split = format!( let split = format!(
"{}{}{}{}{}", "{}{}{}{}{}",
base.chars().next().expect(split_expect), base.chars().next().expect(split_expect),
@@ -63,14 +63,11 @@ fn make_checksum() -> String {
base.chars().nth(3).expect(split_expect) base.chars().nth(3).expect(split_expect)
) )
.repeat(2); .repeat(2);
let sum: u32 = split let sum: u32 = split
.chars() .chars()
.map(|c| c.to_digit(10).expect("a valid integer value")) .map(|c| c.to_digit(10).expect("a valid integer value"))
.sum(); .sum();
let last = (10 - (sum % 10)) % 10; let last = (10 - (sum % 10)) % 10;
format!("{}{}", base, last) format!("{}{}", base, last)
} }

288
src/ui.rs
View File

@@ -0,0 +1,288 @@
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Cell, Clear, Paragraph, Row, Table, TableState},
Frame,
};
use crate::app::{
App, Focus, ModalKind, GLOBAL_CHECKSUM, GLOBAL_COUNT, GLOBAL_HUT_ID, GLOBAL_HUT_TYPE,
GLOBAL_PATH, GLOBAL_TARE, ROW_COUNT, ROW_MAT_ID, ROW_QUANTITY, ROW_UOM,
};
const GLOBAL_LABELS: &[&str] = &[
"Hut ID",
"Checksum",
"Tare",
"Lot Status",
"Path",
"Hut Type",
];
const ROW_LABELS: &[&str] = &["Material ID", "Quantity", "UOM"];
// ── colour palette ────────────────────────────────────────────────────────────
const C_FOCUSED: Color = Color::Cyan;
const C_UNFOCUSED: Color = Color::DarkGray;
const C_ACCENT: Color = Color::Yellow;
const C_ROW_SEL: Color = Color::Blue;
const C_BORDER: Color = Color::Gray;
const C_HEADER: Color = Color::White;
fn focused_style(is_focused: bool) -> Style {
if is_focused {
Style::default().fg(C_FOCUSED).add_modifier(Modifier::BOLD)
} else {
Style::default().fg(C_UNFOCUSED)
}
}
fn focused_block<'a>(title: &'a str, is_focused: bool) -> Block<'a> {
Block::default()
.title(title)
.borders(Borders::ALL)
.border_style(if is_focused {
Style::default().fg(C_FOCUSED)
} else {
Style::default().fg(C_BORDER)
})
}
// ── main draw ────────────────────────────────────────────────────────────────
pub fn draw(f: &mut Frame, app: &App) {
let area = f.area();
// Overall vertical split: globals | lots | help bar
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(10), // global fields
Constraint::Min(6), // lot rows
Constraint::Length(3), // help bar
])
.split(area);
draw_globals(f, app, chunks[0]);
draw_lots(f, app, chunks[1]);
draw_help(f, chunks[2]);
if let Some(modal) = &app.modal {
draw_modal(f, modal, area);
}
}
// ── globals panel ─────────────────────────────────────────────────────────────
fn draw_globals(f: &mut Frame, app: &App, area: Rect) {
let outer = Block::default()
.title(" Global Settings ")
.borders(Borders::ALL)
.border_style(Style::default().fg(C_ACCENT));
let inner = outer.inner(area);
f.render_widget(outer, area);
// Two rows of three fields each
let rows = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Length(3),
Constraint::Length(3),
])
.split(inner);
// Row 0: Hut ID | Checksum | Tare
let row0 = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Ratio(2, 5),
Constraint::Ratio(1, 5),
Constraint::Ratio(2, 5),
])
.split(rows[0]);
// Row 1: Lot Status | Path | Hut Type
let row1 = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
])
.split(rows[1]);
let field_areas = [
row0[0], // GLOBAL_HUT_ID
row0[1], // GLOBAL_CHECKSUM
row0[2], // GLOBAL_TARE
row1[0], // GLOBAL_LOT_STATUS
row1[1], // GLOBAL_PATH
row1[2], // GLOBAL_HUT_TYPE
];
for i in 0..GLOBAL_COUNT {
let is_focused = app.focus == Focus::GlobalField(i);
let label = GLOBAL_LABELS[i];
let value: String = if i == GLOBAL_CHECKSUM {
let checked = app.global[i] == "true";
format!("[{}] (Space)", if checked { "x" } else { " " })
} else {
app.global[i].clone()
};
let display = if is_focused {
format!("{}", value)
} else {
value
};
let p = Paragraph::new(display)
.block(focused_block(label, is_focused))
.style(focused_style(is_focused));
f.render_widget(p, field_areas[i]);
}
}
// ── lots table ────────────────────────────────────────────────────────────────
fn draw_lots(f: &mut Frame, app: &App, area: Rect) {
let outer = Block::default()
.title(" Lot Rows (+) Add (-) Remove ")
.borders(Borders::ALL)
.border_style(Style::default().fg(C_ACCENT));
let inner = outer.inner(area);
f.render_widget(outer, area);
if app.lots.is_empty() {
let hint = Paragraph::new("No lots yet — press + to add one")
.style(Style::default().fg(C_UNFOCUSED));
f.render_widget(hint, inner);
return;
}
// Column widths
let col_constraints = [
Constraint::Length(4), // #
Constraint::Min(18), // Material ID
Constraint::Length(12), // Quantity
Constraint::Length(8), // UOM
];
let header_cells = ["#", "Material ID", "Quantity", "UOM"]
.iter()
.map(|h| Cell::from(*h).style(Style::default().fg(C_HEADER).add_modifier(Modifier::BOLD)));
let header = Row::new(header_cells).height(1).bottom_margin(0);
let rows: Vec<Row> = app
.lots
.iter()
.enumerate()
.map(|(row_idx, entry)| {
let cells: Vec<Cell> = (0..ROW_COUNT + 1)
.map(|col| {
if col == 0 {
// row number
Cell::from(format!("{:>2}", row_idx + 1))
.style(Style::default().fg(C_UNFOCUSED))
} else {
let field_idx = col - 1;
let is_focused = app.focus == Focus::LotField(row_idx, field_idx);
let text = if is_focused {
format!("{}", entry.fields[field_idx])
} else {
entry.fields[field_idx].clone()
};
Cell::from(text).style(focused_style(is_focused))
}
})
.collect();
let row_selected =
matches!(&app.focus, Focus::LotRow(r) | Focus::LotField(r, _) if *r == row_idx);
let row_style = if row_selected {
Style::default().bg(Color::Rgb(20, 30, 50))
} else {
Style::default()
};
Row::new(cells).style(row_style).height(1)
})
.collect();
let table = Table::new(rows, col_constraints)
.header(header)
.block(Block::default())
.row_highlight_style(Style::default().bg(C_ROW_SEL));
let mut state = TableState::default();
f.render_stateful_widget(table, inner, &mut state);
}
// ── help bar ──────────────────────────────────────────────────────────────────
fn draw_help(f: &mut Frame, area: Rect) {
let spans = vec![
Span::styled(" Tab ", Style::default().fg(Color::Black).bg(C_ACCENT)),
Span::raw(" Next field "),
Span::styled(" + ", Style::default().fg(Color::Black).bg(Color::Green)),
Span::raw(" Add lot "),
Span::styled(" - ", Style::default().fg(Color::Black).bg(Color::Red)),
Span::raw(" Remove lot "),
Span::styled(" c ", Style::default().fg(Color::Black).bg(Color::Cyan)),
Span::raw(" Output CSV "),
Span::styled(" q ", Style::default().fg(Color::Black).bg(Color::Magenta)),
Span::raw(" Quit "),
];
let help = Paragraph::new(Line::from(spans)).block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(C_BORDER)),
);
f.render_widget(help, area);
}
// ── modal overlay ─────────────────────────────────────────────────────────────
fn draw_modal(f: &mut Frame, kind: &ModalKind, area: Rect) {
let (title, body) = match kind {
ModalKind::ConfirmClose => (
" Output CSV and Close ",
"Output generated CSV to stdout and exit?\n\n [y] Yes [n] Cancel",
),
ModalKind::ConfirmQuit => (
" Quit Without Output ",
"Quit without outputting anything?\n\n [y] Yes [n] Cancel",
),
};
let popup_width = 50u16;
let popup_height = 7u16;
let x = area.x + area.width.saturating_sub(popup_width) / 2;
let y = area.y + area.height.saturating_sub(popup_height) / 2;
let popup_area = Rect::new(
x,
y,
popup_width.min(area.width),
popup_height.min(area.height),
);
f.render_widget(Clear, popup_area);
let color = match kind {
ModalKind::ConfirmClose => Color::Cyan,
ModalKind::ConfirmQuit => Color::Magenta,
};
let p = Paragraph::new(body)
.block(
Block::default()
.title(title)
.borders(Borders::ALL)
.border_style(Style::default().fg(color).add_modifier(Modifier::BOLD)),
)
.style(Style::default().fg(C_HEADER));
f.render_widget(p, popup_area);
}