Эх сурвалжийг харах

Remove dependency on console and replace with termion.

Kestrel 1 долоо хоног өмнө
parent
commit
f688e8a283
5 өөрчлөгдсөн 32 нэмэгдсэн , 29 устгасан
  1. 3 2
      Cargo.toml
  2. 7 7
      src/cmd.rs
  3. 0 5
      src/cmd/balance.rs
  4. 8 4
      src/cmd/infer.rs
  5. 14 11
      src/show.rs

+ 3 - 2
Cargo.toml

@@ -24,6 +24,7 @@ tempfile = { version = "3.20" }
 # cli dependencies
 pretty_env_logger = { version = "0.5.0" }
 clap = { version = "4.5", features = ["derive", "env"] }
-console = { version = "0.15" }
 boxy = { version = "0.1.0" }
-cliask = { version = "0.1.0", path = "../cliask/" }
+cliask = { version = "0.1.1" }
+unicode-segmentation = { version = "1.0" }
+termion = { version = "4" }

+ 7 - 7
src/cmd.rs

@@ -73,9 +73,9 @@ pub enum Command {
 }
 
 fn summarize(data: &data::Hoard) {
-    let positive = console::Style::new().green();
-    let negative = console::Style::new().red();
-    let neutral = console::Style::new();
+    let green = termion::color::Fg(termion::color::Green);
+    let red = termion::color::Fg(termion::color::Red);
+    let reset = termion::color::Fg(termion::color::Reset);
 
     let Some(maxlen) = data.account_names().map(|v| v.len()).max() else {
         return;
@@ -98,18 +98,18 @@ fn summarize(data: &data::Hoard) {
                     (
                         u,
                         if b.is_sign_positive() {
-                            positive.apply_to(b).to_string()
+                            format!("{green}{b}{reset}")
                         } else if b.is_sign_negative() {
-                            negative.apply_to(b).to_string()
+                            format!("{red}{b}{reset}")
                         } else {
-                            neutral.apply_to("0").to_string()
+                            "0".to_string()
                         },
                     )
                 });
 
             println!(
                 "{shortname:maxlen$} {}",
-                balances.map(|(u, b)| format!("{b:8} {u:5}")).join(" ")
+                balances.map(|(u, b)| format!("{b:10} {u:5}")).join(" ")
             );
         }
     }

+ 0 - 5
src/cmd/balance.rs

@@ -99,11 +99,6 @@ pub fn show_balance(
     rows.push(show::Row::Line);
 
     for account in data.account_names().sorted_by_key(|v| v.as_str()) {
-        // only show monounit accounts
-        /*let Some(unit) = data.account_spec(account).unwrap().unit else {
-            continue
-        };*/
-
         let mut row = vec![];
         row.push(account.to_string());
 

+ 8 - 4
src/cmd/infer.rs

@@ -60,8 +60,10 @@ pub fn do_inference(data: &mut Hoard) -> anyhow::Result<bool> {
             show_transaction(None, &txn);
 
             println!(
-                "Candidate account is {}. Use?",
-                console::style(candidate.account).red()
+                "Candidate account is {}{}{}. Use?",
+                termion::color::Fg(termion::color::Red),
+                candidate.account,
+                termion::color::Fg(termion::color::Reset),
             );
 
             if cliask::ActionPrompt::<cliask::YesNoAction>::new()
@@ -71,8 +73,10 @@ pub fn do_inference(data: &mut Hoard) -> anyhow::Result<bool> {
             {
                 let new_account = *candidate.account;
                 log::info!(
-                    "    Changing to account {} ...",
-                    console::style(new_account).green()
+                    "    Changing to account {}{}{} ...",
+                    termion::color::Fg(termion::color::Green),
+                    new_account,
+                    termion::color::Fg(termion::color::Reset),
                 );
 
                 drop(txn);

+ 14 - 11
src/show.rs

@@ -28,7 +28,9 @@ pub fn show_table(cols: Vec<Column>, rows: impl Iterator<Item = Row>) {
     for row in &table_data {
         let Row::Data(row) = &row else { continue };
         for (idx, rc) in row.iter().enumerate() {
-            min_widths[idx] = min_widths[idx].max(console::measure_text_width(rc.as_str()));
+            use unicode_segmentation::UnicodeSegmentation;
+            let width = rc.graphemes(true).count();
+            min_widths[idx] = min_widths[idx].max(width);
         }
     }
 
@@ -68,21 +70,22 @@ pub fn show_table(cols: Vec<Column>, rows: impl Iterator<Item = Row>) {
 }
 
 pub fn show_transaction(_root: Option<&Hoard>, txn: &Transaction) {
-    let bluestyle = console::Style::new().blue();
-    // let greenstyle = console::Style::new().green();
-    let yellowstyle = console::Style::new().yellow();
-    let graystyle = console::Style::new().white().dim();
+    let blue = termion::color::Fg(termion::color::Blue);
+    let yellow = termion::color::Fg(termion::color::Yellow);
+    let gray = termion::color::Fg(termion::color::LightBlack);
+    let reset = termion::color::Fg(termion::color::Reset);
+
     println!(
-        "{}: {}",
-        bluestyle.apply_to(txn.datestamp),
-        txn.title.as_deref().unwrap_or("")
+        "{blue}{}{reset}: {gray}{}{reset}",
+        txn.datestamp,
+        txn.title.as_deref().unwrap_or(""),
     );
     for change in &txn.changes {
         println!(
-            " - {}: {} {}",
-            yellowstyle.apply_to(change.account),
+            " - {yellow}{}{reset}: {} {gray}{}{reset}",
+            change.account,
             change.amount,
-            graystyle.apply_to(change.unit)
+            change.unit
         );
     }
 }