Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the automatic creation of config files and the ability to open them with the CLI. #285

Merged
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
18 changes: 17 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ use tracing_subscriber::prelude::*;
use tracing_subscriber::util::SubscriberInitExt;
use utils::{ImageCache, Point, Rect, Size};

use crate::opts::Commands;
use crate::opts::{Commands, ConfigCmd};
use anyhow::Context;
use clap::Parser;
use taffy::Taffy;
Expand Down Expand Up @@ -775,6 +775,22 @@ fn main() -> anyhow::Result<()> {
let inlyne = Inlyne::new(opts)?;
inlyne.run();
}
Commands::Config(ConfigCmd::Open) => {
let config_path = dirs::config_dir()
.context("Failed to find the configuration directory")?
.join("inlyne")
.join("inlyne.toml");

if !config_path.is_file() {
tracing::info!(
"No config found. Creating a new config at: {}",
config_path.display()
);
Config::create_default_config(&config_path)?;
}

edit::edit_file(config_path)?;
}
}

Ok(())
Expand Down
9 changes: 9 additions & 0 deletions src/opts/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ impl Cli {
#[derive(Subcommand, Debug, PartialEq, Clone)]
pub enum Commands {
View(View),
#[command(subcommand)]
Config(ConfigCmd),
}

/// View markdown a file with inlyne
Expand All @@ -90,3 +92,10 @@ pub struct View {
#[arg(short = 'w', long = "page-width")]
pub page_width: Option<f32>,
}

/// Configuration related things
#[derive(Subcommand, PartialEq, Clone, Debug)]
pub enum ConfigCmd {
/// Opens the configuration file in the default text editor
Open,
}
18 changes: 15 additions & 3 deletions src/opts/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fs::read_to_string;
use std::path::Path;
use std::fs::{create_dir_all, read_to_string};
use std::io::Write;
use std::path::{Path, PathBuf};

use super::ThemeType;
use crate::color;
Expand Down Expand Up @@ -108,11 +109,22 @@ impl Config {
let config_path = config_dir.join("inlyne").join("inlyne.toml");

if !config_path.is_file() {
return Ok(Self::default());
Self::create_default_config(&config_path)?
}

Self::load_from_file(&config_path)
}

pub fn create_default_config(path: &PathBuf) -> anyhow::Result<()> {
create_dir_all(
path.parent()
.context("Could not find parent directory of path.")?,
)?;

let mut file = std::fs::File::create(path)?;
file.write_all(include_bytes!("../../inlyne.default.toml"))?;
Ok(())
}
}

#[cfg(test)]
Expand Down
2 changes: 1 addition & 1 deletion src/opts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod tests;
use std::path::Path;

use crate::color;
pub use cli::{Cli, Commands, ThemeType, View};
pub use cli::{Cli, Commands, ConfigCmd, ThemeType, View};
pub use config::{Config, FontOptions, KeybindingsSection};

use crate::history::History;
Expand Down