ftag 0.8.4

CLI tool for tagging and searching files. See README.md for more info.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
use egui::text::{CCursor, CCursorRange};
use ftag::{
    core::Error,
    interactive::{InteractiveSession, State},
    open,
    query::TagTable,
};
use std::{
    fmt::Debug,
    ops::Add,
    path::{Path, PathBuf},
    str::FromStr,
};

enum GuiError {
    Core(Error),
    Gui(eframe::Error),
}

impl From<Error> for GuiError {
    fn from(value: Error) -> Self {
        GuiError::Core(value)
    }
}

impl From<eframe::Error> for GuiError {
    fn from(value: eframe::Error) -> Self {
        GuiError::Gui(value)
    }
}

impl Debug for GuiError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Core(inner) => write!(f, "core ERROR: {:?}", inner),
            Self::Gui(inner) => write!(f, "eframe ERROR: {:?}", inner),
        }
    }
}

fn main() -> Result<(), GuiError> {
    let working_dir =
        match std::env::args()
            .skip(1)
            .fold((false, None), |(read_path, path), arg| {
                match (read_path, path, arg.as_str()) {
                    (false, None, "--path" | "-p") => (true, None),
                    (true, None, val) => (
                        false,
                        Some(
                            PathBuf::from_str(val)
                                .expect("ERROR: Cannot parse path argument.")
                                .canonicalize()
                                .expect("ERROR: Unable to parse path argument."),
                        ),
                    ),
                    (_, _, val) => {
                        eprintln!("ERROR: Unexpected argument: {}", val);
                        panic!("ABORTED.");
                    }
                }
            }) {
            (_, None) => std::env::current_dir().map_err(|_| Error::InvalidWorkingDirectory)?,
            (_, Some(path)) => path,
        };
    let table = TagTable::from_dir(working_dir)?;
    let options = eframe::NativeOptions {
        viewport: egui::ViewportBuilder::default()
            .with_maximized(true)
            .with_clamp_size_to_monitor_size(true),
        ..Default::default()
    };
    eframe::run_native(
        "ftagui",
        options,
        Box::new(|cc| {
            let ctx = &cc.egui_ctx;
            ctx.set_pixels_per_point(1.2);
            egui_extras::install_image_loaders(ctx);
            Ok(Box::from(GuiApp {
                session: InteractiveSession::init(table),
                page_index: 0,
                last_page: 0,
            }))
        }),
    )
    .map_err(GuiError::Gui)
}

struct GuiApp {
    session: InteractiveSession,
    page_index: usize,
    last_page: usize,
}

const DESIRED_ROW_HEIGHT: f32 = 200.;
const DESIRED_COL_WIDTH: f32 = 200.;
const ICON_MAX_HEIGHT: f32 = DESIRED_ROW_HEIGHT * 0.5;
const ICON_MAX_WIDTH: f32 = DESIRED_COL_WIDTH * 0.5;
const ROW_SPACING: f32 = 5.;
const COL_SPACING: f32 = 5.;

impl GuiApp {
    fn render_file_preview(relpath: &str, abspath: &Path, ui: &mut egui::Ui) -> egui::Response {
        enum FileType {
            Image,
            PdfDocument,
            Video,
            Other,
        }
        let ftype = match abspath.extension() {
            Some(ext) => match ext.to_ascii_lowercase().to_str() {
                Some(ext) => match ext {
                    "png" | "jpg" | "jpeg" | "bmp" | "webp" => FileType::Image,
                    "pdf" => FileType::PdfDocument,
                    "mov" | "flv" | "mp4" | "3gp" | "mpg" => FileType::Video,
                    _ => FileType::Other,
                },
                None => FileType::Other,
            },
            None => FileType::Other,
        };
        match ftype {
            FileType::Image => ui.add(
                egui::Image::from_uri(format!("file://{}", abspath.display()))
                    .show_loading_spinner(true)
                    .corner_radius(10.)
                    .maintain_aspect_ratio(true)
                    .sense(egui::Sense::click().union(egui::Sense::hover())),
            ),
            FileType::PdfDocument => {
                let response = ui.add(
                    egui::Image::from(egui::include_image!("assets/icon_pdf.svg"))
                        .show_loading_spinner(true)
                        .maintain_aspect_ratio(true)
                        .sense(egui::Sense::click().union(egui::Sense::hover()))
                        .max_height(ICON_MAX_HEIGHT)
                        .max_width(ICON_MAX_WIDTH),
                );
                ui.add(
                    egui::Label::new(
                        egui::RichText::new(relpath).text_style(egui::TextStyle::Monospace),
                    )
                    .selectable(false),
                );
                response
            }
            FileType::Video => {
                let response = ui.add(
                    egui::Image::from(egui::include_image!("assets/icon_video.svg"))
                        .show_loading_spinner(true)
                        .maintain_aspect_ratio(true)
                        .sense(egui::Sense::click().union(egui::Sense::hover()))
                        .max_height(ICON_MAX_HEIGHT)
                        .max_width(ICON_MAX_WIDTH),
                );
                ui.add(
                    egui::Label::new(
                        egui::RichText::new(relpath).text_style(egui::TextStyle::Monospace),
                    )
                    .selectable(false),
                );
                response
            }
            FileType::Other => {
                let response = ui.add(
                    egui::Image::from(egui::include_image!("assets/icon_file.svg"))
                        .show_loading_spinner(true)
                        .maintain_aspect_ratio(true)
                        .sense(egui::Sense::click().union(egui::Sense::hover()))
                        .max_height(ICON_MAX_HEIGHT)
                        .max_width(ICON_MAX_WIDTH),
                );
                ui.add(
                    egui::Label::new(
                        egui::RichText::new(relpath).text_style(egui::TextStyle::Monospace),
                    )
                    .selectable(false),
                );
                response
            }
        }
    }

