Skip to content

Added commit hash to svg #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/print/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use crate::graph::GitGraph;
use crate::settings::Settings;
use svg::node::element::path::Data;
use svg::node::element::{Circle, Line, Path};
use svg::node::element::{Circle, Line, Path, Text};
use svg::node::Text as TextNode;
use svg::Document;

/// Creates a SVG visual representation of a graph.
Expand Down Expand Up @@ -33,7 +34,7 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
let branch_color = &branch.visual.svg_color;

if branch.visual.column.unwrap() > max_column {
max_column = branch.visual.column.unwrap();
max_column = branch.visual.column.unwrap() + 5; // +5 for commit hash
}

for p in 0..2 {
Expand Down Expand Up @@ -77,6 +78,7 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
branch_color,
!info.is_merge,
));
document = document.add(commit_hash(idx, &info.oid.to_string(), true));
}
}
let (x_max, y_max) = commit_coord(max_idx + 1, max_column + 1);
Expand All @@ -90,6 +92,18 @@ pub fn print_svg(graph: &GitGraph, settings: &Settings) -> Result<String, String
Ok(String::from_utf8(out).unwrap_or_else(|_| "Invalid UTF8 character.".to_string()))
}

fn commit_hash(index: usize, hash: &str, shorten: bool) -> Text {
let (x, y) = commit_coord(index, 2);
Text::new()
.set("x", x)
.set("y", y)
.set("stroke", "goldenrod")
.set("font-size", 12)
.set("font-family", "open sans")
.set("letter-spacing", 0.75)
.add(TextNode::new(if shorten { &hash[..7] } else { hash }))
}

fn commit_dot(index: usize, column: usize, color: &str, filled: bool) -> Circle {
let (x, y) = commit_coord(index, column);
Circle::new()
Expand Down
Loading