Function icon_for_file

Source
pub fn icon_for_file<'a, F>(file: F, theme: &Option<Theme>) -> FileIcon
where F: Into<File<'a>>,
Expand description

Get the icon for a file.

This function takes anything that might convert easily to a Path and an optional theme and returns a FileIcon struct.

The only real reason to use this function is if you want to specify a theme. If you don’t care about the theme, you can use the FileIcon::from trait implementation instead.

§Example

use devicons::{icon_for_file, FileIcon, Theme};
use std::path::Path;

let path = Path::new("Cargo.toml");
let icon = icon_for_file(path, &Some(Theme::Dark));
println!("Icon: {} {}", icon.icon, icon.color);
Examples found in repository?
examples/dark_theme.rs (line 9)
4fn main() {
5    // Create a new file instance for "example.txt"
6    let path = Path::new("example.txt");
7
8    // Get the icon for this file with the dark theme
9    let icon = icon_for_file(path, &Some(Theme::Dark));
10
11    // Print its corresponding icon
12    println!("Filename: {}", path.to_string_lossy());
13    println!("Icon: {}", icon.icon);
14    println!("Color: {}", icon.color);
15}
More examples
Hide additional examples
examples/default.rs (line 9)
4fn main() {
5    // Create a new file instance for a directory "my_folder/"
6    let path = Path::new("my_folder/");
7
8    // Get the icon for this directory with the default theme
9    let icon = icon_for_file(path, &None);
10
11    // Print its corresponding icon
12    println!("Filename: {}", path.to_string_lossy());
13    println!("Icon: {}", icon.icon);
14    println!("Color: {}", icon.color);
15}
examples/light_theme.rs (line 9)
4fn main() {
5    // Create a new file instance for a directory "my_folder/"
6    let path = Path::new("my_folder/");
7
8    // Get the icon for this directory with the light theme
9    let icon = icon_for_file(path, &Some(Theme::Light));
10
11    // Print its corresponding icon
12    println!("Filename: {}", path.to_string_lossy());
13    println!("Icon: {}", icon.icon);
14    println!("Color: {}", icon.color);
15}
examples/all.rs (line 7)
4fn main() {
5    // getting the icon from a path with a specified theme
6    let path = Path::new("Cargo.toml");
7    let icon_1 = icon_for_file(path, &Some(Theme::Dark));
8
9    // getting the icon from a string with a specified theme
10    let icon_2 = icon_for_file("Cargo.toml", &Some(Theme::Dark));
11
12    // getting the icon from a path with the default theme
13    let icon_3 = FileIcon::from(path);
14
15    // directly getting an icon from a filename
16    let icon_4 = FileIcon::from("Cargo.toml");
17
18    // from a PathBuf
19    let icon_5 = FileIcon::from(std::path::PathBuf::from("Cargo.toml"));
20
21    // from a String
22    let icon_6 = FileIcon::from("Cargo.toml".to_string());
23
24    println!("File: {}", path.to_string_lossy());
25    println!("Icon: {} {}", icon_1.icon, icon_1.color);
26    println!("Icon: {} {}", icon_2.icon, icon_2.color);
27    println!("Icon: {} {}", icon_3.icon, icon_3.color);
28    println!("Icon: {} {}", icon_4.icon, icon_4.color);
29    println!("Icon: {} {}", icon_5.icon, icon_5.color);
30    println!("Icon: {} {}", icon_6.icon, icon_6.color);
31}