Skip to content

Commit

Permalink
feat: add option to only highlight the gutter for coverage info
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Jun 22, 2023
1 parent f4b3f0f commit 97b359c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
8 changes: 8 additions & 0 deletions assets/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,14 @@
@apply bg-red-400 hover:bg-red-500 dark:bg-red-800 dark:hover:bg-red-700;
}

.gutter.covered {
@apply bg-green-400 dark:bg-green-800 !important;
}

.gutter.uncovered {
@apply bg-red-400 dark:bg-red-800 !important;
}

.page-footer {
@apply text-sm font-bold pl-4 pt-4;
}
Expand Down
24 changes: 23 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Handling of command line arguments.

use std::{
fmt::{self, Display},
fs::OpenOptions,
io::{self, BufWriter, Write},
};

use camino::{Utf8Path, Utf8PathBuf};
use clap::{CommandFactory, Parser, Subcommand, ValueHint};
use clap::{CommandFactory, Parser, Subcommand, ValueEnum, ValueHint};
use clap_complete::Shell;
use color_eyre::eyre::{ensure, Result, WrapErr};

Expand All @@ -24,6 +25,9 @@ pub struct Cli {
/// The highlighting theme to use, if not disabled.
#[arg(long, default_value_t = Theme::OneHalf)]
pub theme: Theme,
/// Where to place the coverage color marker.
#[arg(long, default_value_t = CoverageStyle::Line)]
pub coverage_style: CoverageStyle,
/// Location of the project's Cargo.toml, in case the default detection isn't sufficient.
#[arg(long)]
pub manifest_path: Option<Utf8PathBuf>,
Expand All @@ -40,6 +44,24 @@ impl Cli {
}
}

/// The way in which to mark source code lines as covered or uncovered.
#[derive(Clone, Copy, Eq, PartialEq, ValueEnum)]
pub enum CoverageStyle {
/// Highlight the whole source line.
Line,
/// Only highlight the gutter (count column).
Gutter,
}

impl Display for CoverageStyle {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Line => "line",
Self::Gutter => "gutter",
})
}
}

#[derive(Subcommand)]
pub enum Command {
/// Generate auto-completions scripts for various shells.
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn main() -> Result<()> {
base_dir: &"../".repeat(file.relative_path.ancestors().skip(2).count()),
lines: &lines,
info: &file,
coverage_style: cli.coverage_style,
show_instantiations: cli.show_instantiations,
}
.render()?
Expand Down
7 changes: 4 additions & 3 deletions src/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use askama::Template;
use camino::Utf8PathBuf;
use time::OffsetDateTime;

use crate::schema;
use crate::{cli::CoverageStyle, schema};

/// Global constant instance with this project's info, so it doesn't have to be included as part of
/// each template struct.
Expand Down Expand Up @@ -62,6 +62,7 @@ pub struct Source<'a> {
pub base_dir: &'a str,
pub lines: &'a [String],
pub info: &'a FileInfo,
pub coverage_style: CoverageStyle,
pub show_instantiations: bool,
}

Expand Down Expand Up @@ -148,8 +149,7 @@ mod tests {
use camino::Utf8PathBuf;
use time::OffsetDateTime;

use super::FileInfo;
use crate::schema;
use super::{schema, CoverageStyle, FileInfo};

#[test]
fn render_index() {
Expand Down Expand Up @@ -187,6 +187,7 @@ mod tests {
called: HashMap::default(),
uncalled: HashMap::default(),
},
coverage_style: CoverageStyle::Line,
show_instantiations: true,
}
.render()
Expand Down
23 changes: 12 additions & 11 deletions templates/source.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,25 +45,26 @@
<td>
<a name="L{{loop.index}}" href="#L{{loop.index}}">{{loop.index}}</a>
</td>
<td>
{% match self.get_coverage(loop.index) %}
{% when Coverage::Covered(count) %}
{{count}}
{% when Coverage::Uncovered(count) %}
{{count}}
{% when Coverage::Unknown %}
{% endmatch %}
</td>
{% let coverage = self.get_coverage(loop.index) %}
{% let class %}
{% match self.get_coverage(loop.index) %}
{% match coverage %}
{% when Coverage::Covered(_) %}
{% let class = "covered" %}
{% when Coverage::Uncovered(_) %}
{% let class = "uncovered" %}
{% when Coverage::Unknown %}
{% let class = "" %}
{% endmatch %}
<td class="{{class}}">
<td{% if coverage_style == CoverageStyle::Gutter %} class="gutter {{class}}"{% endif %}>
{% match coverage %}
{% when Coverage::Covered(count) %}
{{count}}
{% when Coverage::Uncovered(count) %}
{{count}}
{% when Coverage::Unknown %}
{% endmatch %}
</td>
<td{% if coverage_style == CoverageStyle::Line %} class="{{class}}"{% endif %}>
<pre>{{line|safe}}</pre>
{% if show_instantiations %}
{% if let Some(functions) = self.info.uncalled.get(loop.index) %}
Expand Down

0 comments on commit 97b359c

Please sign in to comment.