refactor: use as_encoded_bytes instead of as_bytes

This commit is contained in:
sxyazi 2023-11-25 11:57:44 +08:00
parent 47af821f48
commit f7f1e34649
No known key found for this signature in database
12 changed files with 37 additions and 91 deletions

View file

@ -26,7 +26,7 @@ body:
attributes:
label: Yazi version
description: Please do a `yazi -V` and paste the output here.
placeholder: "ex: yazi 0.1.5"
placeholder: "ex: yazi 0.1.5 (3867c29 2023-11-25)"
validations:
required: true
- type: dropdown

View file

@ -14,7 +14,7 @@ body:
label: Will you be willing to contribute this feature?
description: The feature has a much higher chance of completion if you are willing to get involved!
options:
- label: Yes, I will try to implement it
- label: Yes, I'll give it a shot
- type: textarea
id: solution
attributes:

View file

@ -2,27 +2,26 @@ name: Lock Threads
on:
schedule:
- cron: '5 3 * * *'
- cron: "5 3 * * *"
workflow_dispatch:
permissions:
issues: write
pull-requests: write
concurrency:
group: lock
jobs:
action:
lock:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: dessant/lock-threads@v4
with:
issue-inactive-days: '30'
issue-inactive-days: "30"
issue-comment: >
I'm going to lock this issue because it has been closed for _30 days_. ⏳
This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new
issue and complete the issue template so we can capture all the details
necessary to investigate further.
process-only: 'issues'
process-only: "issues"

View file

@ -4,13 +4,16 @@ on:
issue_comment:
types: [created]
schedule:
- cron: '10 * * * *'
- cron: "10 * * * *"
jobs:
noResponse:
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- uses: lee-dohm/no-response@v0.5.0
with:
token: ${{ github.token }}
daysUntilClose: 7
responseRequiredLabel: waiting on op

View file

@ -29,6 +29,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install stable toolchain
run: rustup toolchain install stable --profile minimal
- name: Add aarch64 target
if: contains(fromJson('["aarch64-unknown-linux-gnu", "aarch64-apple-darwin"]'), matrix.target)
run: rustup target add ${{ matrix.target }}

View file

@ -2,9 +2,9 @@ name: Test
on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]
branches: ["main"]
env:
CARGO_TERM_COLOR: always
@ -20,6 +20,9 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Install stable toolchain
run: rustup toolchain install stable --profile minimal
- name: Cache dependencies
uses: Swatinem/rust-cache@v2

View file

@ -39,7 +39,7 @@ pub async fn clipboard_get() -> Result<OsString> {
#[cfg(unix)]
pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
use std::{os::unix::prelude::OsStrExt, process::Stdio};
use std::process::Stdio;
use anyhow::bail;
use tokio::{io::AsyncWriteExt, process::Command};
@ -64,7 +64,7 @@ pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
};
let mut stdin = child.stdin.take().unwrap();
if stdin.write_all(s.as_ref().as_bytes()).await.is_err() {
if stdin.write_all(s.as_ref().as_encoded_bytes()).await.is_err() {
continue;
}
drop(stdin);

View file

@ -75,16 +75,13 @@ impl Manager {
{
let s = old.iter().map(|o| o.as_os_str()).collect::<Vec<_>>().join(OsStr::new("\n"));
let mut f = OpenOptions::new().write(true).create_new(true).open(&tmp).await?;
#[cfg(windows)]
{
f.write_all(s.to_string_lossy().as_bytes()).await?;
}
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
f.write_all(s.as_bytes()).await?;
}
OpenOptions::new()
.write(true)
.create_new(true)
.open(&tmp)
.await?
.write_all(s.as_encoded_bytes())
.await?;
}
let _guard = BLOCKER.acquire().await.unwrap();

View file

@ -77,31 +77,12 @@ impl Finder {
}
#[inline]
fn matches(&self, name: &OsStr) -> bool {
#[cfg(windows)]
{
self.query.is_match(name.to_string_lossy().as_bytes())
}
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
self.query.is_match(name.as_bytes())
}
}
fn matches(&self, name: &OsStr) -> bool { self.query.is_match(name.as_encoded_bytes()) }
/// Explode the name into three parts: head, body, tail.
#[inline]
pub fn highlighted(&self, name: &OsStr) -> Option<Vec<Range<usize>>> {
#[cfg(windows)]
let found = self.query.find(name.to_string_lossy().as_bytes()).map(|m| m.range());
#[cfg(unix)]
let found = {
use std::os::unix::ffi::OsStrExt;
self.query.find(name.as_bytes()).map(|m| m.range())
};
found.map(|r| vec![r])
self.query.find(name.as_encoded_bytes()).map(|m| vec![m.range()])
}
}

View file

@ -46,16 +46,7 @@ impl App {
fn dispatch_quit(&mut self, no_cwd_file: bool) {
if let Some(p) = BOOT.cwd_file.as_ref().filter(|_| !no_cwd_file) {
let cwd = self.cx.manager.cwd().as_os_str();
#[cfg(windows)]
{
std::fs::write(p, cwd.to_string_lossy().as_bytes()).ok();
}
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
std::fs::write(p, cwd.as_bytes()).ok();
}
std::fs::write(p, cwd.as_encoded_bytes()).ok();
}
Term::goodbye(|| false).unwrap();
}
@ -203,15 +194,7 @@ impl App {
s
});
#[cfg(windows)]
{
std::fs::write(p, paths.to_string_lossy().as_bytes()).ok();
}
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
std::fs::write(p, paths.as_bytes()).ok();
}
std::fs::write(p, paths.as_encoded_bytes()).ok();
return emit!(Quit(false));
}

View file

@ -48,23 +48,8 @@ pub fn expand_path(p: impl AsRef<Path>) -> PathBuf { _expand_path(p.as_ref()) }
#[inline]
pub fn ends_with_slash(p: &Path) -> bool {
// TODO: uncomment this when Rust 1.74 is released
// let b = p.as_os_str().as_encoded_bytes();
// if let [.., last] = b { *last == MAIN_SEPARATOR as u8 } else { false }
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
let b = p.as_os_str().as_bytes();
if let [.., last] = b { *last == MAIN_SEPARATOR as u8 } else { false }
}
#[cfg(windows)]
{
let s = p.to_string_lossy();
let b = s.as_bytes();
if let [.., last] = b { *last == MAIN_SEPARATOR as u8 } else { false }
}
let b = p.as_os_str().as_encoded_bytes();
if let [.., last] = b { *last == MAIN_SEPARATOR as u8 } else { false }
}
pub async fn unique_path(mut p: Url) -> Url {

View file

@ -107,16 +107,8 @@ impl ToString for Url {
UrlScheme::Archive => "archive://",
};
#[cfg(unix)]
let path = {
use std::os::unix::ffi::OsStrExt;
percent_encode(self.path.as_os_str().as_bytes(), ENCODE_SET)
};
#[cfg(windows)]
let path = percent_encode(self.path.to_string_lossy().as_bytes(), ENCODE_SET).to_string();
let path = percent_encode(self.path.as_os_str().as_encoded_bytes(), ENCODE_SET);
let frag = self.frag.as_ref().map(|s| format!("#{s}")).unwrap_or_default();
format!("{scheme}{path}{frag}")
}
}