Skip to content

Commit 44edc18

Browse files
authored
Merge pull request #74 from umpire274/v0.5.3
Release v0.5.3 – Interactive initialization and improved startup flow
2 parents af40e98 + 6e9f76b commit 44edc18

File tree

6 files changed

+142
-51
lines changed

6 files changed

+142
-51
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# Changelog
22

3+
## [0.5.3] - 2025-11-05
4+
5+
### Fixed
6+
7+
- Added interactive check when rFortune is launched without an existing configuration directory:
8+
- If run in an interactive terminal, the user is asked to confirm initialization (`Initialize rFortune now? [Y/n]`).
9+
- If run in a non-interactive context (script, CI, etc.), initialization proceeds automatically.
10+
- Prevents unintended directory creation and improves transparency during the first launch.
11+
12+
### Changed
13+
14+
- Initialization flow is now clearer and reports actions through `ConsoleLog` messages.
15+
16+
---
17+
318
## [0.5.2] - 2025-11-04
419

520
### Added

Cargo.lock

Lines changed: 65 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rfortune"
3-
version = "0.5.2"
3+
version = "0.5.3"
44
edition = "2024"
55
authors = ["Umpire274 <umpire274@gmail.com>"]
66
description = "A Rust-based clone of the classic UNIX 'fortune' command"
@@ -19,7 +19,7 @@ include = [
1919
]
2020

2121
[build-dependencies]
22-
winresource = "0.1.23"
22+
winresource = "0.1.26"
2323

2424
[package.metadata.winresource]
2525
Icon = "res/rfortune.ico"
@@ -30,4 +30,5 @@ rand = "0.9.2"
3030
dirs = "6.0.0"
3131
serde = { version = "1.0.228", features = ["derive"] }
3232
serde_yaml = "0.9.33"
33+
atty = "0.2.14"
3334

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,20 @@ scripting, or just a bit of inspiration.
1515

1616
---
1717

18-
## ✨ New in v0.5.2
18+
### ✨ New in v0.5.3
1919

20-
### 🆕 Configuration system improvements
20+
**🧭 Smarter first-time initialization**
2121

22-
- Configuration filename renamed from config.yaml → rfortune.conf for better clarity and portability.
23-
- Automatic migration from legacy config.yaml to the new format — the old file is safely backed up as config.yaml.bak.
24-
- Added the new subcommand config edit:
25-
- rfortune config edit → opens rfortune.conf using the system’s default text editor.
26-
- rfortune config edit --editor <name|path> → opens it with a specific editor (e.g. vi, nano, code).
27-
- Automatic detection of preferred editors via $VISUAL and $EDITOR environment variables.
28-
- Default fallback editors are nano (macOS/Linux) and notepad (Windows).
22+
- Added an interactive prompt when `rfortune` is launched for the first time and no configuration directory exists.
23+
- In interactive terminals, users are asked: `Initialize rFortune now? [Y/n]`.
24+
- In non-interactive contexts (scripts, CI, etc.), initialization happens automatically.
25+
- Prevents unintended directory creation and improves transparency during first setup.
26+
- Initialization progress and results are displayed using `ConsoleLog` messages for consistency.
2927

30-
### 🎨 Improved CLI feedback
28+
**⚙️ Improved startup behavior**
3129

32-
- Introduced the new ConsoleLog class for rich, colored terminal output:
33-
- ℹ Info, ✅ Success, ⚠️ Warning, ❌ Error
34-
- Unified message style and improved feedback during configuration, cache, and fortune file operations.
30+
- The application now clearly reports which configuration directory is being used (`dirs::data_dir()` path).
31+
- Overall initialization flow refined for clarity and better UX.
3532

3633
---
3734

@@ -145,6 +142,15 @@ Running `rfortune` without subcommands prints a random fortune from the default
145142

146143
---
147144

145+
## ⚙️ First-time setup
146+
147+
When `rfortune` is launched for the first time and no configuration directory exists,
148+
the application will ask whether to initialize its environment (creating the default
149+
configuration and fortune files).
150+
In non-interactive contexts, initialization happens automatically.
151+
152+
---
153+
148154
## 🧩 Options & Subcommands
149155

150156
| Command / Option | Description |

src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::Parser;
22
use rfortune::log::ConsoleLog;
3+
use rfortune::utils::ensure_app_initialized;
34
use rfortune::{config, loader, utils};
45

56
mod cli;
@@ -12,6 +13,11 @@ fn main() {
1213

1314
println!();
1415

16+
if let Err(e) = ensure_app_initialized() {
17+
ConsoleLog::ko(format!("Initialization error: {e}"));
18+
return;
19+
}
20+
1521
match cli.command {
1622
// ---------------- CONFIG ----------------
1723
Some(Commands::Config { action }) => match action {

src/utils.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use crate::config;
22
use crate::loader::FortuneFile;
33
use crate::log::ConsoleLog;
44
use rand::seq::IndexedRandom;
5-
use std::fs;
65
use std::io::Write;
76
use std::path::{Path, PathBuf};
7+
use std::{fs, io};
88

99
/// Estrae una citazione casuale dalla lista
1010
pub fn random_quote(quotes: &[String]) -> &str {
@@ -99,7 +99,7 @@ fn get_cache_dir() -> PathBuf {
9999
}
100100

101101
/// Svuota completamente la cache
102-
pub fn clear_cache_dir() -> std::io::Result<()> {
102+
pub fn clear_cache_dir() -> io::Result<()> {
103103
let dir = get_cache_dir();
104104
if dir.exists() {
105105
fs::remove_dir_all(&dir)?;
@@ -111,7 +111,7 @@ pub fn clear_cache_dir() -> std::io::Result<()> {
111111
}
112112

113113
/// Salva l’ultima citazione usata in un file di cache
114-
pub fn save_last_cache(file_path: &Path, quote: &str) -> std::io::Result<()> {
114+
pub fn save_last_cache(file_path: &Path, quote: &str) -> io::Result<()> {
115115
let cache_path = get_cache_path(file_path);
116116
if let Some(parent) = cache_path.parent() {
117117
fs::create_dir_all(parent)?; // assicura che la cartella esista
@@ -122,3 +122,34 @@ pub fn save_last_cache(file_path: &Path, quote: &str) -> std::io::Result<()> {
122122

123123
Ok(())
124124
}
125+
126+
pub fn ensure_app_initialized() -> io::Result<()> {
127+
let dir = config::app_dir();
128+
129+
if dir.exists() {
130+
return Ok(()); // tutto a posto
131+
}
132+
133+
// Se il processo è interattivo (TTY), chiedi conferma
134+
if atty::is(atty::Stream::Stdin) {
135+
print!("Configuration directory not found. Initialize rFortune now? [Y/n]: ");
136+
io::stdout().flush()?;
137+
138+
let mut input = String::new();
139+
io::stdin().read_line(&mut input)?;
140+
let answer = input.trim().to_lowercase();
141+
142+
if answer == "n" || answer == "no" {
143+
ConsoleLog::warn("Initialization aborted by user.");
144+
std::process::exit(0);
145+
}
146+
} else {
147+
ConsoleLog::info("Configuration directory missing — initializing automatically.");
148+
}
149+
150+
// Esegui l’inizializzazione completa
151+
ConsoleLog::info("Initializing rFortune environment...");
152+
config::init_config_file()?;
153+
ConsoleLog::ok("rFortune initialized successfully.");
154+
Ok(())
155+
}

0 commit comments

Comments
 (0)