From ccf73766371411c4d025e6735e4de8c815006c28 Mon Sep 17 00:00:00 2001 From: Patrick Auernig Date: Mon, 14 Aug 2023 21:07:56 +0200 Subject: [PATCH] Remove old non-tui view and remove tui feature --- Cargo.lock | 4 +- Cargo.toml | 2 +- README.md | 6 +- crates/wayfarer/Cargo.toml | 26 +++--- crates/wayfarer/src/edit.rs | 2 +- crates/wayfarer/src/main.rs | 25 ++---- crates/wayfarer/src/show.rs | 146 ------------------------------- crates/wayfarer/src/tui.rs | 21 +++-- crates/wayfarer/src/tui/state.rs | 31 +++++-- 9 files changed, 64 insertions(+), 199 deletions(-) delete mode 100644 crates/wayfarer/src/show.rs diff --git a/Cargo.lock b/Cargo.lock index b99b778..1591fd8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -444,7 +444,7 @@ checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" [[package]] name = "jrny-save" -version = "0.3.0" +version = "0.4.0" dependencies = [ "binrw", "chrono", @@ -1064,7 +1064,7 @@ checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" [[package]] name = "wayfarer" -version = "0.3.0" +version = "0.4.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 11f7019..c5281a0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,7 +7,7 @@ members = [ [workspace.package] -version = "0.3.0" +version = "0.4.0" edition = "2021" license-file = "LICENSE" diff --git a/README.md b/README.md index 1a7f9de..3222b60 100644 --- a/README.md +++ b/README.md @@ -13,11 +13,11 @@ cargo install --git https://github.com/valeth/wayfarer Make sure the path cargo installs to is in your system's `PATH`. ```sh -wayfarer tui +wayfarer -# or +# of if you want to override the last remembered file -wayfarer show +wayfarer --path ``` diff --git a/crates/wayfarer/Cargo.toml b/crates/wayfarer/Cargo.toml index ed14951..1905042 100644 --- a/crates/wayfarer/Cargo.toml +++ b/crates/wayfarer/Cargo.toml @@ -18,27 +18,23 @@ version = "4.3" features = ["derive"] [dependencies.jrny-save] -version = "0.3" +version = "0.4" path = "../save" +[dependencies.ratatui] +version = "0.22" + +[dependencies.tui-input] +version = "0.8" + +[dependencies.crossterm] +version = "0.27" + [dependencies.notify] version = "6.0" optional = true -[dependencies.ratatui] -version = "0.22" -optional = true - -[dependencies.tui-input] -version = "0.8" -optional = true - -[dependencies.crossterm] -version = "0.27" -optional = true - [features] -default = ["watch", "tui"] +default = ["watch"] watch = ["dep:notify"] -tui = ["dep:ratatui", "dep:tui-input", "dep:crossterm"] diff --git a/crates/wayfarer/src/edit.rs b/crates/wayfarer/src/edit.rs index bd71af3..6e81dc5 100644 --- a/crates/wayfarer/src/edit.rs +++ b/crates/wayfarer/src/edit.rs @@ -6,7 +6,7 @@ use clap::builder::PossibleValuesParser; use clap::{value_parser, Parser as ArgParser}; use jrny_save::{RobeColor, Savefile, LEVEL_NAMES}; -use crate::Args as AppArgs; +use crate::AppArgs; #[derive(Debug, Clone, ArgParser)] diff --git a/crates/wayfarer/src/main.rs b/crates/wayfarer/src/main.rs index e30fb9e..c442e7a 100644 --- a/crates/wayfarer/src/main.rs +++ b/crates/wayfarer/src/main.rs @@ -1,5 +1,4 @@ mod edit; -mod show; mod tui; mod watcher; @@ -26,37 +25,31 @@ lazy_static::lazy_static! { #[derive(Debug, ArgParser)] #[command(author, version, about)] -pub(crate) struct Args { +pub(crate) struct AppArgs { + #[command(flatten)] + tui_args: tui::Args, + #[command(subcommand)] - command: CommandArgs, + command: Option, } #[derive(Debug, Clone, clap::Subcommand)] pub(crate) enum CommandArgs { - /// Display info about save files - Show(show::Args), - /// Edit verious aspect of save files Edit(edit::Args), - - #[cfg(feature = "tui")] - Tui(tui::Args), } fn main() -> Result<()> { tracing_setup()?; - let args = Args::parse(); + let args = AppArgs::parse(); + match &args.command { - CommandArgs::Show(sub_args) => show::execute(&args, sub_args)?, - - CommandArgs::Edit(sub_args) => edit::execute(&args, sub_args)?, - - #[cfg(feature = "tui")] - CommandArgs::Tui(sub_args) => tui::execute(&args, sub_args)?, + Some(CommandArgs::Edit(sub_args)) => edit::execute(&args, sub_args)?, + None => tui::execute(&args.tui_args)?, } Ok(()) diff --git a/crates/wayfarer/src/show.rs b/crates/wayfarer/src/show.rs deleted file mode 100644 index 9b341af..0000000 --- a/crates/wayfarer/src/show.rs +++ /dev/null @@ -1,146 +0,0 @@ -use std::fs::File; -use std::path::{Path, PathBuf}; - -use anyhow::Result; -use clap::Parser as ArgParser; -use jrny_save::Savefile; - -use crate::Args as AppArgs; - - -#[derive(Debug, Clone, ArgParser)] -pub struct Args { - path: PathBuf, - - #[cfg(feature = "watch")] - #[arg(long, default_value_t = false)] - watch: bool, -} - - -pub(crate) fn execute(_app_args: &AppArgs, args: &Args) -> Result<()> { - if !args.path.exists() { - anyhow::bail!("Could not find file at given path"); - } - - show_all_info(&args.path)?; - - #[cfg(feature = "watch")] - if args.watch { - watch_all_info(&args.path)?; - } - - Ok(()) -} - - -#[cfg(feature = "watch")] -fn watch_all_info

