Skip to content

Commit d620fde

Browse files
Merge pull request #8 from RandomSearch18/multiline-bin-input
Improve bin_creator to accept multi-line input
2 parents fe91cba + 1e91927 commit d620fde

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ cargo run --bin rusty_man_computer -- --ram demos/factorial.bin
7575

7676
At the moment, the easiest way to write a program is using the assembly language in the [online simulator](https://peterhigginson.co.uk/lmc/). Write the assembly code, click "ASSEMBLE INTO RAM", and click-and-drag to copy the contents of the memory text boxes (you can leave out any empty memory at the end).
7777

78-
At the moment, you have to use a text editor to ensure that each memory cell is only separated by a space, and not a line break. (This can also be accomplished by pasting the memory contents into the address bar of your browser, and copying it from there.)
79-
8078
Then you can run `bin_creator`, giving it the file name that the binary file should be written to, e.g.
8179

8280
```bash
8381
cargo run --bin bin_creator -- my_program.bin
8482
```
8583

84+
Follow the prompt to paste the memory data into your terminal, and press Enter twice in a row to mark the end of the data.
85+
8686
Then you can run the program as described above, e.g.
8787

8888
```bash

src/bin_creator.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@ fn main() -> Result<(), Box<dyn Error>> {
1414
let filename = &args[1];
1515

1616
// Let the user paste in a string
17-
println!("Paste in the memory data:");
17+
println!("Paste the memory data below, then press Enter twice to submit:");
1818
let mut line = String::new();
1919
let stdin = io::stdin();
20-
stdin.lock().read_line(&mut line)?;
20+
// Accept input until enter is pressed twice
21+
loop {
22+
stdin.lock().read_line(&mut line)?;
23+
if line.chars().rev().take(2).collect::<String>() == "\n\n" {
24+
break;
25+
}
26+
}
2127

2228
// Split the string into a vector i16 signed ints
2329
let memory_data_items: Vec<i16> = line
@@ -34,5 +40,6 @@ fn main() -> Result<(), Box<dyn Error>> {
3440
// Write the memory data to a binary file
3541
fs::write(filename, memory_data_bytes)?;
3642

43+
println!("Successfully created binary file: {}", filename);
3744
Ok(())
3845
}

0 commit comments

Comments
 (0)