Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Jul 22, 2024
1 parent c4fe848 commit 31a2eb7
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cspell.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","imagesize","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip"],"version":"0.2","language":"en","flagWords":[]}
{"language":"en","words":["Punct","KEYMAP","splitn","crossterm","YAZI","peekable","ratatui","syntect","pbpaste","pbcopy","ffmpegthumbnailer","oneshot","Posix","Lsar","XADDOS","zoxide","cands","Deque","precache","imageops","IFBLK","IFCHR","IFDIR","IFIFO","IFLNK","IFMT","IFSOCK","IRGRP","IROTH","IRUSR","ISGID","ISUID","ISVTX","IWGRP","IWOTH","IWUSR","IXGRP","IXOTH","IXUSR","libc","winsize","TIOCGWINSZ","xpixel","ypixel","ioerr","appender","Catppuccin","macchiato","gitmodules","Dotfiles","bashprofile","vimrc","flac","webp","exiftool","mediainfo","ripgrep","nvim","indexmap","indexmap","unwatch","canonicalize","serde","fsevent","Ueberzug","iterm","wezterm","sixel","chafa","ueberzugpp","️ Überzug","️ Überzug","Konsole","Alacritty","Überzug","pkgs","paru","unarchiver","pdftoppm","poppler","prebuild","singlefile","jpegopt","EXIF","rustfmt","mktemp","nanos","xclip","xsel","natord","Mintty","nixos","nixpkgs","SIGTSTP","SIGCONT","SIGCONT","mlua","nonstatic","userdata","metatable","natsort","backstack","luajit","Succ","Succ","cand","fileencoding","foldmethod","lightgreen","darkgray","lightred","lightyellow","lightcyan","nushell","msvc","aarch","linemode","sxyazi","rsplit","ZELLIJ","bitflags","bitflags","USERPROFILE","Neovim","vergen","gitcl","Renderable","preloaders","prec","imagesize","Upserting","prio","Ghostty","Catmull","Lanczos","cmds","unyank","scrolloff","headsup","unsub","uzers","scopeguard","SPDLOG","globset","filetime","magick","magick","prefetcher","Prework","prefetchers","PREWORKERS","conds","translit","rxvt","Urxvt","realpath","realname","REPARSE","hardlink","hardlinking","nlink","nlink","linemodes","SIGSTOP","sevenzip","fract"],"flagWords":[],"version":"0.2"}
8 changes: 4 additions & 4 deletions yazi-cli/src/args.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::HashMap};
use std::borrow::Cow;

