fif now outputs the dir it was run from

- refactor Format function signatures to be more uniform
- add Shell and Bash aliases for Sh
This commit is contained in:
Lynne Megido 2021-04-28 21:27:06 +10:00
parent 84ce0fc0ce
commit a05af12352
Signed by: lynnesbian
GPG Key ID: F0A184B5213D9F90
6 changed files with 41 additions and 27 deletions

View File

@ -1,8 +1,8 @@
# Changelog
Dates are given in YYYY-MM-DD format.
## v0.2
### v0.2.14 (2021-xx-yy)
## v0.3
### v0.3.0 (2021-xx-yy)
#### Features
- Added `-x`/`--exclude` flag for excluding file extensions (overrides `-e` or `-E` - `-E images -x jpg` scans all image
files, except ".jpg" files)
@ -12,11 +12,13 @@ Dates are given in YYYY-MM-DD format.
- `-e` and `-E` no longer conflict with each other, and can now be used together. For example, `-E images -e mp3`
will scan all images *and* all MP3 files
- It is now possible to specify multiple extension sets at once: `-E images,system` will scan all images and archives
- fif's output now includes the directory it was run from
#### Other
- Published my fork of ['mime_guess'] as ['new_mime_guess'], allowing it to be used properly with
[crates.io](https://crates.io)
- The `videos` extension set has been renamed to `video`, in line with `audio`. `fif --help` has actually mistakenly
referred to the set as `video` since v0.2.12! 0uo
- CI has been vastly improved
### v0.2.13 (2021-04-26)
#### Features

View File

@ -130,12 +130,12 @@ pub trait Format {
fn no_known_extension<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()>;
fn unreadable<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()>;
fn unknown_type<W: Write>(&self, f: &mut W, path: &Path) -> io::Result<()>;
fn header<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
fn footer<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()>;
fn header<W: Write>(&self, f: &mut W, entries: &Entries) -> io::Result<()>;
fn footer<W: Write>(&self, f: &mut W, entries: &Entries) -> io::Result<()>;
fn write_all<W: Write>(&self, entries: &Entries, f: &mut W) -> io::Result<()> {
fn write_all<W: Write>(&self, f: &mut W, entries: &Entries) -> io::Result<()> {
// TODO: clean this up - it's kinda messy
self.header(entries, f)?;
self.header(f, entries)?;
// output will be generated in the order:
// - files that couldn't be read
@ -170,7 +170,7 @@ pub trait Format {
}
}
self.footer(entries, f)
self.footer(f, entries)
}
}
@ -198,22 +198,20 @@ impl Format for Shell {
smart_write(f, writables!["# Failed to detect mime type for ", path, Newline])
}
fn header<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
fn header<W: Write>(&self, f: &mut W, _: &Entries) -> io::Result<()> {
smart_write(
f,
writables![
"#!/usr/bin/env sh",
Newline,
"# ",
(generated_by().as_str()),
Newline,
"set -e",
Newline
],
)
writables!["#!/usr/bin/env sh", Newline, "# ", (generated_by().as_str()), Newline],
)?;
if let Ok(working_directory) = std::env::current_dir() {
smart_write(f, writables!["# Run from ", (working_directory.as_path()), Newline])?;
}
smart_write(f, writables![Newline, "set -e", Newline, Newline])
}
fn footer<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
fn footer<W: Write>(&self, f: &mut W, _: &Entries) -> io::Result<()> {
smart_write(f, writables![Newline, "echo 'Done.'", Newline])
}
}
@ -272,7 +270,7 @@ impl Format for PowerShell {
)
}
fn header<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
fn header<W: Write>(&self, f: &mut W, _: &Entries) -> io::Result<()> {
smart_write(
f,
writables![
@ -283,10 +281,19 @@ impl Format for PowerShell {
" #>",
Newline
],
)
)?;
if let Ok(working_directory) = std::env::current_dir() {
smart_write(
f,
writables!["<# Run from ", (working_directory.as_path()), " #>", Newline],
)?;
}
smart_write(f, writables![Newline])
}
fn footer<W: Write>(&self, _: &Entries, f: &mut W) -> io::Result<()> {
fn footer<W: Write>(&self, f: &mut W, _: &Entries) -> io::Result<()> {
smart_write(f, writables![Newline, "Write-Output 'Done!'", Newline])
}
}

View File

@ -63,10 +63,10 @@ pub fn mime_type<T: MimeDb>(db: &T, path: &Path) -> io::Result<Option<Mime>> {
// TODO: avoid cloning mime if possible, although i don't really see how it would be - maybe instead of passing the mime
// object, pass a hash of it?
cached! {
MIMEXT;
fn mime_extension_lookup(mime: Mime) -> Option<Vec<String>> = {
// Returns a list of known extensions for this mime type, if any.
// ↑ this is supposed to be a doc comment, but the cached! macro doesn't support that... maybe i should switch to
// the derive macro

View File

@ -143,8 +143,8 @@ fn main() {
let mut buffered_stdout = BufWriter::new(stdout());
let result = match args.output_format {
OutputFormat::Sh => Shell::new().write_all(&results, &mut buffered_stdout),
OutputFormat::PowerShell => PowerShell::new().write_all(&results, &mut buffered_stdout),
OutputFormat::Sh => Shell::new().write_all(&mut buffered_stdout, &results),
OutputFormat::PowerShell => PowerShell::new().write_all(&mut buffered_stdout, &results),
OutputFormat::Text => todo!(),
};

View File

@ -17,6 +17,7 @@ cfg_if! {
#[derive(Clap, PartialEq, Debug)]
pub enum OutputFormat {
/// A Bourne shell compatible script.
#[clap(alias = "shell", alias = "bash")]
Sh,
/// A PowerShell script.
#[clap(alias = "powershell")]
@ -229,7 +230,11 @@ impl ExtensionSet {
"pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx", "csv", "tsv", "odt", "ods", "odp", "oda", "rtf", "ps",
"pages", "key", "numbers",
],
Self::Text => [mime_guess::get_mime_extensions_str("text/*").unwrap(), &["js", "pl", "csh", "sh", "bash", "zsh", "fish", "bat", "php"]].concat(),
Self::Text => [
mime_guess::get_mime_extensions_str("text/*").unwrap(),
&["js", "pl", "csh", "sh", "bash", "zsh", "fish", "bat", "php"],
]
.concat(),
// many compressed file types follow the name scheme "application/x.+compressed.*" - maybe this can be used
// somehow to extract extensions for compressed files from mime_guess?
Self::Archives => vec![

View File

@ -339,7 +339,7 @@ fn outputs_move_commands() {
let mut contents = std::string::String::new();
Shell::new()
.write_all(&entries, &mut cursor)
.write_all(&mut cursor, &entries)
.expect("Failed to write to cursor");
cursor.set_position(0);
cursor