diff options
| author | Max Verevkin <maxxverrr@gmail.com> | 2022-04-23 12:34:22 +0300 |
|---|---|---|
| committer | Max Verevkin <maxxverrr@gmail.com> | 2022-04-23 12:34:22 +0300 |
| commit | 7a9325b7dfb7a2a2285ed459465b58f540a1f11f (patch) | |
| tree | 04406cf9f6821904a92020e98c97af84ed923cf1 /src/status_cmd.rs | |
| parent | 16ef1913ce3f740a8a08c8e047d321600ef04498 (diff) | |
| download | i3bar-mango-7a9325b7dfb7a2a2285ed459465b58f540a1f11f.tar.gz | |
refactor
Diffstat (limited to 'src/status_cmd.rs')
| -rw-r--r-- | src/status_cmd.rs | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/src/status_cmd.rs b/src/status_cmd.rs index 9824be4..7cbdead 100644 --- a/src/status_cmd.rs +++ b/src/status_cmd.rs @@ -14,6 +14,7 @@ pub struct StatusCmd { output: BufReader<ChildStdout>, input: ChildStdin, protocol: Protocol, + buf: Vec<u8>, } impl StatusCmd { @@ -30,12 +31,27 @@ impl StatusCmd { output: BufReader::new(output), input, protocol: Protocol::Unknown, + buf: Vec::new(), }) } pub fn notify_available(&mut self) -> Result<Option<Vec<Block>>> { let buf = self.output.fill_buf()?; - self.protocol.process_new_bytes(buf)?; + if self.buf.is_empty() { + let rem = self.protocol.process_new_bytes(buf)?; + if !rem.is_empty() { + self.buf.extend_from_slice(rem); + } + } else { + self.buf.extend_from_slice(buf); + let rem = self.protocol.process_new_bytes(&self.buf)?; + if rem.is_empty() { + self.buf.clear(); + } else { + let used = self.buf.len() - rem.len(); + self.buf.drain(..used); + } + } let consumed = buf.len(); self.output.consume(consumed); Ok(self.protocol.get_blocks()) |
