Skip to content

Commit 26cdd52

Browse files
refactor: make interface more user friendly
1 parent 2fc728d commit 26cdd52

File tree

7 files changed

+56
-41
lines changed

7 files changed

+56
-41
lines changed

Cargo.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
2-
name = "rust-devicons"
3-
version = "0.2.3"
2+
name = "devicons"
3+
version = "0.3.3"
44
edition = "2021"
55
authors = ["Alexandre Pasmantier <alex.pasmant@gmail.com>"]
66
license = "Unlicense"

README.md

+8-9
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ A dead-simple and efficient Rust library inspired by [vim-devicons](https://gith
1616

1717
## Installation
1818

19-
Add this to your `Cargo.toml`:
20-
21-
```toml
22-
[dependencies]
23-
devicons = "0.1.0"
19+
```sh
20+
cargo add devicons
2421
```
2522

2623
_**NOTE**: you'll need to use a [NerdFont](https://www.nerdfonts.com/) to properly display the icons._
@@ -31,15 +28,17 @@ Here’s a simple example of how to use `devicons` to retrieve a file icon with
3128

3229
```rust
3330
use std::path::Path;
34-
use your_library::{File, Theme, icon_for_file};
31+
use devicons::{File, Theme, icon_for_file};
3532

3633
fn main() {
34+
// getting the icon from a path
3735
let path = Path::new("example.txt");
38-
let file = File::new(path);
36+
let icon = icon_for_file(path, Some(Theme::Dark));
3937

40-
let icon = icon_for_file(&file, Some(Theme::Dark));
38+
// getting the icon from a str
39+
let icon = icon_for_file("example.txt", Some(Theme::Dark));
4140

42-
println!("File: {}", file.name);
41+
println!("File: {}", path.to_string_lossy());
4342
println!("Icon: {}", icon.icon);
4443
println!("Color: {}", icon.color);
4544
}

examples/dark_theme.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use devicons::{icon_for_file, File, Theme};
1+
use devicons::{icon_for_file, Theme};
22
use std::path::Path;
33

44
fn main() {
55
// Create a new file instance for "example.txt"
66
let path = Path::new("example.txt");
7-
let file = File::new(path);
87

98
// Get the icon for this file with the dark theme
10-
let icon = icon_for_file(&file, Some(Theme::Dark));
9+
let icon = icon_for_file(path, Some(Theme::Dark));
1110

1211
// Print its corresponding icon
13-
println!("Filename: {}", file.name);
12+
println!("Filename: {}", path.to_string_lossy());
1413
println!("Icon: {}", icon.icon);
1514
println!("Color: {}", icon.color);
1615
}

examples/default.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use devicons::{icon_for_file, File};
1+
use devicons::icon_for_file;
22
use std::path::Path;
33

44
fn main() {
55
// Create a new file instance for a directory "my_folder/"
66
let path = Path::new("my_folder/");
7-
let file = File::new(path);
87

98
// Get the icon for this directory with the default theme
10-
let icon = icon_for_file(&file, None);
9+
let icon = icon_for_file(path, None);
1110

1211
// Print its corresponding icon
13-
println!("Filename: {}", file.name);
12+
println!("Filename: {}", path.to_string_lossy());
1413
println!("Icon: {}", icon.icon);
1514
println!("Color: {}", icon.color);
1615
}

examples/light_theme.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
use devicons::{icon_for_file, File, Theme};
1+
use devicons::{icon_for_file, Theme};
22
use std::path::Path;
33

44
fn main() {
55
// Create a new file instance for a directory "my_folder/"
66
let path = Path::new("my_folder/");
7-
let file = File::new(path);
87

98
// Get the icon for this directory with the light theme
10-
let icon = icon_for_file(&file, Some(Theme::Light));
9+
let icon = icon_for_file(path, Some(Theme::Light));
1110

1211
// Print its corresponding icon
13-
println!("Filename: {}", file.name);
12+
println!("Filename: {}", path.to_string_lossy());
1413
println!("Icon: {}", icon.icon);
1514
println!("Color: {}", icon.color);
1615
}

src/icons.rs

+30-11
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ pub struct File<'a> {
1313
}
1414