    fn render_grid_preview(&mut self, ui: &mut egui::Ui) {
        let (ncols, ncells, row_height, col_width) = {
            let ncols = f32::ceil(ui.available_width() / (DESIRED_COL_WIDTH + COL_SPACING));
            let nrows = f32::ceil(ui.available_height() / (DESIRED_ROW_HEIGHT + ROW_SPACING));
            let row_height = (ui.available_height() / nrows) - ROW_SPACING;
            let col_width = (ui.available_width() / ncols) - COL_SPACING;
            (
                ncols as usize,
                ncols as usize * nrows as usize,
                row_height,
                col_width,
            )
        };
        // This takes the ceil of integer division.
        self.last_page = self.session.filelist().len() / ncells;
        let mut echo = None;
        egui::Grid::new("image_grid")
            .min_row_height(row_height)
            .max_col_width(col_width)
            .striped(true)
            .spacing(egui::Vec2::new(COL_SPACING, ROW_SPACING))
            .show(ui, |ui| {
                for (counter, (relpath, path)) in self
                    .session
                    .filelist()
                    .iter()
                    .map(|file| {
                        let mut path = self.session.table().path().to_path_buf();
                        path.push(file);
                        (file, path)
                    })
                    .skip(self.page_index * ncells)
                    .take(ncells)
                    .enumerate()
                {
                    ui.vertical_centered(|ui| {
                        let response = Self::render_file_preview(relpath, &path, ui);
                        if response.double_clicked() && open::open(&path).is_err() {
                            echo = Some("Unable to open the file.");
                        } else if response.hovered() {
                            response.show_tooltip_ui(|ui| {
                                ui.monospace(ftag::core::what_is(&path).unwrap_or(String::from(
                                    "Unable to fetch the description of this file.",
                                )));
                            });
                        }
                    });
                    if counter % ncols == ncols - 1 {
                        ui.end_row();
                    }
                }
                if let Some(message) = echo {
                    self.session.set_echo(message);
                }
            });
    }

    fn invert_color(color: &egui::Color32) -> egui::Color32 {
        egui::Color32::from_rgb(
            u8::MAX - color.r(),
            u8::MAX - color.g(),
            u8::MAX - color.b(),
        )
    }

    fn parse_suggestion_string(&self) -> Option<(&str, &str, &str)> {
        let (left, rest) = self.session.echo().split_once('[')?;
        let (middle, right) = rest.split_once(']')?;
        Some((left, middle, right))
    }

    fn render_echo(&self, ui: &mut egui::Ui) {
        let tryparse = if let State::Autocomplete = self.session.state() {
            self.parse_suggestion_string()
        } else {
            None
        };
        match tryparse {
            Some((left, selection, right)) => {
                let left = egui::Label::new(
                    egui::widget_text::RichText::new(left).text_style(egui::TextStyle::Monospace),
                )
                .selectable(false)
                .wrap_mode(egui::TextWrapMode::Truncate);
                let selection = egui::Label::new(
                    egui::widget_text::RichText::new(selection)
                        .text_style(egui::TextStyle::Monospace)
                        .color(Self::invert_color(&ui.visuals().text_color()))
                        .strong()
                        .background_color(Self::invert_color(&ui.visuals().faint_bg_color)),
                )
                .selectable(false)
                .wrap_mode(egui::TextWrapMode::Truncate);
                let right = egui::Label::new(
                    egui::widget_text::RichText::new(right).text_style(egui::TextStyle::Monospace),
                )
                .selectable(false)
                .wrap_mode(egui::TextWrapMode::Truncate);
                ui.add(left);
                ui.add(selection);
                ui.add(right);
            }
            None => {
                ui.add(
                    egui::Label::new(
                        egui::widget_text::RichText::new(self.session.echo())
                            .text_style(egui::TextStyle::Monospace),
                    )
                    .selectable(false)
                    .wrap_mode(egui::TextWrapMode::Truncate),
                );
            }
        }
    }
}

