updated flake. nix package now includes completions
This commit is contained in:
@@ -3,7 +3,14 @@ use clap::{Parser, Subcommand};
|
||||
#[derive(Parser)]
|
||||
pub struct Args {
|
||||
#[command(subcommand)]
|
||||
pub command: CommandArg
|
||||
pub command: CommandArg,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
pub enum ShellArg {
|
||||
Bash,
|
||||
Fish,
|
||||
Zsh,
|
||||
}
|
||||
|
||||
#[derive(Subcommand)]
|
||||
@@ -16,10 +23,13 @@ pub enum CommandArg {
|
||||
InstallReveal,
|
||||
/// Starts a Webserver with the live preview of the Documentation
|
||||
Serve {
|
||||
/// Port for the Live Server
|
||||
/// Port for the Live Server
|
||||
#[arg(short, long)]
|
||||
port: Option<u16>
|
||||
port: Option<u16>,
|
||||
},
|
||||
/// Generates completions for the desired shell
|
||||
Completions {
|
||||
#[command(subcommand)]
|
||||
shell: ShellArg,
|
||||
},
|
||||
/// Information about the completions
|
||||
Completions
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use std::process;
|
||||
|
||||
use regex::Regex;
|
||||
|
||||
fn exec_command(command: &mut process::Command) -> Result<(), String> {
|
||||
let result = command.output();
|
||||
|
||||
@@ -31,7 +29,6 @@ fn asciidoctor_docs(in_path: &str, out_path: &str) -> process::Command {
|
||||
|
||||
fn asciidoctor_slides(in_path: &str, out_path: &str) -> process::Command {
|
||||
let mut command = process::Command::new(format!("asciidoctor-revealjs"));
|
||||
let out_dir = parent_path(out_path);
|
||||
let revealjs_path = "/slides/revealjs";
|
||||
|
||||
command
|
||||
@@ -43,60 +40,6 @@ fn asciidoctor_slides(in_path: &str, out_path: &str) -> process::Command {
|
||||
return command;
|
||||
}
|
||||
|
||||
fn parent_path(child_path: &str) -> String {
|
||||
let split: Vec<&str> = child_path.split("/").collect();
|
||||
let slice = &split[..split.len() - 1];
|
||||
return slice.join("/");
|
||||
}
|
||||
|
||||
pub fn path_between(from: String, to: String) -> String {
|
||||
let from_segments = transform_input_to_clone_split(&from);
|
||||
let to_segments = transform_input_to_clone_split(&to);
|
||||
let last_matching_index = matching_from_start(&from_segments, &to_segments);
|
||||
let number_of_backs = from_segments.len() - last_matching_index;
|
||||
let mut path_between = path_back(number_of_backs);
|
||||
let path_to_to_path = &to_segments[last_matching_index..];
|
||||
path_between.push_str(&path_to_to_path.join("/"));
|
||||
return path_between;
|
||||
}
|
||||
|
||||
fn transform_input_to_clone_split(input: &String) -> Vec<String> {
|
||||
let regex = Regex::new(r"/$").unwrap();
|
||||
let first_transformation = input.clone().replace("./", "");
|
||||
return regex
|
||||
.replace_all(&first_transformation, "")
|
||||
.to_string()
|
||||
.split("/")
|
||||
.collect::<Vec<&str>>()
|
||||
.iter()
|
||||
.map(|s| s.to_string())
|
||||
.collect();
|
||||
}
|
||||
|
||||
fn path_back(count: usize) -> String {
|
||||
let mut path = "".to_string();
|
||||
|
||||
for _ in 0..count {
|
||||
path.push_str("../");
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
pub fn matching_from_start(from_segments: &Vec<String>, to_segments: &Vec<String>) -> usize {
|
||||
for (index, from_segment) in from_segments.iter().enumerate() {
|
||||
if let Some(to_segment) = to_segments.get(index) {
|
||||
if from_segment != to_segment {
|
||||
return index;
|
||||
}
|
||||
} else {
|
||||
return index;
|
||||
}
|
||||
}
|
||||
|
||||
return from_segments.len();
|
||||
}
|
||||
|
||||
pub fn build_doc(in_path: &str, out_path: &str) -> Result<(), String> {
|
||||
let mut command = asciidoctor_docs(in_path, out_path);
|
||||
return exec_command(&mut command);
|
||||
|
||||
@@ -6,10 +6,6 @@ use super::fs_util;
|
||||
|
||||
pub mod asciidoctor;
|
||||
|
||||
pub trait Builder {
|
||||
fn build(&self, in_path: &str, out_path: &str) -> Result<(), String>;
|
||||
}
|
||||
|
||||
pub fn docki_build(in_path: &str) -> DockiBuildResult {
|
||||
let out_path = in_path.replace("/docs/", "/dist/");
|
||||
let convert_out_path = out_path.replace(".adoc", ".html");
|
||||
@@ -35,11 +31,11 @@ pub fn docki_build(in_path: &str) -> DockiBuildResult {
|
||||
}
|
||||
}
|
||||
|
||||
fn copy(in_path: &str, out_path: &str) -> Result<(), String> {
|
||||
fn copy(in_path: &str, out_path: &str) -> Result<(), String> {
|
||||
fs_util::create_parent_dir_recursive(out_path);
|
||||
|
||||
if let Err(err) = fs::copy(in_path, out_path) {
|
||||
return Err(err.to_string())
|
||||
return Err(err.to_string());
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
||||
@@ -1,21 +1,16 @@
|
||||
use colored::Colorize;
|
||||
use std::io;
|
||||
|
||||
const INFO: &str = "
|
||||
You can add completions for docki with the following methods. If you want the completions to be persistent add
|
||||
them to a init file e.g. ~/.zshrc, ~/.bashrc, ~/.config/fish/config.fish.
|
||||
use clap::CommandFactory;
|
||||
use clap_complete::{generate, shells::{Bash, Fish, Zsh}};
|
||||
|
||||
Get Fish Completions
|
||||
source ~/.docki/completions/docki.fish
|
||||
use crate::app::args::structure::{Args, ShellArg};
|
||||
|
||||
Get Zsh Completions
|
||||
source ~/.docki/completions/_docki
|
||||
pub fn completions(shell: ShellArg) {
|
||||
let mut command = Args::command();
|
||||
|
||||
Get Bash Completions
|
||||
source ~/.docki/completions/docki.bash
|
||||
";
|
||||
|
||||
pub fn completions() {
|
||||
println!();
|
||||
println!("{}", "Completions".blue().bold());
|
||||
println!("{}", INFO.bright_black());
|
||||
match shell {
|
||||
ShellArg::Bash => generate(Bash, &mut command, "docki", &mut io::stdout()),
|
||||
ShellArg::Fish => generate(Fish, &mut command, "docki", &mut io::stdout()),
|
||||
ShellArg::Zsh => generate(Zsh, &mut command, "docki", &mut io::stdout()),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,7 +27,7 @@ impl App {
|
||||
CommandArg::Health => health(),
|
||||
CommandArg::InstallReveal => install_reveal().await,
|
||||
CommandArg::Serve { port } => serve(port).await,
|
||||
CommandArg::Completions => completions()
|
||||
CommandArg::Completions { shell } => completions(shell)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -1,37 +0,0 @@
|
||||
use crate::app::build::asciidoctor;
|
||||
|
||||
#[test]
|
||||
fn last_matching_index_0() {
|
||||
let vec1 = vec!["dings", "dings", "dingens"].iter().map(|s| s.to_string()).collect();
|
||||
let vec2 = vec!["dings", "dings", "dings"].iter().map(|s| s.to_string()).collect();
|
||||
|
||||
let last_maching = asciidoctor::matching_from_start(&vec1, &vec2);
|
||||
assert_eq!(last_maching, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn last_matching_index_1() {
|
||||
let vec1 = vec!["dings", "dings", "dingens", "dings", "dingens"].iter().map(|s| s.to_string()).collect();
|
||||
let vec2 = vec!["dings", "dings", "dingens", "dings"].iter().map(|s| s.to_string()).collect();
|
||||
|
||||
let last_maching = asciidoctor::matching_from_start(&vec1, &vec2);
|
||||
assert_eq!(last_maching, 4);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_between_0() {
|
||||
let path1 = "./docs/dings";
|
||||
let path2 = "./dist/dings";
|
||||
|
||||
let path_between = asciidoctor::path_between(path1.to_string(), path2.to_string());
|
||||
assert_eq!(path_between, "../../dist/dings");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_between_1() {
|
||||
let path1 = "./dist/slides/core/";
|
||||
let path2 = "./dist/slides/revealjs";
|
||||
|
||||
let path_between = asciidoctor::path_between(path1.to_string(), path2.to_string());
|
||||
assert_eq!(path_between, "../revealjs")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user