8 Commits

7 changed files with 137 additions and 58 deletions

2
Cargo.lock generated
View File

@@ -4,4 +4,4 @@ version = 4
[[package]] [[package]]
name = "dbus-prettifier" name = "dbus-prettifier"
version = "0.1.0" version = "1.0.0"

View File

@@ -1,6 +1,14 @@
[package] [package]
name = "dbus-prettifier" name = "dbus-prettifier"
version = "0.1.0" version = "1.0.0"
edition = "2024" edition = "2024"
authors = [ "Nicolás A. Ortega Froysa <nicolas@ortegas.org>" ]
description = "A tool to prettify the output of qdbus."
readme = "./README.md"
homepage = "https://code.ortegas.org/nortega/dbus-prettifier"
repository = "https://code.ortegas.org/nortega/dbus-prettifier"
license = "Zlib"
keywords = [ "dbus", "qdbus", "prettifier" ]
categories = [ "command-line-utilities" ]
[dependencies] [dependencies]

View File

@@ -1,4 +1,4 @@
Copyright (C) [year] Nicolás A. Ortega Froysa <nicolas@ortegas.org> Copyright (C) 2025 Nicolás A. Ortega Froysa <nicolas@ortegas.org>
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages

View File

@@ -9,7 +9,7 @@ file with the output so that it is much more readable.
To run the program, simply run it as follows: To run the program, simply run it as follows:
```console ```console
qdbus-prettifier <in-file> <out-file> qdbus-prettifier <in-file> [out-file]
``` ```
## Building ## Building

BIN
out.txt

Binary file not shown.

View File

@@ -23,71 +23,65 @@
*/ */
use std::env; use std::env;
use std::fs;
use std::io; use std::io;
use std::io::Read;
use std::io::Write;
use std::process; use std::process;
// Read 1KiB of the file at a time. use crate::worker::work;
static BLOCK_SIZE:usize = 1024;
mod worker;
fn print_version() {
println!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
}
fn print_usage() {
println!("Usage: {} <in-file> [out-file]", env!("CARGO_PKG_NAME"));
}
fn print_help() {
print_version();
println!("Usage: \n\
\t{} <in-file> [out-file]\n\
\t{} -h | -v\n\
\n\
OPTIONS:\n\
\tin-file Input file ('-' for stdin)\n\
\tout-file Output file\n\
\t-h Show this help information\n\
\t-v Show version information",
env!("CARGO_PKG_NAME"), env!("CARGO_PKG_NAME"));
}
fn main() -> io::Result<()> { fn main() -> io::Result<()> {
let args:Vec<String> = env::args().collect(); let args:Vec<String> = env::args().collect();
let mut in_path:Option<&str> = None;
let mut out_path:Option<&str> = None;
if args.len() != 3 { if args.len() < 2 || args.len() > 3 {
eprintln!("Usage: {} <in-file> <out-file>", args[0]); eprintln!("Invalid number of arguments. Use -h for more information.");
print_usage();
process::exit(1); process::exit(1);
} }
let in_file = fs::File::open(&args[1])?; for i in &args {
let mut in_reader = io::BufReader::new(in_file); if i == "-h" {
print_help();
let out_file = fs::File::create(&args[2])?; return Ok(());
let mut out_writer = io::BufWriter::new(out_file); } else if i == "-v" {
print_version();
let mut read_buf:[u8; BLOCK_SIZE] = [0; BLOCK_SIZE]; return Ok(());
let mut last_ch:char = '\0'; } else if i.starts_with("-") && i != "-" {
let mut tab_num = 0; eprintln!("Invalid argument '{}'. Use -h for help information.", i);
process::exit(1);
while in_reader.read(&mut read_buf)? > 0 {
for i in read_buf {
let ch = i as char;
match ch {
'[' | '{' => {
tab_num += 1;
out_writer.write(format!("{}\n", ch).as_bytes())?;
for _ in 0..tab_num {
out_writer.write(b" ")?;
}
last_ch = ' ';
},
']' | '}' => {
tab_num -= 1;
out_writer.write(b"\n")?;
for _ in 0..tab_num {
out_writer.write(b" ")?;
}
out_writer.write(format!("{}", ch).as_bytes())?;
last_ch = ch;
},
',' => {
out_writer.write(format!("{}\n", ch).as_bytes())?;
for _ in 0..tab_num {
out_writer.write(b" ")?;
}
last_ch = ' ';
},
_ => {
let ch = ch;
if ch != ' ' || last_ch != ' ' {
out_writer.write(format!("{}", ch).as_bytes())?;
last_ch = ch;
}
},
}
} }
} }
Ok(()) if args[1] != "-" {
in_path = Some(args[1].as_str());
}
if args.len() == 3 {
out_path = Some(args[2].as_str());
}
work(in_path, out_path)
} }

77
src/worker.rs Normal file
View File

@@ -0,0 +1,77 @@
use std::io;
use std::fs;
use std::io::Read;
use std::boxed::Box;
// Read 1KiB of the file at a time.
static BLOCK_SIZE:usize = 1024;
pub fn work(in_path:Option<&str>, out_path:Option<&str>) -> io::Result<()> {
let mut in_reader:Box<dyn io::Read> = match in_path {
Some(x) => {
let in_file = fs::File::open(x)?;
Box::new(io::BufReader::new(in_file))
},
None => {
Box::new(io::stdin())
},
};
let mut out_writer:Box<dyn io::Write> = match out_path {
Some(x) => {
let out_file = fs::File::create(x)?;
Box::new(io::BufWriter::new(out_file))
},
None => {
Box::new(io::stdout())
},
};
let mut read_buf:[u8; BLOCK_SIZE] = [0; BLOCK_SIZE];
let mut last_ch:char = '\0';
let mut tab_num = 0;
while in_reader.read(&mut read_buf)? > 0 {
for i in read_buf {
let ch = i as char;
match ch {
'[' | '{' => {
tab_num += 1;
out_writer.write(format!("{}\n", ch).as_bytes())?;
for _ in 0..tab_num {
out_writer.write(b" ")?;
}
last_ch = ' ';
},
']' | '}' => {
tab_num -= 1;
out_writer.write(b"\n")?;
for _ in 0..tab_num {
out_writer.write(b" ")?;
}
out_writer.write(format!("{}", ch).as_bytes())?;
last_ch = ch;
},
',' => {
out_writer.write(format!("{}\n", ch).as_bytes())?;
for _ in 0..tab_num {
out_writer.write(b" ")?;
}
last_ch = ' ';
},
_ => {
let ch = ch;
if ch != ' ' || last_ch != ' ' {
out_writer.write(format!("{}", ch).as_bytes())?;
last_ch = ch;
}
},
}
}
}
out_writer.flush()?;
Ok(())
}