From 9d81cf5dc6f834ea09eb7730bce3ff345fc8e0f1 Mon Sep 17 00:00:00 2001
From: Pavel Kirilin <win10@list.ru>
Date: Sun, 5 Apr 2020 21:40:56 +0400
Subject: [PATCH] Fixed error handling.

Signed-off-by: Pavel Kirilin <win10@list.ru>
---
 Cargo.lock       | 14 +++++++++++++-
 Cargo.toml       |  5 +++--
 src/cli.rs       |  2 ++
 src/main.rs      |  9 ++++++++-
 src/result.rs    |  2 +-
 src/run_modes.rs |  5 ++++-
 src/tty_stuff.rs |  1 -
 7 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index ffcf14a..60fc49e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -37,9 +37,10 @@ dependencies = [
 
 [[package]]
 name = "awatch"
-version = "0.1.0"
+version = "0.3.1"
 dependencies = [
  "alphanumeric-sort",
+ "colored",
  "failure",
  "failure_derive",
  "lazy_static",
@@ -107,6 +108,17 @@ dependencies = [
  "vec_map",
 ]
 
+[[package]]
+name = "colored"
+version = "1.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
+dependencies = [
+ "atty",
+ "lazy_static",
+ "winapi",
+]
+
 [[package]]
 name = "failure"
 version = "0.1.7"
diff --git a/Cargo.toml b/Cargo.toml
index d3a965d..a2eb793 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "awatch"
-version = "0.1.0"
+version = "0.3.1"
 authors = ["Pavel Kirilin <win10@list.ru>"]
 edition = "2018"
 
@@ -17,4 +17,5 @@ lazy_static = "1.4"             # Define lazy static vars.
 alphanumeric-sort = "1.0.12"    # Used to search for videos.
 regex = "1"                     # Regular expressions.
 termion = "1.5.5"               # For interacting with terminal.
-term_grid = "0.1.7"             # For showing matched files while initialization
\ No newline at end of file
+term_grid = "0.1.7"             # For showing matched files while initialization
+colored = "1.9"                 # For terminal coloring
\ No newline at end of file
diff --git a/src/cli.rs b/src/cli.rs
index 2b834bc..55e7eeb 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -20,4 +20,6 @@ pub enum RunMode {
     Next,
     #[structopt(name = "update", about = "Update saved config")]
     Update,
+    #[structopt(name = "reset", about = "Set episode to 0")]
+    Reset,
 }
\ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 6dab836..0c41364 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,6 +15,7 @@ use structopt::StructOpt;
 
 use crate::run_modes::run;
 use crate::result::AppResult;
+use colored::{Colorize, Color};
 
 pub mod result;
 pub mod config;
@@ -26,6 +27,12 @@ include!("cli.rs");
 
 fn main() -> AppResult<()> {
     let opt: Opt = Opt::from_args();
-    run(opt)?;
+    if let Err(error) = run(opt) {
+        println!("{dashes} {title} {dashes}",
+                 dashes = "#######".color(Color::BrightRed),
+                 title= "Error".color(Color::BrightRed)
+        );
+        println!("{}", error);
+    }
     Ok(())
 }
diff --git a/src/result.rs b/src/result.rs
index a3d9495..c8aff76 100644
--- a/src/result.rs
+++ b/src/result.rs
@@ -6,7 +6,7 @@ pub enum AppError {
     StdErr(String),
     #[fail(display = "Config processing failed: {}", _0)]
     ParseError(String),
-    #[fail(display = "Error was found: {}", _0)]
+    #[fail(display = "{}", _0)]
     RuntimeError(String),
 }
 
diff --git a/src/run_modes.rs b/src/run_modes.rs
index eb58c25..a4acc82 100644
--- a/src/run_modes.rs
+++ b/src/run_modes.rs
@@ -2,7 +2,7 @@ use crate::{Opt, RunMode};
 use crate::result::{AppResult, AppError};
 use crate::initialization::init_config;
 use crate::config::{update_episode, update_config, Config};
-use std::process::{Command};
+use std::process::Command;
 
 pub fn run(opts: Opt) -> AppResult<()> {
     let mode = opts.mode.unwrap_or_else(|| RunMode::Play);
@@ -22,6 +22,9 @@ pub fn run(opts: Opt) -> AppResult<()> {
         RunMode::Update => {
             update_config()
         }
+        RunMode::Reset => {
+            update_episode(|_| { Ok(0) })
+        }
     }
 }
 
diff --git a/src/tty_stuff.rs b/src/tty_stuff.rs
index c7b8737..b89ead4 100644
--- a/src/tty_stuff.rs
+++ b/src/tty_stuff.rs
@@ -124,7 +124,6 @@ pub fn read_tty_line(
             Key::Char(c) => {
                 buffer.insert(current_pos - 1, c.clone());
                 current_pos += 1;
-                println!("{:#?}", c);
             }
             Key::Backspace => {
                 if let Some(pos) = current_pos.checked_sub(2) {
-- 
GitLab