fix: Windows build and add github action (#228)

This commit is contained in:
Nguyễn Đức Toàn 2023-09-30 14:36:58 +07:00 committed by sxyazi
parent 492929a0b8
commit d3d3462b5e
No known key found for this signature in database
4 changed files with 41 additions and 17 deletions

25
.github/workflows/rust.yml vendored Normal file
View file

@ -0,0 +1,25 @@
name: Rust
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
env:
CARGO_TERM_COLOR: always
jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

View file

@ -65,12 +65,19 @@ impl<'a> Folder<'a> {
let v = self.is_find.then_some(()).and_then(|_| {
let finder = self.cx.manager.active().finder()?;
let (head, body, tail) = finder.explode(short.name)?;
#[cfg(target_os = "windows")]
let (head, body, tail) = finder.explode(short.name.to_string_lossy().as_bytes())?;
#[cfg(not(target_os = "windows"))]
let (head, body, tail) = {
use std::os::unix::ffi::OsStrExt;
finder.explode(short.name.as_bytes())?
};
// TODO: to be configured by THEME?
let style = Style::new().fg(Color::Rgb(255, 255, 50)).add_modifier(Modifier::ITALIC);
Some(vec![
Span::raw(short.prefix.join(head.as_ref()).display().to_string()),
Span::raw(short.prefix.join(head).display().to_string()),
Span::styled(body, style),
Span::raw(tail),
])

View file

@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::BTreeMap, ffi::OsStr};
use std::{collections::BTreeMap, ffi::OsStr};
use anyhow::Result;
use regex::bytes::Regex;
@ -111,21 +111,12 @@ impl Finder {
/// Explode the name into three parts: head, body, tail.
#[inline]
pub fn explode<'a>(&self, name: &'a OsStr) -> Option<(Cow<'a, str>, Cow<'a, str>, Cow<'a, str>)> {
#[cfg(target_os = "windows")]
let b = { name.to_string_lossy().as_bytes() };
#[cfg(not(target_os = "windows"))]
let b = {
use std::os::unix::ffi::OsStrExt;
name.as_bytes()
};
let range = self.query.find(b).map(|m| m.range())?;
pub fn explode(&self, name: &[u8]) -> Option<(String, String, String)> {
let range = self.query.find(name).map(|m| m.range())?;
Some((
String::from_utf8_lossy(&b[..range.start]),
String::from_utf8_lossy(&b[range.start..range.end]),
String::from_utf8_lossy(&b[range.end..]),
String::from_utf8_lossy(&name[..range.start]).to_string(),
String::from_utf8_lossy(&name[range.start..range.end]).to_string(),
String::from_utf8_lossy(&name[range.end..]).to_string(),
))
}
}

View file

@ -172,6 +172,7 @@ pub fn max_common_root(files: &[impl AsRef<Path>]) -> PathBuf {
root
}
#[cfg(not(target_os = "windows"))]
#[test]
fn test_max_common_root() {
assert_eq!(max_common_root(&[] as &[PathBuf]).as_os_str(), "");