1515
impl File<'_> {
16-
pub fn new<'a>(path: &'a Path) -> File<'a> {
17-
let path = path;
16+
pub fn new<'a, P>(path: P) -> File<'a>
17+
where
18+
P: Into<&'a Path>,
19+
{
20+
let path: &Path = path.into();
1821
let name = path.file_name().unwrap().to_str().unwrap();
1922
let ext = Self::ext(path);
2023

@@ -35,6 +38,19 @@ impl File<'_> {
3538
}
3639
}
3740

41+
impl<'a> From<&'a Path> for File<'a> {
42+
fn from(path: &'a Path) -> File<'a> {
43+
File::new(path)
44+
}
45+
}
46+
47+
impl<'a> From<&'a str> for File<'a> {
48+
fn from(path: &'a str) -> File<'a> {
49+
let path = Path::new(path); // Convert &str to &Path
50+
File::new(path)
51+
}
52+
}
53+
3854
pub enum Theme {
3955
Light,
4056
Dark,
@@ -68,11 +84,15 @@ const DEFAULT_DIR_ICON: FileIcon = FileIcon {
6884
color: "#7e8e91",
6985
};
7086

71-
pub fn icon_for_file(file: &File<'_>, theme: Option<Theme>) -> FileIcon {
87+
pub fn icon_for_file<'a, F>(file: F, theme: Option<Theme>) -> FileIcon
88+
where
89+
F: Into<File<'a>>,
90+
{
91+
let file = file.into();
7292
match theme {
73-
Some(Theme::Light) => light_icon_for_file(file),
74-
Some(Theme::Dark) => dark_icon_for_file(file),
75-
None => dark_icon_for_file(file),
93+
Some(Theme::Light) => light_icon_for_file(&file),
94+
Some(Theme::Dark) => dark_icon_for_file(&file),
95+
None => dark_icon_for_file(&file),
7696
}
7797
}
7898

@@ -162,9 +182,8 @@ mod tests {
162182
#[test]
163183
fn test_icon_for_file_with_light_theme() {
164184
let path = Path::new("file.txt");
165-
let file = File::new(path);
166185

167-
let icon = icon_for_file(&file, Some(Theme::Light));
186+
let icon = icon_for_file(path, Some(Theme::Light));
168187
assert_eq!(icon.icon, '󰈙');
169188
assert_eq!(icon.color, "#447028");
170189
}
@@ -174,7 +193,7 @@ mod tests {
174193
let path = Path::new("file.txt");
175194
let file = File::new(path);
176195

177-
let icon = icon_for_file(&file, Some(Theme::Dark));
196+
let icon = icon_for_file(file, Some(Theme::Dark));
178197
assert_eq!(icon.icon, '󰈙');
179198
assert_eq!(icon.color, "#89e051");
180199
}
@@ -184,7 +203,7 @@ mod tests {
184203
let path = Path::new("some_directory/");
185204
let file = File::new(path);
186205

187-
let icon = icon_for_file(&file, Some(Theme::Dark));
206+
let icon = icon_for_file(file, Some(Theme::Dark));
188207
assert_eq!(icon.icon, '\u{f115}'); // Default directory icon
189208
assert_eq!(icon.color, "#7e8e91");
190209
}
@@ -194,7 +213,7 @@ mod tests {
194213
let path = Path::new("file.txt");
195214
let file = File::new(path);
196215

197-
let icon = icon_for_file(&file, None); // Should default to Dark theme
216+
let icon = icon_for_file(file, None); // Should default to Dark theme
198217
assert_eq!(icon.icon, '󰈙');
199218
assert_eq!(icon.color, "#89e051");
200219
}

0 commit comments

Comments
 (0)