mirror of
https://github.com/orhun/systeroid
synced 2024-07-21 10:25:00 +00:00
fix(app): manually construct output tree to avoid separator conflict
This commit is contained in:
parent
457cedc40f
commit
5ce0fde69b
|
@ -4,7 +4,6 @@ use crate::sysctl::display::DisplayType;
|
|||
use crate::sysctl::section::Section;
|
||||
use colored::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{self, Display, Formatter};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use sysctl::{Ctl, Sysctl as SysctlImpl};
|
||||
|
@ -44,12 +43,6 @@ impl<'a> TryFrom<&'a Ctl> for Parameter {
|
|||
}
|
||||
}
|
||||
|
||||
impl Display for Parameter {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
write!(f, "{} = {}", self.name, self.value.replace('\n', " "))
|
||||
}
|
||||
}
|
||||
|
||||
impl Parameter {
|
||||
/// Returns the absolute name of the parameter, without the sections.
|
||||
pub fn absolute_name(&self) -> Option<&str> {
|
||||
|
|
|
@ -14,12 +14,12 @@ pub struct TreeNode {
|
|||
/// Value of the node.
|
||||
value: String,
|
||||
/// Childs of the node.
|
||||
childs: Vec<TreeNode>,
|
||||
pub childs: Vec<TreeNode>,
|
||||
}
|
||||
|
||||
impl TreeNode {
|
||||
/// Adds new child nodes to the tree node.
|
||||
fn add<'a, I: Iterator<Item = &'a str>>(&mut self, values: &mut I) {
|
||||
pub fn add<'a, I: Iterator<Item = &'a str>>(&mut self, values: &mut I) {
|
||||
if let Some(value) = values.next() {
|
||||
let mut found = false;
|
||||
for child in self.childs.iter_mut() {
|
||||
|
@ -88,7 +88,12 @@ pub struct Tree {
|
|||
|
||||
impl Tree {
|
||||
/// Constructs a new instance.
|
||||
pub fn new<I, O>(input: &mut I, seperator: char) -> Self
|
||||
pub fn new(nodes: Vec<TreeNode>) -> Self {
|
||||
Self { nodes }
|
||||
}
|
||||
|
||||
/// Constructs a new instance from given input.
|
||||
pub fn from_input<I, O>(input: &mut I, seperator: char) -> Self
|
||||
where
|
||||
I: Iterator<Item = O>,
|
||||
O: Display,
|
||||
|
@ -98,7 +103,7 @@ impl Tree {
|
|||
let mut components = line.split(seperator);
|
||||
root.add(&mut components);
|
||||
}
|
||||
Self { nodes: root.childs }
|
||||
Self::new(root.childs)
|
||||
}
|
||||
|
||||
/// Prints the full tree to the given output.
|
||||
|
@ -115,7 +120,7 @@ mod tests {
|
|||
use super::*;
|
||||
|
||||
fn test_single_tree_creation(lines: &[&str], seperator: char, expected_tree: TreeNode) {
|
||||
let tree = Tree::new(&mut lines.iter(), seperator);
|
||||
let tree = Tree::from_input(&mut lines.iter(), seperator);
|
||||
assert_eq!(1, tree.nodes.len());
|
||||
assert_eq!(expected_tree, tree.nodes[0]);
|
||||
}
|
||||
|
@ -194,7 +199,7 @@ mod tests {
|
|||
childs: vec![b],
|
||||
};
|
||||
|
||||
let tree = Tree::new(&mut lines.iter(), '/');
|
||||
let tree = Tree::from_input(&mut lines.iter(), '/');
|
||||
assert_eq!(2, tree.nodes.len());
|
||||
assert_eq!(a, tree.nodes[0]);
|
||||
assert_eq!(c, tree.nodes[1]);
|
||||
|
|
|
@ -8,7 +8,7 @@ use systeroid_core::parsers::KERNEL_DOCS_PATH;
|
|||
use systeroid_core::regex::Regex;
|
||||
use systeroid_core::sysctl::controller::Sysctl;
|
||||
use systeroid_core::sysctl::{DEPRECATED_PARAMS, SYSTEM_PRELOAD};
|
||||
use systeroid_core::tree::Tree;
|
||||
use systeroid_core::tree::{Tree, TreeNode};
|
||||
use systeroid_parser::globwalk;
|
||||
use systeroid_parser::reader;
|
||||
|
||||
|
@ -55,7 +55,23 @@ impl<'a> App<'a> {
|
|||
true
|
||||
});
|
||||
if tree_output {
|
||||
Tree::new(&mut parameters, '.').print(&mut self.stdout)?;
|
||||
let mut root_node = TreeNode::default();
|
||||
parameters.for_each(|parameter| {
|
||||
let mut components = parameter
|
||||
.name
|
||||
.split('.')
|
||||
.map(String::from)
|
||||
.collect::<Vec<String>>();
|
||||
if let Some(last_component) = components.last_mut() {
|
||||
*last_component = format!(
|
||||
"{} = {}",
|
||||
last_component,
|
||||
parameter.value.replace('\n', " ")
|
||||
);
|
||||
}
|
||||
root_node.add(&mut components.iter().map(|v| v.as_ref()));
|
||||
});
|
||||
Tree::new(root_node.childs).print(&mut self.stdout)?;
|
||||
} else {
|
||||
parameters.try_for_each(|parameter| {
|
||||
parameter.display_value(&self.sysctl.config, &mut self.stdout)
|
||||
|
|
Loading…
Reference in a new issue