Skip to content
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

Refactor modules into files #98

Merged
merged 5 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
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
36 changes: 19 additions & 17 deletions src/ascii_art.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,40 +254,42 @@ mod test {
assert_eq!(Tokens(" a;lksjf;a").leading_spaces(), 5);
assert_eq!(Tokens(" {1} {5} {9} a").leading_spaces(), 6);
}

#[test]
fn truncate() {
let colors_shim = Vec::new();
assert_eq!(Tokens("").render(&colors_shim, 0, 0), "\u{1b}[1;37m\u{1b}[0m");
assert_eq!(
Tokens("").truncate(0, 0).collect::<Vec<_>>(),
Tokens("").collect::<Vec<_>>()
);

assert_eq!(
Tokens(" ").render(&colors_shim, 0, 0),
"\u{1b}[1;37m\u{1b}[0m"
Tokens(" ").truncate(0, 0).collect::<Vec<_>>(),
Tokens("").collect::<Vec<_>>()
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 5),
"\u{1b}[1;37m \u{1b}[0m"
Tokens(" ").truncate(0, 5).collect::<Vec<_>>(),
Tokens(" ").collect::<Vec<_>>()
);
assert_eq!(
Tokens(" ").render(&colors_shim, 1, 5),
"\u{1b}[1;37m \u{1b}[0m"
Tokens(" ").truncate(1, 5).collect::<Vec<_>>(),
Tokens(" ").collect::<Vec<_>>()
);
assert_eq!(
Tokens(" ").render(&colors_shim, 3, 5),
"\u{1b}[1;37m \u{1b}[0m"
Tokens(" ").truncate(3, 5).collect::<Vec<_>>(),
Tokens(" ").collect::<Vec<_>>()
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 4),
"\u{1b}[1;37m \u{1b}[0m"
Tokens(" ").truncate(0, 4).collect::<Vec<_>>(),
Tokens(" ").collect::<Vec<_>>()
);
assert_eq!(
Tokens(" ").render(&colors_shim, 0, 3),
"\u{1b}[1;37m \u{1b}[0m"
Tokens(" ").truncate(0, 3).collect::<Vec<_>>(),
Tokens(" ").collect::<Vec<_>>()
);

assert_eq!(
Tokens(" {1} {5} {9} a").render(&colors_shim, 4, 10),
"\u{1b}[1;37m\u{1b}[0m\u{1b}[1;37m\u{1b}[0m\u{1b}[1;37m \u{1b}[0m\u{1b}[1;37m a\u{1b}[0m "
Tokens(" {1} {5} {9} a").truncate(4, 10).collect::<Vec<_>>(),
Tokens("{1}{5} {9} a").collect::<Vec<_>>()
);
}
}
29 changes: 29 additions & 0 deletions src/commit_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use git2::Oid;

pub struct CommitInfo {
commit: Oid,
refs: Vec<String>,
}

impl CommitInfo {
pub fn new(commit: Oid, refs: Vec<String>) -> CommitInfo {
CommitInfo { commit, refs }
}
}

impl std::fmt::Display for CommitInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let short_commit = self.commit.to_string().chars().take(7).collect::<String>();
if self.refs.len() > 0 {
let refs_str = self
.refs
.iter()
.map(|ref_name| ref_name.as_str())
.collect::<Vec<&str>>()
.join(", ");
write!(f, "{} ({})", short_commit, refs_str)
} else {
write!(f, "{}", short_commit)
}
}
}
29 changes: 29 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// Custom error type
pub enum Error {
/// Sourcecode could be located
SourceCodeNotFound,
/// Git is not installed or did not function properly
GitNotInstalled,
/// Did not find any git data in the directory
NoGitData,
/// An IO error occoured while reading ./
ReadDirectory,
/// Not in a Git Repo
NotGitRepo,
/// Error while getting branch info
ReferenceInfoError,
}

impl std::fmt::Debug for Error {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let content = match self {
Error::SourceCodeNotFound => "Could not find any source code in this directory",
Error::GitNotInstalled => "Git failed to execute",
Error::NoGitData => "Could not retrieve git configuration data",
Error::ReadDirectory => "Could not read directory",
Error::NotGitRepo => "This is not a Git Repo",
Error::ReferenceInfoError => "Error while retrieving reference information",
};
write!(f, "{}", content)
}
}
Loading