use anyhow::{bail, Result};
use clap::{command, Parser, Subcommand};
Expand Down Expand Up @@ -106,10 +106,10 @@ macro_rules! impl_body {
Ok(json.into())
} else if let Some(str) = &self.str {
Ok(serde_json::to_string(str)?.into())
} else if !self.list.is_empty() {
Ok(serde_json::to_string(&self.list)?.into())
} else {
let map: HashMap<usize, &str> =
self.list.iter().enumerate().map(|(i, s)| (i + 1, s.as_str())).collect();
Ok(serde_json::to_string(&map)?.into())
Ok("".into())
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions yazi-core/src/manager/commands/update_mimetype.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;

use tracing::error;
use yazi_shared::{event::Cmd, fs::Url, render};

use crate::{manager::{Manager, LINKED}, tasks::Tasks};
Expand All @@ -12,14 +13,14 @@ impl TryFrom<Cmd> for Opt {
type Error = ();

fn try_from(mut c: Cmd) -> Result<Self, Self::Error> {
Ok(Self { updates: c.take("updates").ok_or(())?.into_table_string() })
Ok(Self { updates: c.take("updates").ok_or(())?.into_dict_string() })
}
}

impl Manager {
pub fn update_mimetype(&mut self, opt: impl TryInto<Opt>, tasks: &Tasks) {
let Ok(opt) = opt.try_into() else {
return;
return error!("invalid arguments for update_mimetype");
};

let linked = LINKED.read();
Expand Down
25 changes: 18 additions & 7 deletions yazi-dds/src/sendable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,22 @@ impl Sendable {
Value::Number(n) => Data::Number(n),
Value::String(s) => Data::String(s.to_str()?.to_owned()),
Value::Table(t) => {
let mut map = HashMap::with_capacity(t.raw_len());
let (mut i, mut map) = (0, HashMap::with_capacity(t.raw_len()));
for result in t.pairs::<Value, Value>() {
let (k, v) = result?;
map.insert(Self::value_to_key(k)?, Self::value_to_data(v)?);
let k = Self::value_to_key(k)?;

if k == DataKey::Integer(i) {
i += 1;
}
map.insert(k, Self::value_to_data(v)?);
}

if i as usize == map.len() {
Data::List(map.into_values().collect())
} else {
Data::Dict(map)
}
Data::Table(map)
}
Value::Function(_) => Err("function is not supported".into_lua_err())?,
Value::Thread(_) => Err("thread is not supported".into_lua_err())?,
Expand All @@ -44,8 +54,9 @@ impl Sendable {
Data::Integer(v) => Value::Integer(v),
Data::Number(v) => Value::Number(v),
Data::String(v) => Value::String(lua.create_string(v)?),
Data::Table(t) => {
let seq_len = t.keys().filter(|&k| !k.is_numeric()).count();
Data::List(v) => Value::Table(Self::list_to_table(lua, v)?),
Data::Dict(t) => {
let seq_len = t.keys().filter(|&k| !k.is_integer()).count();
let table = lua.create_table_with_capacity(seq_len, t.len() - seq_len)?;
for (k, v) in t {
table.raw_set(Self::key_to_value(lua, k)?, Self::data_to_value(lua, v)?)?;
Expand All @@ -63,15 +74,15 @@ impl Sendable {
})
}

pub fn vec_to_table(lua: &Lua, data: Vec<Data>) -> mlua::Result<Table> {
pub fn list_to_table(lua: &Lua, data: Vec<Data>) -> mlua::Result<Table> {
let mut vec = Vec::with_capacity(data.len());
for v in data.into_iter() {
vec.push(Self::data_to_value(lua, v)?);
}
lua.create_sequence_from(vec)
}

pub fn vec_to_variadic(lua: &Lua, data: Vec<Data>) -> mlua::Result<Variadic<Value>> {
pub fn list_to_variadic(lua: &Lua, data: Vec<Data>) -> mlua::Result<Variadic<Value>> {
let mut vec = Vec::with_capacity(data.len());
for v in data {
vec.push(Self::data_to_value(lua, v)?);
Expand Down
2 changes: 1 addition & 1 deletion yazi-fm/src/app/commands/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl App {
if let Some(cb) = opt.cb {
cb(&LUA, plugin)
} else {
plugin.call_method("entry", Sendable::vec_to_table(&LUA, opt.args)?)
plugin.call_method("entry", Sendable::list_to_table(&LUA, opt.args)?)
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/src/isolate/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub async fn entry(name: String, args: Vec<Data>) -> mlua::Result<()> {
};

Handle::current()
.block_on(plugin.call_async_method("entry", Sendable::vec_to_table(&lua, args)))
.block_on(plugin.call_async_method("entry", Sendable::list_to_table(&lua, args)))
})
.await
.into_lua_err()?
Expand Down
2 changes: 1 addition & 1 deletion yazi-plugin/src/utils/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl Utils {
return Err("`ya.sync()` must be called in a plugin").into_lua_err();
};

Sendable::vec_to_variadic(lua, Self::retrieve(cur, block, args).await?)
Sendable::list_to_variadic(lua, Self::retrieve(cur, block, args).await?)
})
})?,
)?;
Expand Down
13 changes: 7 additions & 6 deletions yazi-shared/src/event/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ pub enum Data {
Integer(i64),
Number(f64),
String(String),
Table(HashMap<DataKey, Data>),
List(Vec<Data>),
Dict(HashMap<DataKey, Data>),
#[serde(skip_deserializing)]
Url(Url),
#[serde(skip)]
Expand Down Expand Up @@ -64,13 +65,13 @@ impl Data {
}
}

pub fn into_table_string(self) -> HashMap<String, String> {
let Self::Table(table) = self else {
pub fn into_dict_string(self) -> HashMap<String, String> {
let Self::Dict(dict) = self else {
return Default::default();
};

let mut map = HashMap::with_capacity(table.len());
for pair in table {
let mut map = HashMap::with_capacity(dict.len());
for pair in dict {
if let (DataKey::String(k), Self::String(v)) = pair {
map.insert(k, v);
}
Expand Down Expand Up @@ -103,7 +104,7 @@ pub enum DataKey {

impl DataKey {
#[inline]
pub fn is_numeric(&self) -> bool { matches!(self, Self::Integer(_) | Self::Number(_)) }
pub fn is_integer(&self) -> bool { matches!(self, Self::Integer(_)) }
}

// --- Macros
Expand Down
2 changes: 1 addition & 1 deletion yazi-shared/src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl OrderedFloat {
}

#[inline]
pub fn get(&self) -> f64 { self.0 }
pub const fn get(&self) -> f64 { self.0 }
}

impl Hash for OrderedFloat {
Expand Down

0 comments on commit 31a2eb7

Please sign in to comment.