Skip to content

Commit

Permalink
Merge pull request #1 from cqb13/text-to-8xp
Browse files Browse the repository at this point in the history
Text to 8xp
  • Loading branch information
cqb13 authored Jun 29, 2024
2 parents aa08f6b + 9639f29 commit 24a79b2
Show file tree
Hide file tree
Showing 22 changed files with 14,267 additions and 514 deletions.
14 changes: 7 additions & 7 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,28 @@ jobs:
run: |
choco install zip
cd target/${{ matrix.target }}/release
zip ti-tools-0.1.1-${{ matrix.target }}.zip ti-tools.exe
zip ti-tools-0.1.2-${{ matrix.target }}.zip ti-tools.exe
cd ../../..
- name: Create tar.gz file on macOS
if: ${{ matrix.os == 'macos-latest' }}
run: |
chmod +x target/${{ matrix.target }}/release/ti-tools
tar -zcf target/${{ matrix.target }}/release/ti-tools-0.1.1-${{ matrix.target }}.tar.gz -C target/${{ matrix.target }}/release ti-tools
tar -zcf target/${{ matrix.target }}/release/ti-tools-0.1.2-${{ matrix.target }}.tar.gz -C target/${{ matrix.target }}/release ti-tools
chmod +x target/${{ matrix.target2 }}/release/ti-tools
tar -zcf target/${{ matrix.target2 }}/release/ti-tools-0.1.1-${{ matrix.target2 }}.tar.gz -C target/${{ matrix.target2 }}/release ti-tools
tar -zcf target/${{ matrix.target2 }}/release/ti-tools-0.1.2-${{ matrix.target2 }}.tar.gz -C target/${{ matrix.target2 }}/release ti-tools
- name: Create tar.gz file on Linux
if: ${{ matrix.os == 'ubuntu-latest' }}
run: |
chmod +x target/${{ matrix.target }}/release/ti-tools
tar -zcf target/${{ matrix.target }}/release/ti-tools-0.1.1-${{ matrix.target }}.tar.gz -C target/${{ matrix.target }}/release ti-tools
tar -zcf target/${{ matrix.target }}/release/ti-tools-0.1.2-${{ matrix.target }}.tar.gz -C target/${{ matrix.target }}/release ti-tools
- name: Upload release and assets to GitHub
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
tag: "release-0.1.1-${{ github.run_number }}"
release_name: ti-tools 0.1.1
tag: "release-0.1.2-${{ github.run_number }}"
release_name: ti-tools 0.1.2
file_glob: true
file: target/*/release/ti-tools-0.1.1-*.{zip,tar.gz}
file: target/*/release/ti-tools-0.1.2-*.{zip,tar.gz}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.DS_Store
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ti-tools"
version = "0.1.0"
version = "0.1.2"
edition = "2021"
authors = ["cqb13 <[email protected]>"]
license = "MIT"
Expand Down
140 changes: 81 additions & 59 deletions src/commands/convert/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub mod txt_to_8xp;
pub mod xp_to_txt;

use std::io::Write;
use std::path::{Path, PathBuf};
use txt_to_8xp::convert_txt_to_8xp;
use xp_to_txt::convert_8xp_to_txt;

