Skip to content

Commit

Permalink
refactor: simplify building conditions (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Oct 16, 2023
1 parent 54339e7 commit fa5de51
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 26 deletions.
2 changes: 1 addition & 1 deletion app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tracing = "^0"
tracing-appender = "^0"
tracing-subscriber = "^0"

[target.'cfg(not(target_os = "windows"))'.dependencies]
[target."cfg(unix)".dependencies]
libc = "^0"
signal-hook-tokio = { version = "^0", features = [ "futures-v0_3" ] }

Expand Down
8 changes: 4 additions & 4 deletions app/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl App {
if let Some(p) = BOOT.cwd_file.as_ref().filter(|_| !no_cwd_file) {
let cwd = self.cx.manager.cwd().as_os_str();

#[cfg(target_os = "windows")]
#[cfg(windows)]
{
std::fs::write(p, cwd.to_string_lossy().as_bytes()).ok();
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
std::fs::write(p, cwd.as_bytes()).ok();
Expand Down Expand Up @@ -191,11 +191,11 @@ impl App {
s
});

#[cfg(target_os = "windows")]
#[cfg(windows)]
{
std::fs::write(p, paths.to_string_lossy().as_bytes()).ok();
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
std::fs::write(p, paths.as_bytes()).ok();
Expand Down
4 changes: 2 additions & 2 deletions app/src/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ impl Signals {
}
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
fn spawn_system_task(&self) -> Result<()> { Ok(()) }

#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
fn spawn_system_task(&self) -> Result<JoinHandle<()>> {
use libc::{SIGCONT, SIGHUP, SIGINT, SIGQUIT, SIGTERM};

Expand Down
8 changes: 4 additions & 4 deletions config/src/xdg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ impl Xdg {
return Some(expand_path(s));
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
{
dirs::config_dir().map(|p| p.join("yazi").join("config"))
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
env::var_os("XDG_CONFIG_HOME")
.map(PathBuf::from)
Expand All @@ -25,11 +25,11 @@ impl Xdg {
}

pub(super) fn state_dir() -> Option<PathBuf> {
#[cfg(target_os = "windows")]
#[cfg(windows)]
{
dirs::data_dir().map(|p| p.join("yazi").join("state"))
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
env::var_os("XDG_STATE_HOME")
.map(PathBuf::from)
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ yazi-prebuild = "^0"
# Logging
tracing = "^0"

[target.'cfg(target_os = "windows")'.dependencies]
[target."cfg(windows)".dependencies]
clipboard-win = "^4"
8 changes: 4 additions & 4 deletions core/src/external/clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::ffi::OsString;

use anyhow::Result;

#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
pub async fn clipboard_get() -> Result<OsString> {
use std::os::unix::prelude::OsStringExt;

Expand All @@ -28,7 +28,7 @@ pub async fn clipboard_get() -> Result<OsString> {
bail!("failed to get clipboard")
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
pub async fn clipboard_get() -> Result<OsString> {
use anyhow::anyhow;
use clipboard_win::{formats, get_clipboard};
Expand All @@ -37,7 +37,7 @@ pub async fn clipboard_get() -> Result<OsString> {
Ok(result.await?.map_err(|_| anyhow!("failed to get clipboard"))?.into())
}

#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
use std::{os::unix::prelude::OsStrExt, process::Stdio};

Expand Down Expand Up @@ -77,7 +77,7 @@ pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
bail!("failed to set clipboard")
}

#[cfg(target_os = "windows")]
#[cfg(windows)]
pub async fn clipboard_set(s: impl AsRef<std::ffi::OsStr>) -> Result<()> {
use anyhow::anyhow;
use clipboard_win::{formats, set_clipboard};
Expand Down
4 changes: 2 additions & 2 deletions core/src/manager/commands/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ 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(target_os = "windows")]
#[cfg(windows)]
{
f.write_all(s.to_string_lossy().as_bytes()).await?;
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
f.write_all(s.as_bytes()).await?;
Expand Down
6 changes: 3 additions & 3 deletions core/src/manager/commands/suspend.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{emit, manager::Manager};
use crate::manager::Manager;

impl Manager {
pub fn suspend(&mut self) -> bool {
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
tokio::spawn(async move {
emit!(Stop(true)).await;
crate::emit!(Stop(true)).await;
unsafe { libc::raise(libc::SIGTSTP) };
});
false
Expand Down
4 changes: 2 additions & 2 deletions core/src/tab/finder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ impl Finder {

#[inline]
fn matches(&self, name: &OsStr) -> bool {
#[cfg(target_os = "windows")]
#[cfg(windows)]
{
self.query.is_match(name.to_string_lossy().as_bytes())
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
use std::os::unix::ffi::OsStrExt;
self.query.is_match(name.as_bytes())
Expand Down
2 changes: 1 addition & 1 deletion shared/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ pub fn max_common_root(files: &[impl AsRef<Path>]) -> PathBuf {
root
}

#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
#[test]
fn test_max_common_root() {
assert_eq!(max_common_root(&[] as &[PathBuf]).as_os_str(), "");
Expand Down
4 changes: 2 additions & 2 deletions shared/src/term/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Term {
W: Write,
F: FnOnce(&mut W) -> Result<()>,
{
#[cfg(target_os = "windows")]
#[cfg(windows)]
{
use std::{thread, time::Duration};

Expand All @@ -39,7 +39,7 @@ impl Term {
stdout.flush()?;
result
}
#[cfg(not(target_os = "windows"))]
#[cfg(unix)]
{
queue!(&mut stdout, SavePosition, MoveTo(x, y))?;
let result = cb(&mut stdout);
Expand Down

0 comments on commit fa5de51

Please sign in to comment.