Skip to content

Commit

Permalink
Add uicolor option (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheZoq2 committed Dec 4, 2022
1 parent eeea9f5 commit 3ca8490
Show file tree
Hide file tree
Showing 50 changed files with 460 additions and 289 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added

- Add flag `--uicolor` to specify the color to draw UI element text, like the Reset view button. [#271](https://github.com/jonhoo/inferno/pull/271)

### Changed
- Updated `ahash` to version 0.8
- Updated `quick-xml` to version 0.26

- Bumped `clap` to 3.2.0

### Removed

## [0.11.12] - 2022-10-24
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ num-format = { version = "0.4.3", default-features = false }
quick-xml = { version = "0.26", default-features = false }
rgb = "0.8.13"
str_stack = "0.1"
clap = { version = "3.0.1", optional = true, features = ["derive"] }
clap = { version = "3.2.1", optional = true, features = ["derive"] }
once_cell = "1.12.0"

[dev-dependencies]
Expand Down
17 changes: 16 additions & 1 deletion src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::io;
use std::path::{Path, PathBuf};

use env_logger::Env;
use inferno::flamegraph::color::{BackgroundColor, PaletteMap, SearchColor, StrokeColor};
use inferno::flamegraph::color::{
parse_hex_color, BackgroundColor, Color, PaletteMap, SearchColor, StrokeColor,
};
use inferno::flamegraph::{self, defaults, Direction, Options, Palette, TextTruncateDirection};

#[cfg(feature = "nameattr")]
Expand Down Expand Up @@ -131,6 +133,18 @@ struct Opt {
)]
fontwidth: f64,

/// Color of UI text such as the search and reset zoom buttons
#[clap(
long = "uicolor",
default_value = defaults::UI_COLOR,
value_parser = |s: &str| {
parse_hex_color(s)
.ok_or_else(|| format!("Expected a color in hexadecimal format, got: {}", s))
},
value_name = "#RRGGBB"
)]
uicolor: Color,

/// Height of each frame
#[clap(
long = "height",
Expand Down Expand Up @@ -264,6 +278,7 @@ impl<'a> Opt {
options.factor = self.factor;
options.search_color = self.search_color;
options.stroke_color = self.stroke_color;
options.uicolor = self.uicolor;
(self.infiles, options)
}

Expand Down
44 changes: 15 additions & 29 deletions src/flamegraph/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ impl FromStr for BackgroundColor {
"blue" => Ok(BackgroundColor::Blue),
"green" => Ok(BackgroundColor::Green),
"grey" => Ok(BackgroundColor::Grey),
flat => parse_flat_bgcolor(flat)
flat => parse_hex_color(flat)
.map(BackgroundColor::Flat)
.ok_or_else(|| format!("unknown background color: {}", flat)),
}
Expand All @@ -154,7 +154,8 @@ macro_rules! u8_from_hex_iter {
};
}

