feat: support reading non-UTF8 data with Child:read_line() API (#1816)

This commit is contained in:
三咲雅 · Misaki Masa 2024-10-21 02:03:56 +08:00 committed by sxyazi
parent 24a77d8a5f
commit 16881aab2f
No known key found for this signature in database
4 changed files with 25 additions and 21 deletions

View File

@ -79,7 +79,7 @@ end
--- 2: wrong password --- 2: wrong password
--- 3: partial success --- 3: partial success
function M.list_files(args, skip, limit) function M.list_files(args, skip, limit)
local child = M.spawn_7z { "l", "-ba", "-slt", table.unpack(args) } local child = M.spawn_7z { "l", "-ba", "-slt", "-sccUTF-8", table.unpack(args) }
if not child then if not child then
return {}, 0, 1 return {}, 0, 1
end end
@ -136,7 +136,7 @@ end
--- 2: wrong password --- 2: wrong password
--- 3: partial success --- 3: partial success
function M.list_meta(args) function M.list_meta(args)
local child = M.spawn_7z { "l", "-slt", table.unpack(args) } local child = M.spawn_7z { "l", "-slt", "-sccUTF-8", table.unpack(args) }
if not child then if not child then
return nil, 1 return nil, 1
end end

View File

@ -48,7 +48,7 @@ function M:try_with(from, pwd, to)
end end
local archive = require("archive") local archive = require("archive")
local child, code = archive.spawn_7z { "x", "-aou", "-p" .. pwd, "-o" .. tostring(tmp), tostring(from) } local child, code = archive.spawn_7z { "x", "-aou", "-sccUTF-8", "-p" .. pwd, "-o" .. tostring(tmp), tostring(from) }
if not child then if not child then
fail("Spawn `7z` and `7zz` both commands failed, error code %s", code) fail("Spawn `7z` and `7zz` both commands failed, error code %s", code)
end end

View File

@ -26,21 +26,19 @@ impl Child {
impl UserData for Child { impl UserData for Child {
fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) { fn add_methods<'lua, M: mlua::UserDataMethods<'lua, Self>>(methods: &mut M) {
#[inline] #[inline]
// TODO: return mlua::String instead of String async fn read_line(me: &mut Child) -> (Option<Vec<u8>>, u8) {
async fn read_line(me: &mut Child) -> (String, u8) { async fn read(r: Option<impl AsyncBufReadExt + Unpin>) -> Option<Vec<u8>> {
async fn read(r: Option<impl AsyncBufReadExt + Unpin>) -> Option<String> { let mut buf = Vec::new();
let mut r = r?; match r?.read_until(b'\n', &mut buf).await {
let mut buf = String::new();
match r.read_line(&mut buf).await {
Ok(0) | Err(_) => None, Ok(0) | Err(_) => None,
Ok(_) => Some(buf), Ok(_) => Some(buf),
} }
} }
select! { select! {
Some(r) = read(me.stdout.as_mut()) => (r, 0u8), r @ Some(_) = read(me.stdout.as_mut()) => (r, 0u8),
Some(r) = read(me.stderr.as_mut()) => (r, 1u8), r @ Some(_) = read(me.stderr.as_mut()) => (r, 1u8),
else => (String::new(), 2u8), else => (None, 2u8),
} }
} }
@ -61,12 +59,20 @@ impl UserData for Child {
else => (vec![], 2u8) else => (vec![], 2u8)
}) })
}); });
methods.add_async_method_mut("read_line", |_, me, ()| async move { Ok(read_line(me).await) }); methods.add_async_method_mut("read_line", |lua, me, ()| async move {
methods.add_async_method_mut("read_line_with", |_, me, options: Table| async move { match read_line(me).await {
let timeout: u64 = options.raw_get("timeout")?; (Some(b), event) => (lua.create_string(b)?, event).into_lua_multi(lua),
match tokio::time::timeout(Duration::from_millis(timeout), read_line(me)).await { (None, event) => (Value::Nil, event).into_lua_multi(lua),
Ok(value) => Ok(value), }
Err(_) => Ok((String::new(), 3u8)), });
methods.add_async_method_mut("read_line_with", |lua, me, options: Table| async move {
let timeout = Duration::from_millis(options.raw_get("timeout")?);
let Ok(result) = tokio::time::timeout(timeout, read_line(me)).await else {
return (Value::Nil, 3u8).into_lua_multi(lua);
};
match result {
(Some(b), event) => (lua.create_string(b)?, event).into_lua_multi(lua),
(None, event) => (Value::Nil, event).into_lua_multi(lua),
} }
}); });

View File

@ -3,8 +3,6 @@ use std::{fs::{FileType, Metadata}, path::Path, time::SystemTime};
use bitflags::bitflags; use bitflags::bitflags;
use yazi_macro::unix_either; use yazi_macro::unix_either;
use super::Urn;
bitflags! { bitflags! {
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ChaKind: u8 { pub struct ChaKind: u8 {
@ -149,7 +147,7 @@ impl Cha {
let mut attached = ChaKind::empty(); let mut attached = ChaKind::empty();
#[cfg(unix)] #[cfg(unix)]
if Urn::new(path).is_hidden() { if super::Urn::new(path).is_hidden() {
attached |= ChaKind::HIDDEN; attached |= ChaKind::HIDDEN;
} }
#[cfg(windows)] #[cfg(windows)]