Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rand = "0.10.3"
rustyline = "19.0.0"
serde = { version = "1.0.229", features = ["derive"] }
serde_json = "1.0.154"
which = "8.0.6"

[dev-dependencies]
assert_cmd = "2.2.2"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Then type 'help' to see the list of available commands and 'guide' to see the ga

## Installation

## Runtime dependencies

- [`genact`](https://github.com/svenstaro/genact) (optional) - For compilation animations

### Packages

[![Packaging status](https://repology.org/badge/vertical-allrepos/incrust.svg)](https://repology.org/project/incrust/versions)
Expand Down
14 changes: 7 additions & 7 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub fn compile(game_state: &mut GameState, interrupt_rx: &Receiver<()>) -> anyho
// Start the fake compile process and loop over it
// Break the loop if it gets interrupted or if the CI/CD perk is not enabled
loop {
let interrupted = compile::compile_proc(game_state, interrupt_rx)?;
let interrupted = compile::compile_process(game_state, interrupt_rx)?;

if interrupted || !game_state.ci_cd_enabled {
break;
Expand All @@ -46,15 +46,15 @@ pub fn manage(game_state: &mut GameState, submenu_editor: &mut PromptEditor) ->
pub fn status(game_state: &GameState) {
println!("[RESOURCES]");
println!();
println!("CPU {} CORE(S)", game_state.cpu);
println!("RAM {} GB", game_state.ram);
println!("DISK {} GB", game_state.disk);
println!("CREDITS {:.2}", game_state.credits);
println!("CPU {} CORE(S)", game_state.cpu);
println!("RAM {} GB", game_state.ram);
println!("DISK {} GB", game_state.disk);
println!("CREDITS {:.2}", game_state.credits);
println!();
println!("[PERKS]");
println!();
println!(
"CI/CD {}",
"CI/CD {}",
if !game_state.ci_cd_unlocked {
"LOCKED"
} else if game_state.ci_cd_enabled {
Expand All @@ -64,7 +64,7 @@ pub fn status(game_state: &GameState) {
}
);
println!(
"Auto-Scaling {}",
"Auto-Scaling {}",
if !game_state.auto_scaling_unlocked {
"LOCKED"
} else if game_state.auto_scaling_enabled {
Expand Down
79 changes: 56 additions & 23 deletions src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,20 @@ use anyhow::Context;
use colored::Colorize;
use rand::random;
use std::io::{self, Write};
use std::process::{Child, Command, Stdio};
use std::sync::mpsc::{Receiver, TryRecvError};
use std::thread;
use std::time::{Duration, Instant};
use which::which;

use crate::parameters;
use crate::statefile::{self, GameState};
use crate::upgrade;

pub fn compile_proc(
pub fn compile_process(
game_state: &mut GameState,
interrupt_rx: &Receiver<()>,
) -> anyhow::Result<bool> {
// Set timestamps to calculate compilation remaining time
let start = Instant::now();
let mut last_remaining = u64::MAX;

// Silly messages
println!(
"{} Synchronizing imaginary dependencies...",
Expand All @@ -46,55 +44,68 @@ pub fn compile_proc(
parameters::show_compilation_parameters(game_state);
println!();

// Convert compile time to a Duration (used later to calculate remaining time)
// Convert compile time to a Duration (used later to determine when the compilation is over)
let compile_time = Duration::from_secs_f64(compile_time);

// Start compilation timer
let start = Instant::now();

// Start genact (if it's installed, directly checked by the start_genact() function)
let mut genact_process = start_genact();

// Fallback compilation animation if genact is not installed
let spinner = ["-", "\\", "|", "/"];
let mut spinner_index = 0;

// Main compilation loop
loop {
// Check for interruption request via Ctrl+C
match interrupt_rx.try_recv() {
Ok(()) => {
// Stop genact (if needed)
stop_genact(&mut genact_process);

println!();
println!(
"\n{} Compilation interrupted, no credits earned...",
"{} Compilation interrupted, no credits earned...",
"[INFO]".blue()
);
return Ok(true);
}
Err(TryRecvError::Empty) => {}
Err(TryRecvError::Disconnected) => {
eprintln!("\nWarning: Interruption handler disconnected.");
// Stop genact (if needed)
stop_genact(&mut genact_process);
eprintln!("Warning: Interruption handler disconnected.");
return Ok(true);
}
}

// Calculate remaining time
let remaining = compile_time.saturating_sub(start.elapsed());

// Check whether compilation is complete
if remaining.is_zero() {
if start.elapsed() >= compile_time {
break;
}

// Display countdown
let remaining_secs = remaining.as_secs_f64().ceil() as u64;

if remaining_secs != last_remaining {
let minutes = remaining_secs / 60;
let seconds = remaining_secs % 60;

print!("\rCompiling... {minutes:02}:{seconds:02} remaining ");
// Display fallback animation if genact isn't running / installed
if genact_process.is_none() {
print!("\rCompiling {}", spinner[spinner_index]);
let _ = io::stdout().flush();

last_remaining = remaining_secs;
spinner_index = (spinner_index + 1) % spinner.len();
}

// Wait before checking again
// Wait before checking restarting the loop
thread::sleep(Duration::from_millis(100));
}

// Stop genact (if needed)
stop_genact(&mut genact_process);

// Move to a new line after the animation
println!();

// Compilation completion message
println!("\rCompilation completed! ");
println!("Compilation completed!");
println!();

// Roll for compilation failure
Expand Down Expand Up @@ -125,3 +136,25 @@ pub fn compile_proc(

Ok(false)
}

// Helper to start genact for compilation animation (if it's installed)
fn start_genact() -> Option<Child> {
// Check if genact is installed
let genact_path = which("genact").ok()?;

// Run genact
Command::new(genact_path)
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()
.ok()
}

// Helper to stop genact (if there is a genact process running)
fn stop_genact(genact_process: &mut Option<Child>) {
// Kill the running genact process spawned by incrust (if there's one)
if let Some(mut child) = genact_process.take() {
let _ = child.kill();
let _ = child.wait();
}
}
6 changes: 3 additions & 3 deletions src/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ pub fn show_compilation_parameters(game_state: &GameState) -> (f64, f64, f64) {

// Show values
println!(
"Compile time {:02}:{:02}",
"Compile time {:02}:{:02}",
(compile_time / 60.0) as u64,
compile_time as u64 % 60
);
println!("Credit reward {credit_reward:.2}");
println!("Failure {:.1}%", failure_probability * 100.0);
println!("Credit reward {credit_reward:.2}");
println!("Failure probability {:.1}%", failure_probability * 100.0);

// Return values
(compile_time, credit_reward, failure_probability)
Expand Down