mirror of
https://github.com/lune-org/lune.git
synced 2025-04-10 21:40:54 +01:00
feat: impl readToEnd convenience method for reader
Also rename success key in table to be "ok", so that it is consistent with process.exec.
This commit is contained in:
parent
900b6715b6
commit
8ce47806eb
3 changed files with 13 additions and 3 deletions
|
@ -264,7 +264,7 @@ async fn process_spawn(
|
||||||
|
|
||||||
TableBuilder::new(lua)?
|
TableBuilder::new(lua)?
|
||||||
.with_value("code", code)?
|
.with_value("code", code)?
|
||||||
.with_value("success", code == 0)?
|
.with_value("ok", code == 0)?
|
||||||
.build_readonly()
|
.build_readonly()
|
||||||
}
|
}
|
||||||
})?
|
})?
|
||||||
|
|
|
@ -17,6 +17,12 @@ impl<R: AsyncRead + Unpin> ChildProcessReader<R> {
|
||||||
|
|
||||||
Ok(buf.to_vec())
|
Ok(buf.to_vec())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn read_to_end(&mut self) -> LuaResult<Vec<u8>> {
|
||||||
|
let mut buf = vec![];
|
||||||
|
self.0.read_to_end(&mut buf).await?;
|
||||||
|
Ok(buf)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> {
|
impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> {
|
||||||
|
@ -24,6 +30,10 @@ impl<R: AsyncRead + Unpin + 'static> LuaUserData for ChildProcessReader<R> {
|
||||||
methods.add_async_method_mut("read", |lua, this, ()| async {
|
methods.add_async_method_mut("read", |lua, this, ()| async {
|
||||||
Ok(lua.create_buffer(this.read().await?))
|
Ok(lua.create_buffer(this.read().await?))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
methods.add_async_method_mut("readToEnd", |lua, this, ()| async {
|
||||||
|
Ok(lua.create_buffer(this.read_to_end().await?))
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
local process = require("@lune/process")
|
local process = require("@lune/process")
|
||||||
|
|
||||||
local a = process.spawn("yes", {})
|
local a = process.spawn("echo", {"hello"})
|
||||||
|
|
||||||
print(a)
|
print(a)
|
||||||
print(buffer.tostring(a.stdout:read()))
|
print(buffer.tostring(a.stdout:readToEnd()))
|
||||||
print(a.status())
|
print(a.status())
|
Loading…
Add table
Reference in a new issue