Skip to content

Commit

Permalink
poc-manpages
Browse files Browse the repository at this point in the history
  • Loading branch information
Its-Just-Nans committed Jun 30, 2024
1 parent 96fa8e9 commit 724eca9
Show file tree
Hide file tree
Showing 14 changed files with 233 additions and 22 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,8 @@ uu_base32 = { version = ">=0.0.18", path = "src/uu/base32" }
clap = { workspace = true }
once_cell = { workspace = true }
uucore = { workspace = true }
clap_complete = { workspace = true }
clap_mangen = { workspace = true }
clap_complete.workspace = true
clap_mangen.workspace = true
phf = { workspace = true }
selinux = { workspace = true, optional = true }
textwrap = { workspace = true }
Expand Down Expand Up @@ -527,6 +527,10 @@ serde-big-array = "0.5.1"


[build-dependencies]
clap = "4.4"
clap_complete = "4.4"
clap_mangen = "0.2"
uucore = { workspace = true }
phf_codegen = { workspace = true }

[[bin]]
Expand Down
28 changes: 27 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,37 @@
// file that was distributed with this source code.

// spell-checker:ignore (vars) krate

use clap_complete::{generate_to, shells};
use clap_mangen::Man;
use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;

include!("./src/args.rs");

pub fn generate_manpages(_crates: &Vec<String>) -> Result<(), std::io::Error> {
let crates = vec!["arch"];
for one_crate in crates {
let app_name = one_crate;
let outdir = "completion";
let mut cmd = uu_arch::uu_app();

generate_to(shells::Bash, &mut cmd, app_name, outdir)?;
generate_to(shells::Zsh, &mut cmd, app_name, outdir)?;
generate_to(shells::Fish, &mut cmd, app_name, outdir)?;
generate_to(shells::PowerShell, &mut cmd, app_name, outdir)?;
generate_to(shells::Elvish, &mut cmd, app_name, outdir)?;

let file = Path::new("man-page").join(app_name.to_owned() + ".1");
std::fs::create_dir_all("man-page")?;
let mut file = File::create(file)?;

Man::new(cmd).render(&mut file)?;
}
Ok(())
}

pub fn main() {
const ENV_FEATURE_PREFIX: &str = "CARGO_FEATURE_";
const FEATURE_PREFIX: &str = "feat_";
Expand Down Expand Up @@ -39,6 +64,7 @@ pub fn main() {
}
}
crates.sort();
generate_manpages(&crates).unwrap();

let mut mf = File::create(Path::new(&out_dir).join("uutils_map.rs")).unwrap();

Expand Down
35 changes: 35 additions & 0 deletions completion/_arch
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#compdef arch

autoload -U is-at-least

_arch() {
typeset -A opt_args
typeset -a _arguments_options
local ret=1

if is-at-least 5.2; then
_arguments_options=(-s -S -C)
else
_arguments_options=(-s -C)
fi

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
&& ret=0
}

(( $+functions[_arch_commands] )) ||
_arch_commands() {
local commands; commands=()
_describe -t commands 'arch commands' commands "$@"
}

if [ "$funcstack[1]" = "_arch" ]; then
_arch "$@"
else
compdef _arch arch
fi
34 changes: 34 additions & 0 deletions completion/_arch.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

using namespace System.Management.Automation
using namespace System.Management.Automation.Language

Register-ArgumentCompleter -Native -CommandName 'arch' -ScriptBlock {
param($wordToComplete, $commandAst, $cursorPosition)

$commandElements = $commandAst.CommandElements
$command = @(
'arch'
for ($i = 1; $i -lt $commandElements.Count; $i++) {
$element = $commandElements[$i]
if ($element -isnot [StringConstantExpressionAst] -or
$element.StringConstantType -ne [StringConstantType]::BareWord -or
$element.Value.StartsWith('-') -or
$element.Value -eq $wordToComplete) {
break
}
$element.Value
}) -join ';'

$completions = @(switch ($command) {
'arch' {
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
[CompletionResult]::new('-V', 'V ', [CompletionResultType]::ParameterName, 'Print version')
[CompletionResult]::new('--version', 'version', [CompletionResultType]::ParameterName, 'Print version')
break
}
})

$completions.Where{ $_.CompletionText -like "$wordToComplete*" } |
Sort-Object -Property ListItemText
}
38 changes: 38 additions & 0 deletions completion/arch.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
_arch() {
local i cur prev opts cmd
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
cmd=""
opts=""

for i in ${COMP_WORDS[@]}
do
case "${cmd},${i}" in
",$1")
cmd="arch"
;;
*)
;;
esac
done

case "${cmd}" in
arch)
opts="-h -V --help --version"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
case "${prev}" in
*)
COMPREPLY=()
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
;;
esac
}

complete -F _arch -o nosort -o bashdefault -o default arch
28 changes: 28 additions & 0 deletions completion/arch.elv
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

