Skip to content

Commit

Permalink
tecomp: add pkg subcommand with --list option
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed Jun 11, 2024
1 parent 57a1628 commit 5d2802e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 4 deletions.
14 changes: 12 additions & 2 deletions tecomp/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct Cli {
pub enum CommandVariant {
Make(MakeArgs),
Watch(WatchArgs),
Pkg(PkgArgs),
}

#[derive(Args, Debug)]
Expand All @@ -29,7 +30,7 @@ pub struct CommonArgGroup {
#[arg(short, long, default_value = ".")]
pub outdir: String,
/// Print full TeX compilation output
#[arg(short, long, action)]
#[arg(short, long)]
pub verbose: bool,
}

Expand All @@ -47,9 +48,18 @@ pub struct WatchArgs {
pub watch: String,

/// Initially compile the document when watch is started
#[arg(short = 'i', long, action)]
#[arg(short = 'i', long)]
pub initial_make: bool,

#[command(flatten)]
pub com: CommonArgGroup,
}

/// Manage LaTeX packages under the tecomp texmf directory
#[derive(Args, Debug)]
#[command(arg_required_else_help = true)]
pub struct PkgArgs {
/// List installed packages
#[arg(long)]
pub list: bool,
}
4 changes: 4 additions & 0 deletions tecomp/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ pub fn get_texpdfc() -> &'static str {
}
}

pub fn get_texmf_loc() -> CompResult<PathBuf> {
Ok(get_exec_loc()?.join("engine").join("texmf"))
}

pub fn str_to_pathbuf<S: AsRef<str>>(str: S, must_exist: bool) -> CompResult<PathBuf> {
let mut p = PathBuf::from(&str.as_ref());
if must_exist {
Expand Down
17 changes: 15 additions & 2 deletions tecomp/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod error;
mod files;
mod handle;
mod log;
mod pkg;
mod watch;

fn main() {
Expand All @@ -44,7 +45,7 @@ fn main() {
.compile()
.map_err(|_| CompError::CompilationError(format!("Compilation error")))?;

return Ok(());
Ok(())
}
CommandVariant::Watch(o) => {
add_watch_ctrlc_handler()?;
Expand All @@ -58,7 +59,19 @@ fn main() {
o.initial_make,
)?;

return Ok(());
Ok(())
}
CommandVariant::Pkg(o) => {
// list all installed packages
if o.list {
for p in pkg::get_installed()? {
println!("{}", p);
}

return Ok(());
}

Ok(())
}
}
}() {
Expand Down
38 changes: 38 additions & 0 deletions tecomp/src/pkg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Jack Bennett.
* All Rights Reserved.
*
* See the LICENCE file for more information.
*/

use std::{fs, path::PathBuf};

use crate::{
error::{CompError, CompResult},
files,
};

pub fn get_installed() -> CompResult<Vec<String>> {
let dir = get_package_dir()?;

// read folders inside the packages folder (dir)
let pkgs = fs::read_dir(dir)
.map_err(|e| CompError::FileNotFoundError(e.to_string()))?
.map(|res| res.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()
.map_err(|e| CompError::FilesystemError(e.to_string()))?;

// convert pkgs list to contain the folder names
let mut pkgs = pkgs
.into_iter()
.map(|d| d.file_name().and_then(|x| x.to_str()).unwrap().to_string())
.collect::<Vec<_>>();

pkgs.sort();

Ok(pkgs)
}

fn get_package_dir() -> CompResult<PathBuf> {
Ok(files::get_texmf_loc()?.join("tex").join("latex"))
}

0 comments on commit 5d2802e

Please sign in to comment.