Skip to content

Commit

Permalink
restructure directories setup; obligatory bash script
Browse files Browse the repository at this point in the history
  • Loading branch information
Việt Trọng Dương authored Oct 13, 2024
1 parent a3a046c commit 7b11824
Show file tree
Hide file tree
Showing 19 changed files with 358 additions and 135 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ unicode-segmentation = "1.12.0"

[build-dependencies]
vergen-gix = { version = "1.0.0", features = ["build", "cargo", "rustc", "si"] }
directories = "5.0.1"
5 changes: 5 additions & 0 deletions assets/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# THIS IS THE ASSETS FOLDER GOD DAMN IT

This folder was cloned here when `cargo install` ran `build.rs`. Please don't touch this thing unless you know what you're doing!

Thanks! Feel free to tinker with whatever is inside here, by the way.
14 changes: 14 additions & 0 deletions assets/banners/logo.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾/‾‾/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\
‾‾‾‾‾/ /‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ ‾‾ ‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾\ \
/ / /‾‾‾‾‾/ /‾‾‾‾‾‾‾/ /‾‾‾‾‾‾‾‾‾/ /‾‾/ /‾‾‾‾‾‾/ /‾‾‾‾\ \ \
/ / / /‾‾‾ / /‾/ / / /‾/ /‾/ / / / / /‾/ / / /\ \ \ \
/ / / ‾‾‾/ / / / / / / / / / / / / / / / / / ‾‾ \ \ \
/ / / /‾‾‾ / / \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \‾‾‾\ \ \ \
/ / / ‾‾‾/ / / \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ‾‾‾‾‾\
‾‾‾ ‾‾‾‾‾‾ ‾‾‾ ‾‾‾ ‾‾ ‾‾ ‾‾ ‾‾‾ ‾‾‾ ‾‾ ‾‾‾ ‾‾‾ ‾‾‾‾‾‾‾‾
/‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾/ /‾‾\ \‾‾‾‾‾\ \‾‾‾‾‾‾‾‾/ /\ \‾‾‾‾‾\ \‾‾‾‾‾‾\
/ /‾‾ / / /\ \ \ \‾\ \ \ \‾‾‾‾ / \ \ \‾\ \ \ \‾‾‾
/ /‾‾ /‾‾ /‾‾‾‾ /‾‾‾‾ / / / / / / /‾/ / / / / /\ \ \ \ \ \ \ ‾‾/
/ /‾‾ / / / / ‾ / / / / / / / / ‾‾ \ \ \/ / / /‾‾
/ / / /‾/ / / / / / / ‾‾‾/ / /‾‾‾‾\ \ \ / / ‾‾/
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ ‾‾ ‾‾ ‾‾ ‾‾ ‾‾‾‾‾‾ ‾‾ ‾‾ ‾‾ ‾‾‾‾‾
46 changes: 44 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
use vergen_gix::*;
use std::{
fs::*,
path::{
Path,
PathBuf,
},
};

use vergen_gix::*;

type Result<T, E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

fn init_vergen() -> Result<()> {
Emitter::default()
.add_instructions(&BuildBuilder::all_build()?)?
.add_instructions(&CargoBuilder::all_cargo()?)?
Expand All @@ -9,3 +20,34 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.emit()?;
Ok(())
}

// https://stackoverflow.com/a/65192210/26515777
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> Result<()> {
create_dir_all(&dst)?;
for entry in read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}

fn init_assets() -> Result<()> {
let dirs = directories::ProjectDirs::from("", "", env!("CARGO_PKG_NAME"))
.expect("unable to retrieve project dirs");
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
let local_assets_dir = manifest_dir.join("assets");
let asset_dir = dirs.data_dir().join(".assets");
let _ = remove_dir_all(asset_dir.clone());
copy_dir_all(local_assets_dir, asset_dir)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
init_vergen()?;
init_assets()?;
Ok(())
}
9 changes: 9 additions & 0 deletions copy-assets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#! /usr/bin/bash

