From 74cc46a2383db5538d7e0392e4b79b7287775e9b Mon Sep 17 00:00:00 2001 From: MaxVerevkin Date: Thu, 7 Sep 2023 00:47:28 +0300 Subject: hyprland again --- src/status_cmd.rs | 33 ++++----------------------------- 1 file changed, 4 insertions(+), 29 deletions(-) (limited to 'src/status_cmd.rs') diff --git a/src/status_cmd.rs b/src/status_cmd.rs index 217ddab..0d6f049 100644 --- a/src/status_cmd.rs +++ b/src/status_cmd.rs @@ -1,10 +1,11 @@ -use std::io::{self, BufWriter, ErrorKind, Write}; -use std::os::unix::io::{AsRawFd, RawFd}; +use std::io::{BufWriter, ErrorKind, Write}; +use std::os::unix::io::AsRawFd; use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio}; use anyhow::Result; use crate::i3bar_protocol::{Block, Event, Protocol}; +use crate::utils::read_to_vec; #[derive(Debug)] pub struct StatusCmd { @@ -38,7 +39,7 @@ impl StatusCmd { } pub fn receive_blocks(&mut self) -> Result>> { - match read_to_vec(self.output.as_raw_fd(), &mut self.buf) { + match read_to_vec(&mut self.output, &mut self.buf) { Ok(0) => bail!("status command exited"), Ok(_n) => (), Err(e) if e.kind() == ErrorKind::WouldBlock => return Ok(None), @@ -61,29 +62,3 @@ impl StatusCmd { Ok(()) } } - -/// Read from a raw file descriptor to the vector. -/// -/// Appends data at the end of the buffer. Resizes vector as needed. -pub fn read_to_vec(fd: RawFd, buf: &mut Vec) -> io::Result { - if buf.capacity() - buf.len() < 1024 { - buf.reserve(buf.capacity().max(1024)); - } - - let res = unsafe { - libc::read( - fd, - buf.as_mut_ptr().add(buf.len()) as *mut libc::c_void, - (buf.capacity() - buf.len()) as libc::size_t, - ) - }; - - if res == -1 { - return Err(io::Error::last_os_error()); - } - - let read = res as usize; - unsafe { buf.set_len(buf.len() + read) }; - - Ok(read) -} -- cgit v1.3.1