]>
code.octet-stream.net Git - hashgood/blob - src/display.rs
a4c011c760e6cea3a879dfce92138c03eb3bad82
1 use super::{Algorithm
, CandidateHash
, Hash
, MatchLevel
, MessageLevel
, VerificationSource
};
2 use std
::borrow
::Borrow
;
5 use termcolor
::{Color
, ColorChoice
, ColorSpec
, StandardStream
, WriteColor
};
7 pub type PrintResult
= Result
<(), Box
<dyn Error
>>;
9 fn filename_display(filename
: &str) -> &str {
11 return "standard input";
16 fn get_stdout(no_colour
: bool
) -> StandardStream
{
18 StandardStream
::stdout(ColorChoice
::Never
)
20 StandardStream
::stdout(ColorChoice
::Always
)
24 fn write_filename(mut stdout
: &mut StandardStream
, filename
: &str) -> PrintResult
{
25 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
26 write
!(&mut stdout
, "{}", filename_display(filename
))?
;
31 fn write_algorithm(mut stdout
: &mut StandardStream
, alg
: Algorithm
) -> PrintResult
{
34 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Magenta
)))?
;
35 write
!(&mut stdout
, "MD5")?
;
38 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
;
39 write
!(&mut stdout
, "SHA-1")?
;
41 Algorithm
::Sha256
=> {
42 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
;
43 write
!(&mut stdout
, "SHA-256")?
;
50 fn print_hex_compare(print
: &str, against
: &str, mut stdout
: &mut StandardStream
) -> PrintResult
{
51 for (p
, a
) in print
.chars().zip(against
.chars()) {
53 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
;
55 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
57 write
!(&mut stdout
, "{}", p
)?
;
60 writeln
!(&mut stdout
)?
;
65 mut stdout
: &mut StandardStream
,
66 verify_source
: &VerificationSource
,
67 candidate_filename
: &Option
<String
>,
69 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
70 match &verify_source
{
71 VerificationSource
::CommandArgument
=> {
72 writeln
!(&mut stdout
, "command line argument")?
;
74 VerificationSource
::Clipboard
=> {
75 writeln
!(&mut stdout
, "pasted from clipboard")?
;
77 VerificationSource
::RawFile(raw_path
) => match raw_path
.to_string_lossy().borrow() {
79 writeln
!(&mut stdout
, "from standard input")?
;
82 writeln
!(&mut stdout
, "from file '{}' containing raw hash", path
)?
;
85 VerificationSource
::DigestsFile(digest_path
) => {
86 match digest_path
.to_string_lossy().borrow() {
90 "'{}' from digests on standard input",
91 candidate_filename
.as_ref().unwrap
()
97 "'{}' in digests file '{}'",
98 candidate_filename
.as_ref().unwrap
(),
111 verify_hash
: Option
<&CandidateHash
>,
112 verify_source
: Option
<&VerificationSource
>,
115 let mut stdout
= get_stdout(no_colour
);
117 write_filename(&mut stdout
, &hash
.filename
)?
;
118 write
!(&mut stdout
, " / ")?
;
119 write_algorithm(&mut stdout
, hash
.alg
)?
;
120 writeln
!(&mut stdout
)?
;
122 // Handle basic case first - nothing to compare it to
123 let hash_hex
= hex
::encode(&hash
.bytes
);
124 let verify_hash
= match verify_hash
{
126 write
!(&mut stdout
, "{}\n\n", hash_hex
)?
;
129 Some(verify_hash
) => verify_hash
,
131 let other_hex
= hex
::encode(&verify_hash
.bytes
);
133 // Do a top-to-bottom comparison
134 print_hex_compare(&hash_hex
, &other_hex
, &mut stdout
)?
;
135 print_hex_compare(&other_hex
, &hash_hex
, &mut stdout
)?
;
137 // Show the source of our hash
138 if let Some(source
) = verify_source
{
139 write_source(&mut stdout
, source
, &verify_hash
.filename
)?
;
142 writeln
!(&mut stdout
)?
;
146 pub fn print_messages(messages
: Vec
<(MessageLevel
, String
)>, no_colour
: bool
) -> PrintResult
{
147 let mut stdout
= get_stdout(no_colour
);
149 for (level
, msg
) in &messages
{
151 MessageLevel
::Error
=> {
152 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
153 write
!(&mut stdout
, "(error) ")?
;
155 MessageLevel
::Warning
=> {
156 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
157 write
!(&mut stdout
, "(warning) ")?
;
159 MessageLevel
::Note
=> {
160 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Cyan
)))?
;
161 write
!(&mut stdout
, "(note) ")?
;
165 writeln
!(&mut stdout
, "{}", msg
)?
;
167 if !messages
.is
_empty
() {
168 writeln
!(&mut stdout
)?
174 pub fn print_match_level(match_level
: MatchLevel
, no_colour
: bool
) -> PrintResult
{
175 let mut stdout
= get_stdout(no_colour
);
176 write
!(&mut stdout
, "Result: ")?
;
179 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Green
)))?
;
180 writeln
!(&mut stdout
, "OK")?
;
182 MatchLevel
::Maybe
=> {
183 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Yellow
)))?
;
184 writeln
!(&mut stdout
, "MAYBE")?
;
186 MatchLevel
::Fail
=> {
187 stdout
.set_color(ColorSpec
::new().set_fg(Some(Color
::Red
)))?
;
188 writeln
!(&mut stdout
, "FAIL")?
;