feat(app): display the documentation references

This commit is contained in:
Orhun Parmaksız 2021-11-17 22:14:39 +03:00
parent d4768c5695
commit 4039c09f1a
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
4 changed files with 36 additions and 32 deletions

View file

@ -17,7 +17,7 @@ lazy_static! {
},
Parser {
glob_path: "networking/*-sysctl.rst",
regex: RegexBuilder::new("^([a-zA-Z0-9_/-]+)[ ]-[ ][a-zA-Z].*$")
regex: RegexBuilder::new("^([a-zA-Z0-9_/-]+[ ]-[ ][a-zA-Z].*)$")
.multi_line(true)
.build()
.expect("failed to compile regex"),

View file

@ -6,10 +6,9 @@ use rayon::prelude::*;
use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
use std::io::Write;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::result::Result as StdResult;
use sysctl::{Ctl, CtlFlags, CtlIter, Sysctl as SysctlImpl};
use systeroid_parser::document::Document;
/// Sections of the sysctl documentation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
@ -86,8 +85,10 @@ pub struct Parameter {
pub description: Option<String>,
/// Section of the kernel parameter.
pub section: Section,
/// Parsed document about the kernel parameter.
pub document: Option<Document>,
/// Documentation path.
pub docs_path: PathBuf,
/// Title of the kernel parameter taken from the documentation.
pub docs_title: Option<String>,
}
impl Parameter {
@ -116,7 +117,7 @@ impl Parameter {
}
/// Prints the kernel parameter to given output.
pub fn display<W: Write>(&self, config: &ColorConfig, output: &mut W) -> Result<()> {
pub fn display_value<W: Write>(&self, config: &ColorConfig, output: &mut W) -> Result<()> {
if !config.no_color {
writeln!(
output,
@ -131,8 +132,24 @@ impl Parameter {
Ok(())
}
/// Prints the description of the kernel parameter to the given output.
pub fn display_documentation<W: Write>(&self, output: &mut W) -> Result<()> {
if let Some(title) = &self.docs_title {
writeln!(output, "{}", title)?;
}
writeln!(
output,
"\n{}\n",
self.description
.as_deref()
.unwrap_or("No documentation available")
)?;
writeln!(output, "Reference: {}", self.docs_path.to_string_lossy())?;
Ok(())
}
/// Sets a new value for the kernel parameter.
pub fn update<W: Write>(
pub fn update_value<W: Write>(
&mut self,
new_value: &str,
config: &ColorConfig,
@ -141,7 +158,7 @@ impl Parameter {
let ctl = Ctl::new(&self.name)?;
let new_value = ctl.set_value_string(new_value)?;
self.value = new_value;
self.display(config, output)
self.display_value(config, output)
}
}
@ -156,7 +173,8 @@ impl<'a> TryFrom<&'a Ctl> for Parameter {
.ok()
.and_then(|v| (v == "[N/A]").then(|| None)?),
section: Section::from(ctl.name()?),
document: None,
docs_path: PathBuf::new(),
docs_title: None,
})
}
}
@ -228,7 +246,8 @@ impl Sysctl {
})
{
param.description = Some(paragraph.contents.to_owned());
param.document = Some(document.clone());
param.docs_title = Some(paragraph.title.to_owned());
param.docs_path = document.path.clone();
continue;
}
}

View file

@ -24,16 +24,8 @@ impl Paragraph {
) -> Result<Vec<Self>, Error> {
let mut paragraphs = Vec::new();
for (i, captures) in capture_group.iter().enumerate() {
let title_capture = captures
.iter()
.last()
.flatten()
.ok_or(Error::CaptureError)?;
let content_capture = captures
.iter()
.next()
.flatten()
.ok_or(Error::CaptureError)?;
let content_capture = captures.get(0).ok_or(Error::CaptureError)?;
let title_capture = captures.get(1).ok_or(Error::CaptureError)?;
paragraphs.push(Paragraph::new(
title_capture.as_str().trim().to_string(),
if let Some(next_capture) = capture_group.get(i + 1) {

View file

@ -1,4 +1,4 @@
use std::io::{self, Stdout, Write};
use std::io::{self, Stdout};
use systeroid_core::config::Config;
use systeroid_core::error::Result;
use systeroid_core::sysctl::Sysctl;
@ -30,20 +30,13 @@ impl<'a> App<'a> {
self.sysctl
.parameters
.iter()
.try_for_each(|parameter| parameter.display(&self.config.color, &mut self.stdout))
.try_for_each(|parameter| parameter.display_value(&self.config.color, &mut self.stdout))
}
/// Displays the documentation of a parameter.
pub fn display_documentation(&mut self, param_name: &str) -> Result<()> {
if let Some(parameter) = self.sysctl.get_parameter(param_name) {
writeln!(
self.stdout,
"{}",
parameter
.description
.as_deref()
.unwrap_or("No documentation available")
)?;
parameter.display_documentation(&mut self.stdout)?;
}
Ok(())
}
@ -63,9 +56,9 @@ impl<'a> App<'a> {
};
if let Some(parameter) = self.sysctl.get_parameter(&param_name) {
if let Some(new_value) = new_value {
parameter.update(&new_value, &self.config.color, &mut self.stdout)?;
parameter.update_value(&new_value, &self.config.color, &mut self.stdout)?;
} else {
parameter.display(&self.config.color, &mut self.stdout)?;
parameter.display_value(&self.config.color, &mut self.stdout)?;
}
}
Ok(())