aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGuillaume Pasquet <dev@etenil.net>2022-02-24 23:39:33 +0000
committerGuillaume Pasquet <dev@etenil.net>2022-02-24 23:39:33 +0000
commit3517f9d9ff69eebae14e32a3b1550bd920ea3238 (patch)
treeb56d16e0b757bc8868e9f49a7cc20c0ae546c6ad /src
parent649299965d2368f65b45d7f51b89eb69b491c7a4 (diff)
Fix #5: Add JSON syntax coloring
Diffstat (limited to 'src')
-rw-r--r--src/barbfile.rs2
-rw-r--r--src/executor.rs13
-rw-r--r--src/main.rs11
-rw-r--r--src/output.rs31
4 files changed, 43 insertions, 14 deletions
diff --git a/src/barbfile.rs b/src/barbfile.rs
index 19a24cd..66a8756 100644
--- a/src/barbfile.rs
+++ b/src/barbfile.rs
@@ -119,7 +119,7 @@ impl BarbFile {
pub fn url(&self) -> &String {
&self.preamble.url
}
-
+
pub fn filter(&self) -> &Option<String> {
&self.preamble.filter
}
diff --git a/src/executor.rs b/src/executor.rs
index 4cfa59e..f6ca4f5 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -5,7 +5,6 @@ use std::collections::HashMap;
use ureq;
use ureq::Error as UreqError;
-
pub struct Context {
vars: HashMap<String, String>,
}
@@ -60,7 +59,10 @@ impl Executor {
}
fn make_req(&self, bfile: &BarbFile, output: &BarbOutput) -> ureq::Request {
- output.req(bfile.method_as_string(), self.context.substitute(&bfile.url()));
+ output.req(
+ bfile.method_as_string(),
+ self.context.substitute(&bfile.url()),
+ );
let mut req = ureq::request(
bfile.method_as_string().as_str(),
self.context.substitute(&bfile.url()).as_str(),
@@ -68,15 +70,12 @@ impl Executor {
for header in bfile.headers() {
let hdr_val = self.context.substitute(header.value());
- req = req.set(
- header.name(),
- hdr_val.as_str(),
- );
+ req = req.set(header.name(), hdr_val.as_str());
output.req_hdr(header.name().to_string(), hdr_val);
}
output.end_req();
-
+
req
}
diff --git a/src/main.rs b/src/main.rs
index a02938a..b7ce5ba 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -77,11 +77,18 @@ fn run_file(args: &Args, executor: &mut Executor, file_name: &String) -> Result<
output.status(response.status(), response.status_text());
for header_name in response.headers_names() {
- output.resp_hdr(header_name.to_string(), response.header(header_name.as_str()).unwrap());
+ output.resp_hdr(
+ header_name.to_string(),
+ response.header(header_name.as_str()).unwrap(),
+ );
}
output.end_resp_hdr();
- output.body(apply_filters(args, &bfile, response.into_string().unwrap())?);
+ output.body(apply_filters(
+ args,
+ &bfile,
+ response.into_string().unwrap(),
+ )?);
Ok(())
}
diff --git a/src/output.rs b/src/output.rs
index d181d0f..d7ab74e 100644
--- a/src/output.rs
+++ b/src/output.rs
@@ -1,4 +1,5 @@
use colored::*;
+use colored_json::prelude::*;
use jsonformat::{format_json, Indentation};
use std::fmt::Display;
@@ -13,11 +14,21 @@ pub struct BarbOutput {
impl BarbOutput {
fn _print_header<T, I>(&self, name: T, value: I)
- where T: Display, I: Display{
+ where
+ T: Display,
+ I: Display,
+ {
println!("{}: {}", name, value);
}
- pub fn new(request: bool, req_headers: bool, headers: bool, body: bool, raw_body: bool, color: bool) -> BarbOutput {
+ pub fn new(
+ request: bool,
+ req_headers: bool,
+ headers: bool,
+ body: bool,
+ raw_body: bool,
+ color: bool,
+ ) -> BarbOutput {
BarbOutput {
request,
req_headers,
@@ -88,16 +99,28 @@ impl BarbOutput {
}
}
+ fn _format_body(&self, body: String) -> String {
+ if self.raw_body {
+ return body;
+ }
+
+ let formatted = format_json(body.as_str(), Indentation::Default);
+ match self.color {
+ true => formatted.to_colored_json_auto().unwrap_or(formatted),
+ _ => formatted,
+ }
+ }
+
pub fn body(&self, body: String) {
if !self.body {
return;
}
-
+
println!(
"{}",
match self.raw_body {
true => body,
- false => format_json(body.as_str(), Indentation::Default),
+ _ => self._format_body(body),
}
);
}