pub enum FileType {
enum FileType {
XP,
TXT,
}
Expand Down Expand Up @@ -39,18 +41,17 @@ impl FileType {
pub fn convert_command(
input_path_string: String,
output_path_string: Option<String>,
bytes: bool,
name: Option<String>,
raw: bool,
display: bool,
log_messages: bool,
) {
let (input_path, output_path) =
match confirm_paths(input_path_string, &output_path_string, log_messages) {
Ok((input_path, output_path)) => (input_path, output_path),
Err(err) => {
println!("{}", err);
std::process::exit(0);
}
};
let (input_path, output_path) = match confirm_paths(input_path_string, &output_path_string) {
Ok((input_path, output_path)) => (input_path, output_path),
Err(err) => {
println!("{}", err);
std::process::exit(0);
}
};

let file_type = match get_conversion_file_type(input_path.as_path()) {
Ok(file_type) => file_type,
Expand All @@ -60,57 +61,72 @@ pub fn convert_command(
}
};

let output_file = match file_type {
FileType::XP => convert_8xp_to_txt(input_path, bytes, display, log_messages),
FileType::TXT => convert_txt_to_8xp(input_path, bytes),
};
match file_type {
FileType::XP => {
let output: Vec<String> = convert_8xp_to_txt(input_path, raw, display);
if output_path_string.is_none() {
return;
}

if output_path_string.is_none() {
return;
}
if display || raw {
println!();
}

if display || bytes {
println!();
}
write_to_file(&output_path, output.join(""), "8xp");
}
FileType::TXT => {
if name.is_none() {
println!("You must specify a name for the program");
std::process::exit(0);
}

if log_messages {
println!("Writing to file");
}
if name.as_ref().unwrap().len() > 8 {
println!("Name must be 8 or less characters");
std::process::exit(0);
}

match std::fs::write(output_path, output_file.join("")) {
Ok(_) => {
if log_messages {
println!("Wrote to file");
if !name
.as_ref()
.unwrap()
.chars()
.all(|c| c.is_ascii_alphabetic())
{
println!("Name must be alphabetical characters only");
std::process::exit(0);
}

let output: Vec<u8> =
convert_txt_to_8xp(input_path, name.unwrap().to_string(), raw, display);
if output_path_string.is_none() {
return;
}
}
Err(err) => {
println!("Failed to write to file: {}", err);
std::process::exit(0);
}
}
}

fn convert_txt_to_8xp(input_path: PathBuf, bytes: bool) -> Vec<String> {
Vec::new()
if display || raw {
println!();
}

write_to_file(&output_path, output, "txt");
}
};
}

pub fn print_bytes(file: &Vec<u8>) {
let mut i = 0;
for byte in file {
print!("{:02X}", byte);
i += 1;
if i % 16 == 0 {
println!();
} else {
print!(", ");
fn write_to_file<T: AsRef<[u8]>>(path: &Path, content: T, file_type: &str) {
match std::fs::write(path, content) {
Ok(_) => println!(
"Successfully converted {} to {}",
file_type,
if file_type == "8xp" { "txt" } else { "8xp" }
),
Err(err) => {
eprintln!("Failed to write {}: {}", file_type, err);
std::process::exit(1);
}
}
}

fn confirm_paths(
input_path_string: String,
output_path_string: &Option<String>,
log_messages: bool,
) -> Result<(PathBuf, PathBuf), String> {
let input_path = Path::new(&input_path_string);

Expand All @@ -126,11 +142,8 @@ fn confirm_paths(

let output_path = match output_path_string {
Some(output_path_string) => {
match validate_and_fix_output_path(
output_path_string.to_string(),
file_type.opposite(),
log_messages,
) {
match validate_and_fix_output_path(output_path_string.to_string(), file_type.opposite())
{
Ok(path_buf) => path_buf,
Err(err) => return Err(err),
}
Expand All @@ -155,7 +168,6 @@ fn get_conversion_file_type(path: &Path) -> Result<FileType, String> {
fn validate_and_fix_output_path(
output_path_string: String,
output_file_type: FileType,
log_messages: bool,
) -> Result<PathBuf, String> {
// We don't really care what the user put here, we already know the output type based on input
// So if they messed up we can fix the extension for them
Expand Down Expand Up @@ -185,14 +197,11 @@ fn validate_and_fix_output_path(
std::io::stdin().read_line(&mut input).unwrap();
let input = input.trim();
if input == "y" || input == "Y" {
if log_messages {
println!("Deleting existing file");
}
println!("Deleting existing file");

match std::fs::remove_file(output_path) {
Ok(_) => {
if log_messages {
println!("Deleted existing file");
}
println!("Deleted existing file");
}
Err(err) => {
return Err(format!("Failed to delete file: {}", err));
Expand All @@ -206,3 +215,16 @@ fn validate_and_fix_output_path(

Ok(output_path.to_owned())
}

fn print_bytes(file: &Vec<u8>) {
let mut i = 0;
for byte in file {
print!("{:02X}", byte);
i += 1;
if i % 16 == 0 {
println!();
} else {
print!(", ");
}
}
}
Loading

0 comments on commit 24a79b2

Please sign in to comment.