Skip to content

Commit

Permalink
v0.5, consistent display
Browse files Browse the repository at this point in the history
  • Loading branch information
dandyvica committed Jan 5, 2025
1 parent 030e23b commit 13defdf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ chrono = "0.4.38"
clap = "4.5.9"
colored = "2.2.0"
enum_from = { git = "https://github.com/dandyvica/enum_from.git" }
handlebars = "6.2.0"
handlebars = "6.3.0"
http = "1.0.0"
idna = "1.0.3"
lazy_static = "1.4.0"
Expand Down
7 changes: 5 additions & 2 deletions doc/usage_examples.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ $ dqy A AAAA MX TXT NSEC3 www.example.com @1.1.1.1 --tls
# Use DoH for a resolver supporting DNS over HTTPS:
$ dqy A www.google.com @https://cloudflare-dns.com/dns-query --doh

# Show OPT record
$ dqy AAAA www.google.com --show-opt
# Use DoQ
$ dqy A www.google.com @quic://dns.adguard.com

# Don't use colors in output
$ dqy A AAAA MX TXT www.example.com --no-colors

# Don't ask for recursion
$ dqy AAAA www.google.com --no-recurse

# Use DNSSEC
$ dqy NS . --dnssec

# IDNA support
$ dqy AAAA ουτοπία.δπθ.gr
$ dqy 中国.asia
4 changes: 2 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::transport::{endpoint::EndPoint, TransportOptions};

// value of the environment variable for flags if any
const ENV_FLAGS: &str = "DQY_FLAGS";
const VERSION: &str = "v0.4.0";
const VERSION: &str = "v0.5.0";

// help to set or unset flags
macro_rules! set_unset_flag {
Expand Down Expand Up @@ -828,7 +828,7 @@ Caveat: all options starting with a dash (-) should be placed after optional [TY
// manage display options
//───────────────────────────────────────────────────────────────────────────────────
options.display.align_names = matches.get_flag("align");
options.display.headers = matches.get_flag("headers");
options.display.show_headers = matches.get_flag("headers");
options.display.json = matches.get_flag("json");
options.display.json_pretty = matches.get_flag("json-pretty");
// options.display.no_additional = matches.get_flag("no-add");
Expand Down
16 changes: 13 additions & 3 deletions src/dns/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::rfc::{query::Query, response::Response, response_code::ResponseCode};
use log::{error, trace};
use serde::Serialize;

use crate::show::{DisplayOptions, QueryInfo, Show, ShowAll};
use crate::show::{header_section, DisplayOptions, QueryInfo, Show, ShowAll};

#[derive(Debug, Serialize)]
pub struct Message {
Expand Down Expand Up @@ -86,7 +86,7 @@ impl Show for Message {
}

//───────────────────────────────────────────────────────────────────────────────────
// convenient struct for holding al messages
// convenient struct for holding all messages
//───────────────────────────────────────────────────────────────────────────────────
#[derive(Debug, Serialize)]
pub struct MessageList(Vec<Message>);
Expand Down Expand Up @@ -120,7 +120,7 @@ impl fmt::Display for MessageList {
}

impl ShowAll for MessageList {
fn show_all(&self, display_options: &DisplayOptions, info: QueryInfo) {
fn show_all(&self, display_options: &mut DisplayOptions, info: QueryInfo) {
//───────────────────────────────────────────────────────────────────────────────────
// JSON
//───────────────────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -152,7 +152,17 @@ impl ShowAll for MessageList {
// we only have 1 message
let msg = &self[0];
let resp = msg.response();

// when we only have one message, we print out a dig-like info
display_options.sho_resp_header = true;
display_options.show_headers = true;
display_options.show_all = true;

resp.show(display_options, None);

// print out stats
println!("{}", header_section("STATS", None));
println!("{}", info);
}
//───────────────────────────────────────────────────────────────────────────────────
// when several messages, just print out the ANSWER
Expand Down
16 changes: 12 additions & 4 deletions src/dns/rfc/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,23 @@ impl<'a> FromNetworkOrder<'a> for Response {

impl Show for Response {
fn show(&self, display_options: &DisplayOptions, max_length: Option<usize>) {
const HEADER_LENGTH: usize = 80;
// const HEADER_LENGTH: usize = 80;

//───────────────────────────────────────────────────────────────────────────────────
// Response HEADER
//───────────────────────────────────────────────────────────────────────────────────
if display_options.sho_resp_header {
println!("{}", header_section("Response HEADER", None));
println!("{}\n", self.header);
}

//───────────────────────────────────────────────────────────────────────────────────
// ANSWER
//───────────────────────────────────────────────────────────────────────────────────
if self.header.an_count > 0 {
debug_assert!(self.answer.is_some());

if display_options.headers {
if display_options.show_headers {
println!("{}", header_section("ANSWER", None));
}
self.answer.as_ref().unwrap().show(display_options, max_length);
Expand All @@ -300,7 +308,7 @@ impl Show for Response {
if self.header.ns_count > 0 && display_options.show_all {
debug_assert!(self.authority.is_some());

if display_options.headers {
if display_options.show_headers {
println!("\n{}", header_section("AUTHORATIVE", None));
}
self.authority.as_ref().unwrap().show(display_options, max_length);
Expand All @@ -312,7 +320,7 @@ impl Show for Response {
if self.header.ar_count > 0 && display_options.show_all {
debug_assert!(self.additional.is_some());

if display_options.headers {
if display_options.show_headers {
println!("\n{}", header_section("ADDITIONAL", None));
}
self.additional.as_ref().unwrap().show(display_options, max_length);
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ fn run() -> error::Result<()> {
if let Some(tpl) = &options.display.hb_tpl {
handlebars::render(&messages, &info, tpl);
} else {
messages.show_all(&options.display, info);
messages.show_all(&mut options.display, info);
}
//messages.show_all(&options.display, info);

Expand Down
11 changes: 7 additions & 4 deletions src/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ pub struct QueryInfo {
impl fmt::Display for QueryInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(peer) = self.netinfo.peer {
write!(f, "\nendpoint: {} ({})\n", peer, self.mode)?;
writeln!(f, "endpoint: {} ({})", peer, self.mode)?;
}
writeln!(f, "elapsed: {} ms", self.elapsed)?;
write!(
writeln!(
f,
"sent:{}, received:{} bytes",
self.netinfo.sent, self.netinfo.received
Expand Down Expand Up @@ -63,7 +63,7 @@ pub struct DisplayOptions {
pub no_authorative: bool,

// true if we want header for each section
pub headers: bool,
pub show_headers: bool,

// show OPT record if any
pub show_opt: bool,
Expand All @@ -86,6 +86,9 @@ pub struct DisplayOptions {
// show all information possible
pub show_all: bool,

// show response header
pub sho_resp_header: bool,

// Lua code if specified
#[cfg(feature = "mlua")]
pub lua_code: Option<String>,
Expand All @@ -107,7 +110,7 @@ pub trait Show: Display {
fn show(&self, display_options: &DisplayOptions, length: Option<usize>);
}
pub trait ShowAll: Display {
fn show_all(&self, display_options: &DisplayOptions, info: QueryInfo);
fn show_all(&self, display_options: &mut DisplayOptions, info: QueryInfo);
}

pub trait ToColor: Display {
Expand Down

0 comments on commit 13defdf

Please sign in to comment.