fn parse_flat_bgcolor(s: &str) -> Option<Color> {
/// Parses a string as a hex color, returning None if it is an invalid color
pub fn parse_hex_color(s: &str) -> Option<Color> {
if !s.starts_with('#') || (s.len() != 7) {
None
} else {
Expand All @@ -176,7 +177,7 @@ impl FromStr for SearchColor {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
parse_flat_bgcolor(s)
parse_hex_color(s)
.map(SearchColor)
.ok_or_else(|| format!("unknown color: {}", s))
}
Expand Down Expand Up @@ -204,7 +205,7 @@ impl FromStr for StrokeColor {
if s == "none" {
return Ok(StrokeColor::None);
}
parse_flat_bgcolor(s)
parse_hex_color(s)
.map(StrokeColor::Color)
.ok_or_else(|| format!("unknown color: {}", s))
}
Expand Down Expand Up @@ -466,36 +467,21 @@ pub(super) fn bgcolor_for<'a>(
#[cfg(test)]
mod tests {
use super::namehash;
use super::parse_flat_bgcolor;
use super::parse_hex_color;
use super::Color;
use pretty_assertions::assert_eq;

#[test]
fn bgcolor_parse_test() {
assert_eq!(
parse_flat_bgcolor("#ffffff"),
Some(color!(0xff, 0xff, 0xff))
);
assert_eq!(
parse_flat_bgcolor("#000000"),
Some(color!(0x00, 0x00, 0x00))
);
assert_eq!(
parse_flat_bgcolor("#abcdef"),
Some(color!(0xab, 0xcd, 0xef))
);
assert_eq!(
parse_flat_bgcolor("#123456"),
Some(color!(0x12, 0x34, 0x56))
);
assert_eq!(
parse_flat_bgcolor("#789000"),
Some(color!(0x78, 0x90, 0x00))
);
assert_eq!(parse_flat_bgcolor("ffffff"), None);
assert_eq!(parse_flat_bgcolor("#fffffff"), None);
assert_eq!(parse_flat_bgcolor("#xfffff"), None);
assert_eq!(parse_flat_bgcolor("# fffff"), None);
assert_eq!(parse_hex_color("#ffffff"), Some(color!(0xff, 0xff, 0xff)));
assert_eq!(parse_hex_color("#000000"), Some(color!(0x00, 0x00, 0x00)));
assert_eq!(parse_hex_color("#abcdef"), Some(color!(0xab, 0xcd, 0xef)));
assert_eq!(parse_hex_color("#123456"), Some(color!(0x12, 0x34, 0x56)));
assert_eq!(parse_hex_color("#789000"), Some(color!(0x78, 0x90, 0x00)));
assert_eq!(parse_hex_color("ffffff"), None);
assert_eq!(parse_hex_color("#fffffff"), None);
assert_eq!(parse_hex_color("#xfffff"), None);
assert_eq!(parse_hex_color("# fffff"), None);
}

macro_rules! test_hash {
Expand Down
7 changes: 7 additions & 0 deletions src/flamegraph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ pub mod defaults {
define! {
COLORS: &str = "hot",
SEARCH_COLOR: &str = "#e600e6",
UI_COLOR: &str = "#000000",
STROKE_COLOR: &str = "none",
TITLE: &str = "Flame Graph",
CHART_TITLE: &str = "Flame Chart",
Expand All @@ -102,6 +103,9 @@ pub struct Options<'a> {
/// If `None`, the background color will be selected based on the value of `colors`.
pub bgcolors: Option<color::BackgroundColor>,

/// The color of UI text such as the search and reset view button. Defaults to black
pub uicolor: color::Color,

/// Choose names based on the hashes of function names.
///
/// This will cause similar functions to be colored similarly.
Expand Down Expand Up @@ -306,6 +310,7 @@ impl<'a> Default for Options<'a> {
notes: Default::default(),
subtitle: Default::default(),
bgcolors: Default::default(),
uicolor: Default::default(),
hash: Default::default(),
deterministic: Default::default(),
palette_map: Default::default(),
Expand Down Expand Up @@ -511,10 +516,12 @@ where
StrokeColor::Color(c) => Some(c.to_string()),
StrokeColor::None => None,
};
let uicolor = opt.uicolor.to_string();
let style_options = StyleOptions {
imageheight,
bgcolor1,
bgcolor2,
uicolor,
strokecolor,
};

Expand Down
17 changes: 11 additions & 6 deletions src/flamegraph/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub(super) struct StyleOptions<'a> {
pub(super) imageheight: usize,
pub(super) bgcolor1: Cow<'a, str>,
pub(super) bgcolor2: Cow<'a, str>,
pub(super) uicolor: String,
pub(super) strokecolor: Option<String>,
}

Expand Down Expand Up @@ -121,7 +122,7 @@ where
let titlesize = &opt.font_size + 5;
svg.write_event(Event::Text(BytesText::from_escaped(&format!(
"
text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
text {{ font-family:{}; font-size:{}px }}
#title {{ text-anchor:middle; font-size:{}px; }}
",
font_type, &opt.font_size, titlesize,
Expand Down Expand Up @@ -181,7 +182,7 @@ text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
x: Dimension::Percent(50.0),
y: (opt.font_size * 2) as f64,
text: (&*opt.title).into(),
extra: vec![("id", "title")],
extra: vec![("id", "title"), ("fill", &style_options.uicolor)],
},
)?;

Expand Down Expand Up @@ -212,7 +213,7 @@ text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
opt.ypad1() - opt.font_size
} as f64,
text: " ".into(),
extra: iter::once(("id", "details")),
extra: vec![("id", "details"), ("fill", &style_options.uicolor)],
},
)?;

Expand All @@ -223,7 +224,11 @@ text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
x: Dimension::Pixels(super::XPAD),
y: (opt.font_size * 2) as f64,
text: "Reset Zoom".into(),
extra: vec![("id", "unzoom"), ("class", "hide")],
extra: vec![
("id", "unzoom"),
("class", "hide"),
("fill", &style_options.uicolor),
],
},
)?;

Expand All @@ -234,7 +239,7 @@ text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
x: Dimension::Pixels(image_width as usize - super::XPAD),
y: (opt.font_size * 2) as f64,
text: "Search".into(),
extra: vec![("id", "search")],
extra: vec![("id", "search"), ("fill", &style_options.uicolor)],
},
)?;

Expand All @@ -245,7 +250,7 @@ text {{ font-family:{}; font-size:{}px; fill:rgb(0,0,0); }}
x: Dimension::Pixels(image_width as usize - super::XPAD),
y: (style_options.imageheight - (opt.ypad2() / 2)) as f64,
text: " ".into(),
extra: iter::once(("id", "matched")),
extra: vec![("id", "matched"), ("fill", &style_options.uicolor)],
},
)?;

Expand Down
12 changes: 6 additions & 6 deletions tests/data/flamegraph/austin/flame.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions tests/data/flamegraph/colors/async-profiler-java.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions tests/data/flamegraph/colors/deterministic.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions tests/data/flamegraph/colors/java.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions tests/data/flamegraph/colors/js.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3ca8490

Please sign in to comment.