use builtin;
use str;

set edit:completion:arg-completer[arch] = {|@words|
fn spaces {|n|
builtin:repeat $n ' ' | str:join ''
}
fn cand {|text desc|
edit:complex-candidate $text &display=$text' '(spaces (- 14 (wcswidth $text)))$desc
}
var command = 'arch'
for word $words[1..-1] {
if (str:has-prefix $word '-') {
break
}
set command = $command';'$word
}
var completions = [
&'arch'= {
cand -h 'Print help'
cand --help 'Print help'
cand -V 'Print version'
cand --version 'Print version'
}
]
$completions[$command]
}
2 changes: 2 additions & 0 deletions completion/arch.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
complete -c arch -s h -l help -d 'Print help'
complete -c arch -s V -l version -d 'Print version'
20 changes: 20 additions & 0 deletions man-page/arch.1
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.ie \n(.g .ds Aq \(aq
.el .ds Aq '
.TH /home/n4n5/Documents/github/coreutils/target/debug/build/coreutils-5760bf6c2a424d19/build-script-build 1 "/home/n4n5/Documents/github/coreutils/target/debug/build/coreutils-5760bf6c2a424d19/build-script-build 0.0.27"
.SH NAME
/home/n4n5/Documents/github/coreutils/target/debug/build/coreutils\-5760bf6c2a424d19/build\-script\-build \- Display machine architecture
.SH SYNOPSIS
\fB/home/n4n5/Documents/github/coreutils/target/debug/build/coreutils\-5760bf6c2a424d19/build\-script\-build\fR [\fB\-h\fR|\fB\-\-help\fR] [\fB\-V\fR|\fB\-\-version\fR]
.SH DESCRIPTION
Display machine architecture
.SH OPTIONS
.TP
\fB\-h\fR, \fB\-\-help\fR
Print help
.TP
\fB\-V\fR, \fB\-\-version\fR
Print version
.SH EXTRA
Determine architecture name for current machine.
.SH VERSION
v0.0.27
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod uu_arch {
include!("./uu/arch/src/mod.rs");
}
6 changes: 6 additions & 0 deletions src/uu/arch/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ platform-info = { workspace = true }
clap = { workspace = true }
uucore = { workspace = true }

[build-dependencies]
clap = "4.4"
clap_complete = "4.4"
clap_mangen = "0.2"
uucore = { workspace = true }

[[bin]]
name = "arch"
path = "src/main.rs"
15 changes: 1 addition & 14 deletions src/uu/arch/src/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,14 @@

use platform_info::*;

use clap::{crate_version, Command};
use uucore::error::{UResult, USimpleError};
use uucore::{help_about, help_section};

static ABOUT: &str = help_about!("arch.md");
static SUMMARY: &str = help_section!("after help", "arch.md");

#[uucore::main]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
uu_app().try_get_matches_from(args)?;
crate::uu_app().try_get_matches_from(args)?;

let uts = PlatformInfo::new().map_err(|_e| USimpleError::new(1, "cannot get system name"))?;

println!("{}", uts.machine().to_string_lossy().trim());
Ok(())
}

pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.after_help(SUMMARY)
.infer_long_args(true)
}
17 changes: 17 additions & 0 deletions src/uu/arch/src/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

use clap::{crate_version, Command};
use uucore::{help_about, help_section};
static ABOUT: &str = help_about!("arch.md");
static SUMMARY: &str = help_section!("after help", "arch.md");

pub fn uu_app() -> Command {
Command::new(uucore::util_name())
.version(crate_version!())
.about(ABOUT)
.after_help(SUMMARY)
.infer_long_args(true)
}
19 changes: 14 additions & 5 deletions src/uucore_procs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,22 @@ fn get_argument(input: &[TokenTree], index: usize, name: &str) -> String {
fn read_help(filename: &str) -> String {
let mut content = String::new();

let mut path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let parent_path = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());

let mut path = PathBuf::from(parent_path.clone());

Check failure on line 157 in src/uucore_procs/src/lib.rs

View workflow job for this annotation

GitHub Actions / Style and Lint (ubuntu-22.04, unix)

ERROR: `cargo clippy`: useless conversion to the same type: `std::path::PathBuf` (file:'src/uucore_procs/src/lib.rs', line:157)
path.push(filename);

File::open(path)
.unwrap()
.read_to_string(&mut content)
.unwrap();
let mut file_state = File::open(&path);
if let Err(ref _err_file) = file_state {
let name = path.as_path().file_stem().unwrap();
let path = parent_path.join("src").join("uu").join(name).join(filename);
file_state = File::open(&path);
if let Err(err_file) = file_state {
panic!("Error opening file ({:?}): {:?}", path, err_file);
}
}

file_state.unwrap().read_to_string(&mut content).unwrap();

content
}

0 comments on commit 724eca9

Please sign in to comment.