Skip to content

Commit

Permalink
feat: Add arguments parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Myzel394 committed Jun 28, 2024
1 parent 1ca8d5c commit c3cc138
Show file tree
Hide file tree
Showing 5 changed files with 268 additions and 74 deletions.
120 changes: 120 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.7", features = ["derive"] }
colored = "2.1.0"
prettytable-rs = "0.10.0"
46 changes: 46 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
pub mod cli {
use std::fmt;

use clap::{arg, command, Parser};
use clap::builder::TypedValueParser as _;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Copy, Clone)]
pub enum Encoding {
UTF8,
}

impl Encoding {
pub fn from_str(s: &str) -> Self {
match s {
"utf8" => Encoding::UTF8,
_ => Encoding::UTF8,
}
}
}

impl fmt::Display for Encoding {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Parser, Debug)]
#[command(name = "charsplit")]
#[command(bin_name = "charsplit")]
#[command(about = "Split a string into its bytes and characters")]
#[command(long_about = "charsplit is a small utility tool that will give you information about your string input. It will split the string into its bytes and graphemes, and give you information about them.")]
#[command(version = "0.1.0")]
pub struct Arguments {
/// What encoding to use
#[arg(
short,
long,
default_value_t = Encoding::UTF8,
value_parser = clap::builder::PossibleValuesParser::new(["UTF8"])
.map(|value| Encoding::from_str(&value))
)]
pub encoding: Encoding,

#[arg(default_value_t = String::new())]
pub text: String,
}
}
102 changes: 28 additions & 74 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,40 @@
use core::fmt;
use std::{char, io::{self, Read}, iter::Iterator, slice::Iter};
use colored::Colorize;
use std::{io::{self, IsTerminal, Read}, process::{exit, ExitCode}};
use clap::Parser;
use cli::cli::Arguments;
use config::config::Config;
use charset::charset::{Charset, Utf8Charset};
use prettytable::{format, row, Table};
use utils::utils::format_grapheme;

mod config;
mod charset;
mod utf8_groups;
mod utils;
mod cli;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
enum ByteType {
Ascii,
Utf8Base,
Utf8Continuation,
AsciiNewLine,
Unknown
}

impl fmt::Display for ByteType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}

fn get_byte_type(byte: &u8) -> ByteType {
if byte == &0x0A {
return ByteType::AsciiNewLine;
}

if byte >> 7 == 0 {
return ByteType::Ascii;
}

if byte >> 5 == 0b110 || byte >> 4 == 0b1110 || byte >> 3 == 0b11110 {
return ByteType::Utf8Base;
}

if byte >> 6 == 0b10 {
return ByteType::Utf8Continuation;
}

ByteType::Unknown
}

fn format_grapheme(table: &mut Table, grapheme: &char, use_truecolors: &bool) {
let grapheme_string = grapheme.to_string();
let bytes = grapheme_string.as_bytes();

let first_byte = bytes.get(0).unwrap();

table.add_row(row![
grapheme,
format!("{:0b}", first_byte),
format!("{}", (*first_byte) as u32),
(*grapheme) as u32,
format!("0x{:X}", (*grapheme) as u32),
get_byte_type(&first_byte),
Utf8Charset::get_description(&((*grapheme) as u32)),
]);
fn main() -> ExitCode {
let config = Config::build();
let args = Arguments::parse();

let input = match args.text.as_str() {
"" => {
if io::stdin().is_terminal() {
String::new()
} else {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
input
}
}
_ => args.text
};

dbg!(&input);

for byte in &bytes[1..] {
table.add_row(row![
" ",
format!("{:08b}", byte),
format!("{}", (*byte) as u32),
" ",
" ",
get_byte_type(&byte),
" ",
]);
if input.len() == 0 {
eprintln!("Please provide some input either via argument or stdin");
return ExitCode::FAILURE;
}

}

fn main() {
let config = Config::build();

let input = {
let mut input = String::new();
io::stdin().read_to_string(&mut input).unwrap();
input
};
let graphemes = input.chars();

let mut table = Table::new();
Expand All @@ -106,4 +58,6 @@ fn main() {
]);

table.printstd();

ExitCode::SUCCESS
}
Loading

0 comments on commit c3cc138

Please sign in to comment.