├── cargo ├── Cargo.toml ├── art.txt ├── README.md ├── .gitignore └── src └── main.rs /cargo: -------------------------------------------------------------------------------- 1 | Hello form input run . 2 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 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] 9 | -------------------------------------------------------------------------------- /art.txt: -------------------------------------------------------------------------------- 1 | __ _ 2 | \.'---.//| 3 | |\./| \/ 4 | _|.|.|_ \ 5 | /( ) ' ' \ 6 | | \/ . | \ 7 | jrjc \_/\__/| | 8 | V /V / | 9 | /__/ / 10 | \___/\ 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 | 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 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 15 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | use std::io::{self, Read}; 2 | 3 | fn read_std_in() -> io::Result { 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 | } 67 | --------------------------------------------------------------------------------