diff options
author | Julian Sitkevich <sitkevij@gmail.com> | 2019-01-01 22:23:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-01 22:23:10 -0800 |
commit | 93b11a84bd1dffb3a5b17499cccb236904d5c764 (patch) | |
tree | ed4d52abd9a6c8ec6b329e3c38f1b84b39259fcd | |
parent | 05149736512ec00fb97d5a261d045bdfa71e35cd (diff) | |
parent | 72cd49201e6ad30d3891552633ef2bf814a39a33 (diff) |
Merge pull request #7 from sitkevij/feature/cli-makev0.1.3
#6 0.1.3 enhancements, improve cli user feedback, Makefile, cargo fmt
-rw-r--r-- | .travis.yml | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | Dockerfile | 2 | ||||
-rw-r--r-- | Makefile | 41 | ||||
-rw-r--r-- | README.md | 16 | ||||
-rw-r--r-- | src/lib.rs | 81 | ||||
-rw-r--r-- | src/main.rs | 141 |
7 files changed, 183 insertions, 102 deletions
diff --git a/.travis.yml b/.travis.yml index 545c307..8aeebcd 100644 --- a/.travis.yml +++ b/.travis.yml @@ -14,4 +14,4 @@ matrix: script: - cargo build --verbose --all - cargo test --verbose --all -- --nocapture - - docker build -t sitkevij/stretch-slim:hex-0.1.2 . + - docker build -t sitkevij/stretch-slim:hex-0.1.3 . @@ -1,6 +1,6 @@ [package] name = "hex" -version = "0.1.2" +version = "0.1.3" authors = ["author https://github.com/sitkevij"] description = "hexdump utility." homepage = "https://github.com/sitkevij/hex" @@ -6,7 +6,7 @@ LABEL org.label-schema.name="hex" \ org.label-schema.usage="https://github.com/sitkevij/hex/blob/master/README.md" \ org.label-schema.vcs-url="https://github.com/sitkevij/hex" \ org.label-schema.vendor="sitkevij" \ - org.label-schema.version="0.1.0" \ + org.label-schema.version="0.1.3" \ maintainer="https://github.com/sitkevij" RUN apt-get update && \ apt-get install --no-install-recommends -y \ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..bc881cf --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +# http://www.gnu.org/software/make/manual/make.html#Special-Variables +.DEFAULT_GOAL := release + +# http://www.gnu.org/software/make/manual/make.html#Phony-Targets +.PHONY: clean docker + +TARGET_DIR = target +DEBUG_DIR = $(TARGET_DIR)/debug +RELEASE_DIR = $(TARGET_DIR)/release +RLS_DIR = $(TARGET_DIR)/rls +INSTALL_DIR = /usr/local/bin +BINARY = hex + +all: fmt test clean + +fmt: + cargo +nightly fmt --verbose + +debug: test + cargo build + +release: test + cargo build --release + +test: + cargo test --verbose --all -- --nocapture + +install: release debug test + cargo install --path . + ## cp $(RELEASE_DIR)/$(BINARY) $(INSTALL_DIR)/$(BINARY) + +install-force: clean release debug test + cargo install --path . --force + +docker: + docker build -t sitkevij/stretch-slim:hex-0.1.3 . + +clean: ## Remove all artifacts + rm -rf $(DEBUG_DIR) + rm -rf $(RELEASE_DIR) + rm -rf $(RLS_DIR)
\ No newline at end of file @@ -34,6 +34,22 @@ $ hex -c12 tests/files/alphanumeric.txt ![octal hex output format](https://raw.githubusercontent.com/sitkevij/hex/master/tests/files/hex_screenshot_macos_format_o.png) +# install + +From within the `hex` source code directory, simply execute: +``` +make install +``` +This will run the followng `cargo` commands: +``` +cargo build --release +cargo test --verbose --all -- --nocapture +cargo install --path . +``` +Which will compile the release version, run tests and install release binary to `<USERDIR>/.cargo/bin/hex`. + +If `<USERDIR>/.cargo/bin` is part of the `PATH` evironment variable, `hex` should be able to be executed anywhere in the shell. + # feature: output arrays in rust, c or golang `hex` has a feature which can output the input file bytes as source code arrays. @@ -1,21 +1,26 @@ -#![deny(missing_docs, - missing_debug_implementations, missing_copy_implementations, - trivial_casts, trivial_numeric_casts, - unsafe_code, - unstable_features, - unused_import_braces, unused_qualifications)] +#![deny( + missing_docs, + missing_debug_implementations, + missing_copy_implementations, + trivial_casts, + trivial_numeric_casts, + unsafe_code, + unstable_features, + unused_import_braces, + unused_qualifications +)] // #![allow(dead_code)] //! general hex lib -extern crate clap; extern crate ansi_term; +extern crate clap; use clap::ArgMatches; +use std::f64; use std::fs; use std::fs::File; -use std::io::Read; use std::io::BufReader; -use std::f64; +use std::io::Read; /// nothing ⇒ Display /// ? ⇒ Debug @@ -139,38 +144,30 @@ pub fn print_byte(b: u8, format: Format, colorize: bool) { if colorize { // note, for color testing: for (( i = 0; i < 256; i++ )); do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; done match format { - Format::Octal => { - print!( - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_octal(b)) - ) - } - Format::LowerHex => { - print!( - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_lower_hex(b)) - ) - } - Format::UpperHex => { - print!( - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_upper_hex(b)) - ) - } - Format::Binary => { - print!( - "{} ", - ansi_term::Style::new() - .fg(ansi_term::Color::Fixed(color)) - .paint(hex_binary(b)) - ) - } + Format::Octal => print!( + "{} ", + ansi_term::Style::new() + .fg(ansi_term::Color::Fixed(color)) + .paint(hex_octal(b)) + ), + Format::LowerHex => print!( + "{} ", + ansi_term::Style::new() + .fg(ansi_term::Color::Fixed(color)) + .paint(hex_lower_hex(b)) + ), + Format::UpperHex => print!( + "{} ", + ansi_term::Style::new() + .fg(ansi_term::Color::Fixed(color)) + .paint(hex_upper_hex(b)) + ), + Format::Binary => print!( + "{} ", + ansi_term::Style::new() + .fg(ansi_term::Color::Fixed(color)) + .paint(hex_binary(b)) + ), _ => print!("{}", "unk_fmt "), } } else { @@ -193,7 +190,7 @@ pub fn func_out(len: u64, places: usize) { for y in 0..len { let y_float: f64 = y as f64; let len_float: f64 = len as f64; - let x: f64 = ((((y_float / len_float)) * f64::consts::PI) / 2.0).sin(); + let x: f64 = (((y_float / len_float) * f64::consts::PI) / 2.0).sin(); let formatted_number = format!("{:.*}", places, x); print!("{}", formatted_number); print!(","); diff --git a/src/main.rs b/src/main.rs index 40fe645..7e379c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,68 +1,95 @@ extern crate clap; mod lib; use clap::{App, Arg}; +use std::env; use std::process; /// Central application entry point. fn main() { - let matches = App::new(env!("CARGO_PKG_NAME")) - .version(env!("CARGO_PKG_VERSION")) - .about(env!("CARGO_PKG_DESCRIPTION")) // CARGO_PKG_HOMEPAGE - .author(env!("CARGO_PKG_AUTHORS")) - .arg(Arg::with_name("cols") - .short("c") - .long("cols") - .value_name("columns") - .help("Set column length") - .takes_value(true)) - .arg(Arg::with_name("len") - .short("l") - .long("len") - .value_name("len") - .help("Set <len> bytes to read") - .takes_value(true)) - .arg(Arg::with_name("format") - .short("f") - .long("format") - .help("Set format of octet: Octal (o), LowerHex (x), UpperHex (X), Binary (b)") - .possible_values(&["o", "x", "X", "b"]) - .takes_value(true)) - .arg(Arg::with_name("INPUTFILE") - .help("Pass file path as an argument for hex dump") - .required(true) - .index(1)) - .arg(Arg::with_name("v") - .short("v") - .multiple(true) - .help("Sets verbosity level")) - .arg(Arg::with_name("color") - .short("t") - .long("color") - .help("Set color tint terminal output. 0 to disable, 1 to enable") - .default_value("1") - .possible_values(&["0", "1"]) - .takes_value(true)) - .arg(Arg::with_name("array") - .short("a") - .long("array") - .value_name("array_format") - .help("Set source code format output: rust (r), C (c), golang (g)") - .possible_values(&["r", "c", "g"]) - .takes_value(true)) - .arg(Arg::with_name("func") - .short("u") - .long("func") - .value_name("func_length") - .help("Set function wave length") - .takes_value(true)) - .arg(Arg::with_name("places") - .short("p") - .long("places") - .value_name("func_places") - .help("Set function wave output decimal places") - .takes_value(true)) - .get_matches(); + let app = App::new(env!("CARGO_PKG_NAME")) + .version(env!("CARGO_PKG_VERSION")) + .about(env!("CARGO_PKG_DESCRIPTION")) // CARGO_PKG_HOMEPAGE + .author(env!("CARGO_PKG_AUTHORS")) + .arg( + Arg::with_name("cols") + .short("c") + .long("cols") + .value_name("columns") + .help("Set column length") + .takes_value(true), + ) + .arg( + Arg::with_name("len") + .short("l") + .long("len") + .value_name("len") + .help("Set <len> bytes to read") + .takes_value(true), + ) + .arg( + Arg::with_name("format") + .short("f") + .long("format") + .help("Set format of octet: Octal (o), LowerHex (x), UpperHex (X), Binary (b)") + .possible_values(&["o", "x", "X", "b"]) + .takes_value(true), + ) + .arg( + Arg::with_name("INPUTFILE") + .help("Pass file path as an argument for hex dump") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("v") + .short("v") + .multiple(true) + .help("Sets verbosity level"), + ) + .arg( + Arg::with_name("color") + .short("t") + .long("color") + .help("Set color tint terminal output. 0 to disable, 1 to enable") + .default_value("1") + .possible_values(&["0", "1"]) + .takes_value(true), + ) + .arg( + Arg::with_name("array") + .short("a") + .long("array") + .value_name("array_format") + .help("Set source code format output: rust (r), C (c), golang (g)") + .possible_values(&["r", "c", "g"]) + .takes_value(true), + ) + .arg( + Arg::with_name("func") + .short("u") + .long("func") + .value_name("func_length") + .help("Set function wave length") + .takes_value(true), + ) + .arg( + Arg::with_name("places") + .short("p") + .long("places") + .value_name("func_places") + .help("Set function wave output decimal places") + .takes_value(true), + ); + let args: Vec<_> = env::args().collect(); + if args.len() == 1 { + app.clone().print_help().unwrap(); + println!(""); + println!(""); + process::exit(0); + } + + let matches = app.get_matches(); match lib::run(matches) { Ok(_) => { process::exit(0); |