(path: P) -> Result<()> -where - P: AsRef, -{ - use std::sync::mpsc; - - use crate::watcher::FileWatcher; - - let (tx, rx) = mpsc::channel(); - - let _watcher = FileWatcher::new(path.as_ref(), move || { - tx.send(()).unwrap(); - }); - - println!("Watching file for changes..."); - - loop { - let _ = rx.recv()?; - - show_all_info(&path).unwrap(); - } -} - - -fn show_all_info

(path: P) -> Result<()> -where - P: AsRef, -{ - let file = File::open(path)?; - let savefile = Savefile::from_reader(file)?; - - println!("------======::::: WAYFARER :::::======------\n"); - general_info(&savefile); - println!("\n---===---===---===---===---===---===---===---\n"); - current_journey(&savefile); - println!("\n---===---===---===---===---===---===---===---\n"); - glyphs(&savefile); - println!("\n---===---===---===---===---===---===---===---\n"); - murals(&savefile); - println!("\n---===---===---===---===---===---===---===---\n"); - current_companions(&savefile); - println!("\n---===---===---===---===---===---===---===---\n"); - past_companions(&savefile); - println!("\n------======::::::::::::::::::::======------"); - - Ok(()) -} - -fn general_info(savefile: &Savefile) { - println!("Journeys Completed: {:>}", savefile.journey_count); - println!("Total Companions Met: {:>}", savefile.total_companions_met); - println!( - "Total Symbols Collected: {}", - savefile.total_collected_symbols - ); -} - -fn current_journey(savefile: &Savefile) { - println!("Current Level: {:<10}", savefile.current_level_name()); - println!("Companions Met: {:<10}", savefile.companions_met); - println!("Scarf Length: {:<10}", savefile.scarf_length); - println!("Symbol Number: {:<10}", savefile.symbol.id); - println!( - "Robe: {:<10}, Tier {}", - savefile.robe_color(), - savefile.robe_tier() - ); - println!("Last Played: {:<10}", savefile.last_played); -} - -fn current_companions(savefile: &Savefile) { - for companion in savefile.current_companions() { - println!( - "{:24} {}", - companion.name, - companion.steam_url().to_string() - ); - } -} - -fn past_companions(savefile: &Savefile) { - for companion in savefile.past_companions() { - println!( - "{:24} {}", - companion.name, - companion.steam_url().to_string() - ); - } -} - -fn glyphs(savefile: &Savefile) { - for (level, glyphs) in savefile.glyphs.all() { - print!("{:<16} ", jrny_save::LEVEL_NAMES[level]); - for glyph in glyphs { - print!("{:3}", if glyph { "X" } else { "O" }); - } - println!(); - } -} - -fn murals(savefile: &Savefile) { - for (level, murals) in savefile.murals.all() { - print!("{:<16} ", jrny_save::LEVEL_NAMES[level]); - for mural in murals { - print!("{:3}", if mural { "X" } else { "O" }); - } - println!(); - } -} diff --git a/crates/wayfarer/src/tui.rs b/crates/wayfarer/src/tui.rs index 5eef3b5..f11c269 100644 --- a/crates/wayfarer/src/tui.rs +++ b/crates/wayfarer/src/tui.rs @@ -1,11 +1,10 @@ -#![cfg(feature = "tui")] - mod events; mod state; mod view; use std::io::{self, Stdout}; +use std::path::PathBuf; use std::sync::mpsc::{self, TryRecvError}; use anyhow::Result; @@ -21,14 +20,17 @@ use ratatui::backend::CrosstermBackend; use tracing::{debug, error, info}; use self::state::{Mode, State}; -use crate::Args as AppArgs; type Terminal = ratatui::Terminal>; #[derive(Debug, Clone, ArgParser)] -pub struct Args; +pub struct Args { + /// Overrides the last loaded file + #[arg(long, short)] + path: Option, +} #[derive(Debug, Clone)] @@ -47,8 +49,15 @@ pub enum Message { } -pub(crate) fn execute(_app_args: &AppArgs, _args: &Args) -> Result<()> { - let state = State::load()?; +pub(crate) fn execute(args: &Args) -> Result<()> { + let state = match &args.path { + Some(path) => { + let mut state = State::default(); + state.set_savefile_from_path(path)?; + state + } + None => State::load()?, + }; let mut terminal = setup()?; diff --git a/crates/wayfarer/src/tui/state.rs b/crates/wayfarer/src/tui/state.rs index 367d956..19ec44c 100644 --- a/crates/wayfarer/src/tui/state.rs +++ b/crates/wayfarer/src/tui/state.rs @@ -1,6 +1,7 @@ use std::fs::{self, create_dir_all, read_to_string}; use std::io::Write; use std::os::unix::prelude::OsStrExt; +use std::path::Path; use anyhow::Result; use jrny_save::Savefile; @@ -12,15 +13,6 @@ use crate::watcher::FileWatcher; use crate::DIRS; -pub struct State { - savefile: Option, - pub mode: Mode, - pub file_select: Input, - #[cfg(feature = "watch")] - file_watcher: Option, -} - - #[derive(Debug, Default, Clone, PartialEq, Eq)] pub enum Mode { #[default] @@ -31,6 +23,17 @@ pub enum Mode { SelectFile, } + +#[derive(Default)] +pub struct State { + savefile: Option, + pub mode: Mode, + pub file_select: Input, + #[cfg(feature = "watch")] + file_watcher: Option, +} + + impl State { pub fn load() -> Result { let data_dir = DIRS.data_local_dir(); @@ -54,6 +57,16 @@ impl State { self.savefile.as_ref() } + pub fn set_savefile_from_path

(&mut self, path: P) -> Result<()> + where + P: AsRef, + { + let savefile = Savefile::from_path(path)?; + self.savefile = Some(savefile); + + Ok(()) + } + pub fn set_selected_as_active_savefile(&mut self) -> Result<()> { let savefile = Savefile::from_path(&self.file_select.value())?;