Skip to content

Commit

Permalink
Merge pull request #166 from stepchowfun/v1.7.0
Browse files Browse the repository at this point in the history
Revert the two-column layout behavior for the `list-tags`, `list-refs`, and `list-unused` subcommands
  • Loading branch information
stepchowfun committed Apr 4, 2023
2 parents f987548 + 0384534 commit 867650a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 168 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.7.0] - 2023-04-03

### Changed
- The `list-tags`, `list-refs`, and `list-unused` subcommands now have their original behavior, the same as in v1.5.0. The fancy two-column output was too buggy.

## [1.6.1] - 2023-04-03

### Fixed
Expand Down
86 changes: 1 addition & 85 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tagref"
version = "1.6.1"
version = "1.7.0"
authors = ["Stephan Boyer <[email protected]>"]
edition = "2021"
description = "Tagref helps you maintain cross-references in your code."
Expand All @@ -12,11 +12,9 @@ readme = "README.md"

[dependencies]
atty = "0.2"
colonnade = "1"
colored = "1"
ignore = "0.4"
regex = "1"
term_size = "0.3"

[dependencies.clap]
version = "2"
Expand Down
18 changes: 4 additions & 14 deletions src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,6 @@ pub enum Type {
Ref,
}

impl fmt::Display for Type {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}",
match self {
Type::Tag => "tag",
Type::Ref => "ref",
},
)
}
}

#[derive(Clone, Debug)]
pub struct Label {
pub label_type: Type,
Expand All @@ -40,7 +27,10 @@ impl fmt::Display for Label {
write!(
f,
"[{}:{}] @ {}:{}",
self.label_type,
match self.label_type {
Type::Tag => "tag",
Type::Ref => "ref",
},
self.label,
self.path.to_string_lossy(),
self.line_number,
Expand Down
77 changes: 11 additions & 66 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ mod walk;
use {
atty::Stream,
clap::{App, AppSettings, Arg, ArgMatches, SubCommand},
colonnade::Colonnade,
colored::Colorize,
regex::{escape, Regex},
std::{
cmp::max,
collections::HashMap,
io::BufReader,
path::{Path, PathBuf},
Expand Down Expand Up @@ -109,25 +107,6 @@ fn settings<'a>() -> (ArgMatches<'a>, Vec<PathBuf>, String, String) {
(matches, paths, tag_prefix, ref_prefix)
}

// Print data in two tabulated columns.
fn print_tabulated(data: Vec<[String; 2]>) {
// Set the viewport width to the terminal width if STDOUT is a TTY, or 80 otherwise. We ensure
// the width is at least 3 to ensure there is enough room for 2 columns with a 1-space margin
// between them [tag:min_terminal_width].
let terminal_width = max(
term_size::dimensions_stdout().map_or(80, |(width, _height)| width),
3,
);

// Create a 2-column table. The `unwrap` is safe due to [ref:min_terminal_width].
let mut colonnade = Colonnade::new(2, terminal_width).unwrap();

// The `unwrap` is safe by manual inspection of the types of errors that can be thrown.
for line in colonnade.tabulate(data).unwrap() {
println!("{line}");
}
}

// Program entrypoint
#[allow(clippy::too_many_lines)]
fn entry() -> Result<(), String> {
Expand All @@ -153,60 +132,36 @@ fn entry() -> Result<(), String> {
match matches.subcommand_name() {
Some(LIST_TAGS_SUBCOMMAND) => {
// Parse and print all the tags.
let tags = Arc::new(Mutex::new(vec![]));
let tags_clone = tags.clone();
let mutex = Arc::new(Mutex::new(()));
let _ = walk::walk(&paths, move |file_path, file| {
let tags = tags_clone.clone();
let mut results = vec![];
for tag in label::parse(
&tag_regex,
&ref_regex,
label::Type::Tag,
file_path,
BufReader::new(file),
) {
results.push([
tag.label,
format!("{}:{}", tag.path.display(), tag.line_number),
]);
let _lock = mutex.lock().unwrap(); // Safe assuming no poisoning
println!("{tag}");
}
tags.lock().unwrap().extend(results.drain(..)); // Safe assuming no poisoning
});

// Safe assuming no poisoning
let mut tags: Vec<[String; 2]> = std::mem::take(&mut tags.lock().unwrap());

tags.sort();
print_tabulated(tags);
}

Some(LIST_REFS_SUBCOMMAND) => {
// Parse and print all the references.
let references = Arc::new(Mutex::new(vec![]));
let references_clone = references.clone();
let mutex = Arc::new(Mutex::new(()));
let _ = walk::walk(&paths, move |file_path, file| {
let references = references_clone.clone();
let mut results = vec![];
for r#ref in label::parse(
&tag_regex,
&ref_regex,
label::Type::Ref,
file_path,
BufReader::new(file),
) {
results.push([
r#ref.label,
format!("{}:{}", r#ref.path.display(), r#ref.line_number),
]);
let _lock = mutex.lock().unwrap(); // Safe assuming no poisoning
println!("{ref}");
}
references.lock().unwrap().extend(results.drain(..)); // Safe assuming no poisoning
});

// Safe assuming no poisoning
let mut references: Vec<[String; 2]> = std::mem::take(&mut references.lock().unwrap());

references.sort();
print_tabulated(references);
}

Some(LIST_UNUSED_SUBCOMMAND) => {
Expand Down Expand Up @@ -252,21 +207,11 @@ fn entry() -> Result<(), String> {
});

// Print the remaining tags. The `unwrap` is safe assuming no poisoning.
let mut references: Vec<[String; 2]> = tags_map
.lock()
.unwrap()
.values()
.flat_map(|tags| {
tags.iter().map(|tag| {
[
tag.label.clone(),
format!("{}:{}", tag.path.display(), tag.line_number),
]
})
})
.collect();
references.sort();
print_tabulated(references);
for tags in tags_map.lock().unwrap().values() {
for tag in tags {
println!("{tag}");
}
}
}

Some(CHECK_SUBCOMMAND) | None => {
Expand Down

0 comments on commit 867650a

Please sign in to comment.