assets_path=$HOME/.local/share/terminal-arcade/.assets
if [ ! -d $assets_path ]; then
mkdir $assets_path
else
rm -rf $assets_path
fi
cp -r ./assets/* $assets_path
2 changes: 1 addition & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! All components (screens & widgets) implmeneted in Terminal Arcade.
//! All components (screens & widgets) implmeneted for the app.
pub mod screens;
pub mod widgets;
7 changes: 7 additions & 0 deletions src/components/screens/home/banner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Banner showing the Terminal Arcade ASCII art logo.
use lazy_static::lazy_static;

lazy_static! {
static ref BANNER: String = std::fs::read_to_string("path");
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Home screen.
//! Home screen to greet a user on running.
use ratatui::{
layout::Rect,
Expand Down Expand Up @@ -38,15 +38,15 @@ impl UiElement for HomeScreen {
Ok(())
}

#[rustfmt::skip]
fn render(&self, _state: Self::State, _frame: &mut Frame<'_>, _size: Rect) {}
fn render(&self, _state: Self::State, _frame: &mut Frame<'_>, _size: Rect) {
}
}

impl Screen for HomeScreen {
fn get_init_state<'a>(
&self,
builder: &'a mut ScreenDataBuilder,
) -> &'a mut ScreenDataBuilder {
builder.title("Welcome home!")
builder.title("Terminal Arcade 🕹️")
}
}
2 changes: 1 addition & 1 deletion src/components/screens/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//! All screens implmeneted in Terminal Arcade.
//! All screens implmeneted for the app.
pub mod home;
38 changes: 17 additions & 21 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//! Configuration in Terminal Arcade.
//!
//! Under the [config folder](crate::util::dirs::get_config_dir) will be a list
//! of configuration files, defaulting to `config.toml`.
//! Configuration for the app.
use std::path::PathBuf;

Expand All @@ -21,35 +18,32 @@ use serde::{
};

use crate::{
services::dirs::{
get_config_dir,
get_data_dir,
AppDirs,
services::{
dirs::AppDirs,
CARGO_PKG_NAME,
},
tui::GameSpecs,
};

#[derive(Debug, Clone, Default, Serialize, Deserialize, new)]
#[serde(rename_all = "kebab-case")]
pub struct Config {
/// Directories that Terminal Arcade depends on.
/// App directories.
#[serde(skip)]
pub app_dirs: AppDirs,

/// Terminal Arcade's game specifications.
/// Game specifications.
pub game_specs: GameSpecs,
}

impl Config {
/// Fetches a new configuration object for Terminal Arcade.
/// Fetches a new configuration object for the app.
/// If none is found, a default one will be created at the config folder and
/// returned. If one is found, the function tries to deserialize it and
/// returns the resulting config.
pub fn fetch() -> crate::Result<Self> {
let config_dir = get_config_dir();
let data_dir = get_data_dir();
let mut config_builder = ConfigBuilder::<DefaultState>::default()
.set_default("config_dir", config_dir.to_str())?
.set_default("data_dir", data_dir.to_str())?;
pub fn fetch(app_dirs: AppDirs) -> crate::Result<Self> {
let (config_dir, _) = app_dirs.get_config_dir("config", None)?;
let mut config_builder = ConfigBuilder::<DefaultState>::default();

let config_path = config_dir.join("config.toml");
if !config_path.exists() {
Expand All @@ -66,19 +60,21 @@ impl Config {
.format(FileFormat::Toml)
.required(true),
)
.add_source(config::Environment::with_prefix("TA"));
.add_source(config::Environment::with_prefix(&CARGO_PKG_NAME));

config_builder
let mut config = config_builder
.build()?
.try_deserialize()
.try_deserialize::<Self>()
.wrap_err("unable to parse & deserialize config")
.warning(
"your config might have been modified - it is missing fields, \
malformatted, etc.",
)
.with_suggestion(|| {
format!("check your config at {}!", config_path.display())
})
})?;
config.app_dirs = app_dirs;
Ok(config)
}

/// Constructs a new default config with the provided path,
Expand Down
7 changes: 5 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use crate::{
app::App,
config::Config,
services::dirs::AppDirs,
};

mod app;
Expand All @@ -35,6 +36,8 @@ type Result<T, E = color_eyre::eyre::Report> = color_eyre::eyre::Result<T, E>;

#[tokio::main]
async fn main() -> Result<()> {
services::initialize_services()?;
App::with_config(Config::fetch()?)?.run()
let app_dirs = AppDirs::default();
services::initialize_services(&app_dirs)?;
let config = Config::fetch(app_dirs)?;
App::with_config(config)?.run()
}
Loading

0 comments on commit 7b11824

Please sign in to comment.