Skip to content

Commit 878d095

Browse files
Initial
0 parents  commit 878d095

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Generated by Cargo
2+
# will have compiled files and executables
3+
debug/
4+
target/
5+
6+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
7+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
8+
Cargo.lock
9+
10+
# These are backup files generated by rustfmt
11+
**/*.rs.bk
12+
13+
# MSVC Windows builds of rustc generate these, which store debugging information
14+
*.pdb

Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "ascii-lua-table"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## Ascii Lua table
2+
3+
Very small project generate a lua table from a string.
4+
5+
6+
## Usage
7+
8+
Copy the ascii art to art.txt and run the following
9+
10+
```bash
11+
cat animal.txt | cargo run .
12+
```
13+

art.txt

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
__ _
2+
\.'---.//|
3+
|\./| \/
4+
_|.|.|_ \
5+
/( ) ' ' \
6+
| \/ . | \
7+
jrjc \_/\__/| |
8+
V /V / |
9+
/__/ /
10+
\___/\

cargo

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello form input run .

src/main.rs

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use std::io::{self, Read};
2+
3+
fn read_std_in() -> io::Result<String> {
4+
let mut buffer = String::new();
5+
let stdin = io::stdin();
6+
let mut handle = stdin.lock();
7+
8+
handle.read_to_string(&mut buffer)?;
9+
10+
Ok(buffer)
11+
}
12+
13+
fn find_longest_line(input: &str) -> &str {
14+
return input.lines().max_by_key(|line| line.len()).unwrap()
15+
}
16+
17+
fn str_to_lua_table(s: &str) -> String {
18+
let mut result = String::new();
19+
let mut first = true;
20+
21+
let longest_line = find_longest_line(s);
22+
let longest_line_len = longest_line.len();
23+
24+
for line in s.lines() {
25+
if first {
26+
first = false;
27+
} else {
28+
result.push_str(", \n");
29+
}
30+
31+
let line_len = line.len();
32+
33+
let diff = longest_line_len - line_len;
34+
35+
let to_push: String;
36+
37+
let newline = line.replace("\\", "\\\\").replace("\"", "\\\"");
38+
39+
if line_len < longest_line_len {
40+
let padding = " ".repeat(diff);
41+
42+
to_push = format!("\"{}{}\"", newline, padding)
43+
} else {
44+
to_push = format!("\"{}\"", newline)
45+
}
46+
47+
// transform to escaped lua string but keep quotes on the end
48+
49+
result.push_str(&to_push);
50+
println!("Diff: {}", diff);
51+
}
52+
53+
return format!("{{{}}}", result)
54+
}
55+
56+
fn main() {
57+
let input = read_std_in();
58+
59+
match input {
60+
Ok(s) => {
61+
let lua_table = str_to_lua_table(&s);
62+
print!("Lua table:\n{}\n", lua_table);
63+
},
64+
Err(e) => println!("Error: {}", e),
65+
}
66+
}

0 commit comments

Comments
 (0)