reads a line of input, appending it to the specified buffer. API from : Read all bytes until a newline (the 0xA byte) is reached, and append them to the provided buffer. You do not need to clear the buffer before appending. This function will read bytes from the underlying stream until the newline delimiter (the 0xA by
(&self, buf: &mut String)
| 91 | /// ever sending a newline or EOF. |
| 92 | /// |
| 93 | pub fn read_line(&self, buf: &mut String) -> Result<usize, SyscallError> { |
| 94 | let mut length = 0; |
| 95 | loop { |
| 96 | match syscalls::receive(&STDIN) { |
| 97 | Ok(syscalls::Message::Short( |
| 98 | message::CHAR, ch, _)) => { |
| 99 | // Received a character |
| 100 | if ch == 0x8 { |
| 101 | // Backspace |
| 102 | if let Some(_) = buf.pop() { |
| 103 | // If there is a character to remove |
| 104 | // Erase by overwriting with a space |
| 105 | print!("\u{08} \u{08}"); |
| 106 | } |
| 107 | } else { |
| 108 | // Echo character to stdout |
| 109 | _ = syscalls::send(&STDOUT, syscalls::Message::Short( |
| 110 | message::CHAR, ch, 0)); // Not really bothered if it fails |
| 111 | if let Some(utf_ch) = char::from_u32(ch as u32) { |
| 112 | // If it's a UTF char then append to buffer |
| 113 | buf.push(utf_ch); |
| 114 | length += 1; |
| 115 | if ch == 0xA { |
| 116 | return Ok(length); |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | _ => { |
| 122 | // Ignore |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 |