summary history branches tags files
commit:f428c0770e7cb26caf451cb6488fbdc18756d50d
author:Trevor Bentley
committer:Trevor Bentley
date:Wed Aug 28 23:06:45 2024 +0200
parents:cf5146f5e1c2227adbadf00583cc93717d90ea82
add org-mode export
diff --git a/src/main.rs b/src/main.rs
line changes: +35/-3
index 6860115..692476c
--- a/src/main.rs
+++ b/src/main.rs
@@ -54,7 +54,11 @@ struct CalcArgs {
     #[arg(short, long)]
     relative: bool,
 
-    /// Output in CSV format.  Single row by default, or table if csv-fraction and csv-stops specified.
+    /// Output table in emacs org-mode format
+    #[arg(short, long)]
+    org: bool,
+
+    /// Output table in CSV format
     #[arg(short, long)]
     csv: bool,
 
@@ -252,7 +256,7 @@ fn fstop_table(cli: &CalcArgs) -> (Vec<Fraction>, Vec<Fraction>, Vec<FstopEntry>
         fractions_to_steps(&fractions, cli.stops.unwrap_or(3))
     };
 
-    let row_steps = match cli.csv {
+    let row_steps = match cli.csv || cli.org {
         true => {
             let fractions = denominators_to_fractions(&cli.table_fraction);
             fractions_to_steps(&fractions, cli.table_stops.unwrap_or(3))
@@ -337,6 +341,31 @@ fn fstop_table_formula_csv(col_steps: &Vec<Fraction>, row_steps: &Vec<Fraction>,
     }
 }
 
+// print an f-stop table in org-mode format
+fn fstop_table_org(col_steps: &Vec<Fraction>, row_steps: &Vec<Fraction>, table: &Vec<FstopEntry>, cli: &CalcArgs) {
+    println!("|-");
+    print!("|");
+    if !cli.csv_skip_header {
+        print!(" |");
+        for col_frac in col_steps {
+            print!(" {} |", col_frac.offset_str(0, true));
+        }
+        println!();
+        println!("|-");
+    }
+    for (row, row_frac) in row_steps.iter().enumerate() {
+        print!("|");
+        if !cli.csv_skip_header {
+            print!(" {} |", row_frac.offset_str(0, true));
+        }
+        for (col, _col_frac) in col_steps.iter().enumerate() {
+            print!(" {} |", table[row*col_steps.len() + col].str(cli.precision(), cli.relative));
+        }
+        println!();
+    }
+    println!("|-");
+}
+
 // print a single-rowed f-stop table for the terminal
 fn fstop_table_print_row(col_steps: &Vec<Fraction>, table: &Vec<FstopEntry>, cli: &CalcArgs) {
     for idx in 0..col_steps.len() {
@@ -377,7 +406,10 @@ fn main() {
                 1 => fstop_table_print_row(&col_steps, &table, &cli),
                 _ => match cli.csv_formulas {
                     true => fstop_table_formula_csv(&col_steps, &row_steps, &table, &cli),
-                    _ => fstop_table_csv(&col_steps, &row_steps, &table, &cli),
+                    _ => match cli.org {
+                        true => fstop_table_org(&col_steps, &row_steps, &table, &cli),
+                        _ => fstop_table_csv(&col_steps, &row_steps, &table, &cli),
+                    },
                 },
             }
         },