Skip to content

Commit

Permalink
upgrade syntect to 5
Browse files Browse the repository at this point in the history
  • Loading branch information
tyrchen committed Aug 30, 2022
1 parent 2599aa5 commit 59d3363
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 33 deletions.
27 changes: 11 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions cli-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xreq-cli-utils"
version = "0.3.0"
version = "0.3.1"
edition = "2021"
license = "MIT"
documentation = "https://docs.rs/xreq-cli-utils"
Expand All @@ -17,6 +17,6 @@ categories = ["development-tools"]
[dependencies]
anyhow = "1.0.62"
atty = "0.2.14"
syntect = "4"
syntect = "5.0.0"

xreq-lib = { version = "0.4.0", path = "../requester" }
8 changes: 5 additions & 3 deletions cli-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ pub fn get_default_config(name: &str) -> Result<PathBuf> {
Err(anyhow::anyhow!("Config file not found. You can either specify it with the --config option or put it in one of the following locations: {}", paths.join(", ")))
}

pub fn print_syntect(output: &mut Vec<String>, s: String, ext: &str) {
pub fn print_syntect(output: &mut Vec<String>, s: String, ext: &str) -> Result<()> {
if atty::isnt(atty::Stream::Stdout) {
output.push(s);
return;
return Ok(());
}

// Load these once at the start of your program
Expand All @@ -69,8 +69,10 @@ pub fn print_syntect(output: &mut Vec<String>, s: String, ext: &str) {
let syntax = ps.find_syntax_by_extension(ext).unwrap();
let mut h = HighlightLines::new(syntax, &ts.themes["base16-ocean.dark"]);
for line in LinesWithEndings::from(&s) {
let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
let ranges: Vec<(Style, &str)> = h.highlight_line(line, &ps)?;
let escaped = as_24_bit_terminal_escaped(&ranges[..], false);
output.push(escaped);
}

Ok(())
}
6 changes: 3 additions & 3 deletions xdiff/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xdiff"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
license = "MIT"
documentation = "https://docs.rs/xdiff"
Expand All @@ -16,11 +16,11 @@ categories = ["development-tools"]
[dependencies]
anyhow = "1.0.62"
atty = "0.2.14"
clap = { version = "3.2.17", features = ["derive"] }
clap = { version = "3.2.18", features = ["derive"] }
dialoguer = { version = "0.10.2", features = ["history", "completion"] }
tokio = { version = "1.20.1", features = ["full"] }
serde_json = "1.0.85"
serde_yaml = "0.9.10"

xreq-cli-utils = { version = "0.3.0", path = "../cli-utils" }
xreq-cli-utils = { version = "0.3.1", path = "../cli-utils" }
xreq-lib = { version = "0.4.0", path = "../requester" }
2 changes: 1 addition & 1 deletion xdiff/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async fn parse(output: &mut Vec<String>) -> Result<()> {
let result = serde_yaml::to_string(&config)?;

output.push("---\n".to_string());
print_syntect(output, result, "yaml");
print_syntect(output, result, "yaml")?;
Ok(())
}

Expand Down
6 changes: 3 additions & 3 deletions xreq/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "xreq"
version = "0.4.1"
version = "0.4.2"
edition = "2021"
license = "MIT"
documentation = "https://docs.rs/xreq"
Expand All @@ -16,13 +16,13 @@ categories = ["development-tools"]
[dependencies]
anyhow = "1.0.62"
atty = "0.2.14"
clap = { version = "3.2.17", features = ["derive"] }
clap = { version = "3.2.18", features = ["derive"] }
colored = "2.0.0"
dialoguer = { version = "0.10.2", features = ["history", "completion"] }
mime = "0.3.16"
serde_json = "1.0.85"
serde_yaml = "0.9.10"
tokio = { version = "1.20.1", features = ["full"] }

xreq-cli-utils = { version = "0.3.0", path = "../cli-utils" }
xreq-cli-utils = { version = "0.3.1", path = "../cli-utils" }
xreq-lib = { version = "0.4.0", path = "../requester" }
13 changes: 8 additions & 5 deletions xreq/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn parse(output: &mut Vec<String>, ParseArgs { profile, url }: ParseArgs) -> Res
let result = serde_yaml::to_string(&config)?;

output.push("---\n".to_string());
print_syntect(output, result, "yaml");
print_syntect(output, result, "yaml")?;
Ok(())
}

Expand All @@ -117,7 +117,7 @@ async fn run(output: &mut Vec<String>, args: RunArgs) -> Result<()> {
let mime = get_content_type(&resp);
let body = resp.text().await?;

print_body(output, mime, body);
print_body(output, mime, body)?;

Ok(())
}
Expand All @@ -135,16 +135,19 @@ fn print_headers(output: &mut Vec<String>, resp: &Response) {
output.push("\n".into());
}

fn print_body(output: &mut Vec<String>, m: Option<Mime>, body: String) {
fn print_body(output: &mut Vec<String>, m: Option<Mime>, body: String) -> Result<()> {
match m {
Some(v) if v.essence_str() == mime::APPLICATION_JSON => {
let json: Value = serde_json::from_str(&body).unwrap();
let body = serde_json::to_string_pretty(&json).unwrap();
print_syntect(output, body, "json");
print_syntect(output, body, "json")
}
Some(v) if v == mime::TEXT_HTML => print_syntect(output, body, "html"),

_ => output.push(format!("{}\n", body)),
_ => {
output.push(format!("{}\n", body));
Ok(())
}
}
}

Expand Down

0 comments on commit 59d3363

Please sign in to comment.