Skip to content

Commit

Permalink
Merge pull request #8 from frederikstroem/release-v0.3.0
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
frederikstroem committed Jan 16, 2024
2 parents e374a59 + f5a15b4 commit 19ea308
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
16 changes: 14 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

- Add merge CI/CD to verify Git tags, Cargo.toml version, CHANGELOG.md version and PKGBUILD version
- Add printing of pathing relative to current Git directory (if applicable)
- Add printing of pathing including current dir.
- Add printing of pathing relative to home directory (if applicable)
- Add printing of pathing relative to root directory
- Include only files matching a regex
Expand All @@ -29,6 +27,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
```
```
- Add video/gif demo to README.md
- Improve code quality and structure
## [v0.3.0] - 2024-01-16
### Added
- Git ancestry feature to display the file's ancestry within a Git repository.
- Added Dependency on `git2` crate to support Git.
- Added "Improve code quality and structure" to the roadmap in `CHANGELOG.md`.
### Changed
- Updated `clap` dependency.
### Removed
- Removed TODO items from the Unreleased section that have been implemented in this release.
## [v0.2.0] - 2024-01-01
Expand Down
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "psource"
version = "0.2.0"
version = "0.3.0"
authors = ["Frederik Holm Strøm <crates.io@frederikstroem.com>"]
description = "CLI tool to pretty print source code to stdout or directly to the clipboard."
edition = "2021"
Expand All @@ -13,9 +13,10 @@ exclude = [".github", ".vscode"]

[dependencies]
anyhow = "1.0"
clap = { version = "4.4.11", features = ["derive"] }
clap = { version = "4.4", features = ["derive"] }
dirs = "5.0.1"
config = { version = "0.13.4", features = ["toml"] }
walkdir = "2.4.0"
arboard = "3.3.0"
file-format = "0.23.0"
git2 = "0.18"
6 changes: 5 additions & 1 deletion src/cli/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ pub struct Cli {
pub copy: bool,

/// Display the file's ancestry in the output path by including the specified number of parent directories relative to the current working directory, or 0 to omit the ancestry
#[arg(short, long, default_value = "1")]
#[arg(short, long, default_value = "1", conflicts_with = "git_ancestry")]
pub ancestry: usize,

/// Display the file's ancestry in the output path, including parent directories from the current working directory within a Git repository to its root, unlike the fixed number specified by the 'ancestry' option
#[arg(short, long = "git-ancestry", conflicts_with = "ancestry")]
pub git_ancestry: bool,

// /// Exclude files matching the given regex
// #[arg(short, long)]
// pub exclude: Option<String>,
Expand Down
38 changes: 35 additions & 3 deletions src/printer/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::fs;
use std::path::{PathBuf, Path};
use std::ffi::OsStr;
use std::time::Duration;
use anyhow::Context;
use arboard::Clipboard;
use git2::Repository;

use crate::{config::{Config, OutputTarget}, cli::Cli};
use crate::printer::file_walker::FileWalker;
Expand All @@ -25,7 +27,32 @@ impl<'a> Printer<'a> {
)
}

fn get_ancestry_path(&self, path: &Path, ancestry: usize) -> String {
fn get_ancestry_count_to_git_root(&self) -> anyhow::Result<usize> {
// Get current working directory.
let cwd: PathBuf = env::current_dir().context("Failed to get current working directory")?;

// Discover the .git directory, starting from `cwd`.
let repo = Repository::discover(&cwd)
.with_context(|| format!("Failed to discover Git repository for current working directory: {:?}", cwd))?;

// Get the workdir of the repo, which is the root directory of the repository.
let repo_workdir = repo.workdir()
.ok_or_else(|| anyhow::anyhow!("Repository does not have a working directory."))?;

// Get the relative path of `cwd` from the root of the repository.
let relative_path = cwd.strip_prefix(repo_workdir)
.with_context(|| format!("Failed to get relative path for current working directory: {:?}", cwd))?;

// Count the number of directory components in the relative path.
let mut ancestry_count = relative_path.iter().filter(|&component| component != OsStr::new(".")).count();

// Increment the count by one to include the Git repository directory itself.
ancestry_count += 1;

Ok(ancestry_count)
}

fn get_ancestry_path(&self, path: &Path, ancestry: usize) -> anyhow::Result<String> {
// Get current working directory. This is the directory from which the program was executed.
let cwd: PathBuf = env::current_dir().expect("Failed to get current working directory");

Expand Down Expand Up @@ -91,7 +118,7 @@ impl<'a> Printer<'a> {
ancestry_path.push_str(&format!("{}", path.to_string_lossy()));

// Return the ancestry path
ancestry_path
Ok(ancestry_path)
}

fn get_output(&self) -> anyhow::Result<String> {
Expand All @@ -100,7 +127,12 @@ impl<'a> Printer<'a> {
let mut output = String::new();
for path in paths {
let content = fs::read_to_string(&path)?;
let ancestry_path = self.get_ancestry_path(&path, self.cli.ancestry);
let ancestry = if self.cli.git_ancestry {
self.get_ancestry_count_to_git_root()?
} else {
self.cli.ancestry
};
let ancestry_path = self.get_ancestry_path(&path, ancestry)?;
output.push_str(&format!("⚫ {}\n{}\n", ancestry_path, content));
}

Expand Down

0 comments on commit 19ea308

Please sign in to comment.