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: attributes:
label: Yazi version label: Yazi version
description: Please do a `yazi -V` and paste the output here. 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: validations:
required: true required: true
- type: dropdown - type: dropdown

View file

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

View file

@ -2,27 +2,26 @@ name: Lock Threads
on: on:
schedule: schedule:
- cron: '5 3 * * *' - cron: "5 3 * * *"
workflow_dispatch: workflow_dispatch:
permissions:
issues: write
pull-requests: write
concurrency: concurrency:
group: lock group: lock
jobs: jobs:
action: lock:
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps: steps:
- uses: dessant/lock-threads@v4 - uses: dessant/lock-threads@v4
with: with:
issue-inactive-days: '30' issue-inactive-days: "30"
issue-comment: > issue-comment: >
I'm going to lock this issue because it has been closed for _30 days_. ⏳ 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. 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 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 issue and complete the issue template so we can capture all the details
necessary to investigate further. necessary to investigate further.
process-only: 'issues' process-only: "issues"

View file

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

View file

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

View file

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

View file

@ -39,7 +39,7 @@ pub async fn clipboard_get() -> Result<OsString> {
#[cfg(unix)] #[cfg(unix)]
pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> { 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 anyhow::bail;
use tokio::{io::AsyncWriteExt, process::Command}; 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(); 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; continue;
} }
drop(stdin); 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 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?; OpenOptions::new()
#[cfg(windows)] .write(true)
{ .create_new(true)
f.write_all(s.to_string_lossy().as_bytes()).await?; .open(&tmp)
} .await?
#[cfg(unix)] .write_all(s.as_encoded_bytes())
{ .await?;
use std::os::unix::ffi::OsStrExt;
f.write_all(s.as_bytes()).await?;
}
} }
let _guard = BLOCKER.acquire().await.unwrap(); let _guard = BLOCKER.acquire().await.unwrap();

View file

@ -77,31 +77,12 @@ impl Finder {
} }
#[inline] #[inline]
fn matches(&self, name: &OsStr) -> bool { fn matches(&self, name: &OsStr) -> bool { self.query.is_match(name.as_encoded_bytes()) }
#[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())
}
}
/// Explode the name into three parts: head, body, tail. /// Explode the name into three parts: head, body, tail.
#[inline] #[inline]
pub fn highlighted(&self, name: &OsStr) -> Option<Vec<Range<usize>>> { pub fn highlighted(&self, name: &OsStr) -> Option<Vec<Range<usize>>> {
#[cfg(windows)] self.query.find(name.as_encoded_bytes()).map(|m| vec![m.range()])
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])
} }
} }

View file

@ -46,16 +46,7 @@ impl App {
fn dispatch_quit(&mut self, no_cwd_file: bool) { fn dispatch_quit(&mut self, no_cwd_file: bool) {
if let Some(p) = BOOT.cwd_file.as_ref().filter(|_| !no_cwd_file) { if let Some(p) = BOOT.cwd_file.as_ref().filter(|_| !no_cwd_file) {
let cwd = self.cx.manager.cwd().as_os_str(); let cwd = self.cx.manager.cwd().as_os_str();
std::fs::write(p, cwd.as_encoded_bytes()).ok();
#[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();
}
} }
Term::goodbye(|| false).unwrap(); Term::goodbye(|| false).unwrap();
} }
@ -203,15 +194,7 @@ impl App {
s s
}); });
#[cfg(windows)] std::fs::write(p, paths.as_encoded_bytes()).ok();
{
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();
}
return emit!(Quit(false)); 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] #[inline]
pub fn ends_with_slash(p: &Path) -> bool { 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();
// let b = p.as_os_str().as_encoded_bytes(); if let [.., last] = b { *last == MAIN_SEPARATOR as u8 } else { false }
// 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 }
}
} }
pub async fn unique_path(mut p: Url) -> Url { pub async fn unique_path(mut p: Url) -> Url {

View file

@ -107,16 +107,8 @@ impl ToString for Url {
UrlScheme::Archive => "archive://", UrlScheme::Archive => "archive://",
}; };
#[cfg(unix)] let path = percent_encode(self.path.as_os_str().as_encoded_bytes(), ENCODE_SET);
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 frag = self.frag.as_ref().map(|s| format!("#{s}")).unwrap_or_default(); let frag = self.frag.as_ref().map(|s| format!("#{s}")).unwrap_or_default();
format!("{scheme}{path}{frag}") format!("{scheme}{path}{frag}")
} }
} }