ratatui_explorer/file_explorer/file.rs
1use std::{fs::FileType, path::PathBuf};
2
3/// A file or directory in the file explorer.
4#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5pub struct File {
6 /// The name of the file or directory.
7 ///
8 /// # Examples
9 /// Suppose you have this tree file, with `passport.png` selected inside `file_explorer`:
10 /// ```plaintext
11 /// /
12 /// ├── .git
13 /// └── Documents
14 /// ├── passport.png <- selected
15 /// └── resume.pdf
16 /// ```
17 /// You can get the name of the selected file like this:
18 /// ```no_run
19 /// use ratatui_explorer::FileExplorer;
20 ///
21 /// let file_explorer = FileExplorer::new().unwrap();
22 ///
23 /// /* user select `password.png` */
24 ///
25 /// let file = file_explorer.current();
26 /// assert_eq!(file.name, "passport.png");
27 /// ```
28 pub name: String,
29
30 /// Returns the path of the file or directory.
31 ///
32 /// # Examples
33 /// Suppose you have this tree file, with `passport.png` selected inside `file_explorer`:
34 /// ```plaintext
35 /// /
36 /// ├── .git
37 /// └── Documents
38 /// ├── passport.png <- selected
39 /// └── resume.pdf
40 /// ```
41 /// You can get the path of the selected file like this:
42 /// ```no_run
43 /// use ratatui_explorer::FileExplorer;
44 ///
45 /// let file_explorer = FileExplorer::new().unwrap();
46 ///
47 /// /* user select `password.png` */
48 ///
49 /// let file = file_explorer.current();
50 /// assert_eq!(file.path.display().to_string(), "/Documents/passport.png");
51 /// ```
52 pub path: PathBuf,
53
54 /// Is `true` is the file is a directory.
55 ///
56 /// # Examples
57 /// Suppose you have this tree file, with `passport.png` selected inside `file_explorer`:
58 /// ```plaintext
59 /// /
60 /// ├── .git
61 /// └── Documents
62 /// ├── passport.png <- selected
63 /// └── resume.pdf
64 /// ```
65 /// You can know if the selected file is a directory like this:
66 /// ```no_run
67 /// use ratatui_explorer::FileExplorer;
68 ///
69 /// let file_explorer = FileExplorer::new().unwrap();
70 ///
71 /// /* user select `password.png` */
72 ///
73 /// let file = file_explorer.current();
74 /// assert_eq!(file.is_dir, false);
75 ///
76 /// /* user select `Documents` */
77 ///
78 /// let file = file_explorer.current();
79 /// assert_eq!(file.is_dir, true);
80 /// ```
81 pub is_dir: bool,
82
83 /// Is `true` if the file or directory is hidden.
84 ///
85 /// # Examples
86 /// Suppose you have this tree file, with `passport.png` selected inside `file_explorer`:
87 /// ```plaintext
88 /// /
89 /// ├── .git
90 /// └── Documents
91 /// ├── passport.png <- selected
92 /// └── resume.pdf
93 /// ```
94 /// You can know if the selected file or directory is hidden like this:
95 /// ```no_run
96 /// use ratatui_explorer::FileExplorer;
97 ///
98 /// let mut file_explorer = FileExplorer::new().unwrap();
99 /// file_explorer.set_show_hidden(true);
100 ///
101 /// /* user select `password.png` */
102 ///
103 /// let file = file_explorer.current();
104 /// assert_eq!(file.is_hidden, false);
105 ///
106 /// /* user select `.git` */
107 ///
108 /// let file = file_explorer.current();
109 /// assert_eq!(file.is_hidden, true);
110 /// ```
111 pub is_hidden: bool,
112
113 /// The [`FileType`](https://doc.rust-lang.org/stable/std/fs/struct.FileType.html) of the file, when available.
114 ///
115 /// # Examples
116 /// Suppose you have this tree file, with `passport.png` selected inside `file_explorer`:
117 /// ```plaintext
118 /// /
119 /// ├── .git
120 /// └── Documents
121 /// ├── passport.png <- selected
122 /// └── resume.pdf
123 /// ```
124 /// You can know if the selected file is a directory like this:
125 /// ```no_run
126 /// use ratatui_explorer::FileExplorer;
127 ///
128 /// let file_explorer = FileExplorer::new().unwrap();
129 ///
130 /// /* user select `password.png` */
131 ///
132 /// let file = file_explorer.current();
133 /// assert_eq!(file.file_type.unwrap().is_dir(), false);
134 ///
135 /// /* user select `Documents` */
136 ///
137 /// let file = file_explorer.current();
138 /// assert_eq!(file.file_type.unwrap().is_dir(), true);
139 /// ```
140 pub file_type: Option<FileType>,
141}
142
143impl File {
144 /// Returns `true` is the file is a regular file.
145 ///
146 /// # Examples
147 /// Suppose you have this tree file, with `passport.png` selected inside `file_explorer`:
148 /// ```plaintext
149 /// /
150 /// ├── .git
151 /// └── Documents
152 /// ├── passport.png <- selected
153 /// └── resume.pdf
154 /// ```
155 /// You can know if the selected file is a directory like this:
156 /// ```no_run
157 /// use ratatui_explorer::FileExplorer;
158 ///
159 /// let file_explorer = FileExplorer::new().unwrap();
160 ///
161 /// /* user select `password.png` */
162 ///
163 /// let file = file_explorer.current();
164 /// assert_eq!(file.is_file(), true);
165 ///
166 /// /* user select `Documents` */
167 ///
168 /// let file = file_explorer.current();
169 /// assert_eq!(file.is_file(), false);
170 /// ```
171 #[inline]
172 #[must_use]
173 pub fn is_file(&self) -> bool {
174 self.file_type.is_some_and(|f| f.is_file())
175 }
176
177 #[allow(missing_docs)]
178 #[inline]
179 #[must_use]
180 #[deprecated(
181 since = "0.3.0",
182 note = "`name` field is public and should be acceded with `.name`"
183 )]
184 pub fn name(&self) -> &str {
185 &self.name
186 }
187
188 #[allow(missing_docs)]
189 #[inline]
190 #[must_use]
191 #[deprecated(
192 since = "0.3.0",
193 note = "`path` field is public and should be acceded with `.path`"
194 )]
195 pub const fn path(&self) -> &PathBuf {
196 &self.path
197 }
198
199 #[allow(missing_docs)]
200 #[inline]
201 #[must_use]
202 #[deprecated(
203 since = "0.3.0",
204 note = "`is_dir` field is public and should be acceded with `.is_dir`"
205 )]
206 pub const fn is_dir(&self) -> bool {
207 self.is_dir
208 }
209
210 #[allow(missing_docs)]
211 #[inline]
212 #[must_use]
213 #[deprecated(
214 since = "0.3.0",
215 note = "`is_hidden` field is public and should be acceded with `.is_hidden`"
216 )]
217 pub fn is_hidden(&self) -> bool {
218 self.is_hidden
219 }
220
221 #[allow(missing_docs)]
222 #[inline]
223 #[must_use]
224 #[deprecated(
225 since = "0.3.0",
226 note = "`file_type` field is public and should be acceded with `.file_type`"
227 )]
228 pub const fn file_type(&self) -> Option<FileType> {
229 self.file_type
230 }
231}