impl eframe::App for GuiApp {
    fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
        // Tags panel.
        egui::SidePanel::left("tags_panel").show(ctx, |ui| {
            egui::ScrollArea::vertical().show(ui, |ui| {
                for tag in self.session.taglist() {
                    ui.add(
                        egui::Label::new(
                            egui::widget_text::RichText::new(tag)
                                .text_style(egui::TextStyle::Monospace),
                        )
                        .selectable(false),
                    );
                }
            });
        });
        // Current filter string.
        egui::TopBottomPanel::top("top_bar").show(ctx, |ui| {
            ui.centered_and_justified(|ui| {
                ui.add(
                    egui::Label::new(
                        egui::widget_text::RichText::new(format!(
                            "{}: {} results, page {} of {}",
                            if self.session.filter_str().is_empty() {
                                "ALL_TAGS"
                            } else {
                                self.session.filter_str()
                            },
                            self.session.filelist().len(),
                            self.page_index + 1,
                            self.last_page + 1,
                        ))
                        .text_style(egui::TextStyle::Monospace),
                    )
                    .selectable(false),
                );
            });
        });
        // Input field and echo string.
        egui::TopBottomPanel::bottom("bottom_panel").show(ctx, |ui| {
            ui.vertical_centered(|ui| {
                ui.horizontal(|ui| {
                    self.render_echo(ui);
                });
                ui.separator();
                let mut output = egui::TextEdit::singleline(self.session.command_mut())
                    .frame(false)
                    .desired_width(f32::INFINITY)
                    .min_size(egui::Vec2::new(100., 24.))
                    .font(egui::FontId::monospace(14.))
                    .horizontal_align(egui::Align::Center)
                    .vertical_align(egui::Align::Center)
                    .hint_text("command:")
                    .show(ui);
                let query_response = output.response;
                if query_response.lost_focus() {
                    if ui.input(|i| i.key_pressed(egui::Key::Enter)) {
                        // User hit return with a query.
                        self.session.process_input();
                        match self.session.state() {
                            State::Default | State::Autocomplete => {} // Do nothing.
                            State::ListsUpdated => {
                                self.page_index = 0;
                                self.session.set_state(State::Default);
                            }
                            State::Exit => {
                                ctx.send_viewport_cmd(egui::ViewportCommand::Close);
                            }
                        }
                        // Move the cursor to the end of the line, say, after autocomplete.
                        output.state.cursor.set_char_range(Some(CCursorRange::two(
                            CCursor::new(self.session.command().len()),
                            CCursor::new(self.session.command().len()),
                        )));
                        output.state.store(ctx, query_response.id);
                    } else if ui.input(|i| i.key_pressed(egui::Key::Tab)) {
                        self.session.autocomplete();
                    }
                } else if query_response.changed() {
                    self.session.stop_autocomplete();
                } else if ui.input_mut(|i| i.consume_key(egui::Modifiers::CTRL, egui::Key::N)) {
                    self.page_index = self.page_index.add(1).min(self.last_page);
                } else if ui.input_mut(|i| i.consume_key(egui::Modifiers::CTRL, egui::Key::P)) {
                    self.page_index = self.page_index.saturating_sub(1);
                }
                query_response.request_focus();
            });
        });
        // Files previews.
        egui::CentralPanel::default().show(ctx, |ui| {
            /*
             * Sometimes I've found that egui starts rendering images before
             * it's ready with the layout of the frame. This causes the file
             * grid calculations to go haywire, and try to load a large number
             * of images from the disk, because the reported available space by
             * the layout of the ui is too large.
             *
             * I've found that checking if the CPU usage is initialized avoids
             * this problem. There is a correlation between the CPU usage being
             * initialized, and the UI layout being initialized.
             */
            match frame.info().cpu_usage {
                Some(_) => {
                    self.render_grid_preview(ui);
                }
                None => {
                    ui.add(
                        egui::Label::new("Loading...")
                            .halign(egui::Align::Center)
                            .selectable(false),
                    );
                }
            }
        });
    }
}