Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Glob in quotes #64

Merged
merged 6 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 23 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ Generally the options are:-
* Preview - Clunky and really only good for viewing one image at a time
* Others that require a GUI folder browser

Riv on the other hand runs from the command line, and accepts a glob. For example:-
Riv on the other hand runs from the command line, and accepts a path with globs in quotes (with globstar `**` for recursive search). For example:-

```$ riv **/*.jpg```
```$ riv "**/*.jpg"```

## Manual

Start riv with

```$ riv```.

As an optional second parameter you can add a glob, space separated filepaths or even a single filepath.
As an optional second parameter you can add a path with globs.

```$ riv **/*.png```
```$ riv "**/*.png"```

Without any second parameter, riv will look for all images in the current directory.

Expand All @@ -29,25 +29,7 @@ Set a destination folder for moving files with the `f` flag. The folder will be

Set a sorting order with the `s` or `--sort` flag, case insensitive.

```$ riv -s alphabetical **/*.png```

### Sorting Options

| Options | Description |
|------------------|------------------------------------------------------------------------------------------|
| Alphabetical | Alphabetically by filename only |
| Date | By date last modified, most recent first |
| Size | By size, largest first |
| DepthFirst | [Default] Ordered by farthest depth from current directory first |
| BreadthFirst | Ordered by farthest depth from current directory last |

Reverse the sorting order with `r` or `--reverse` flag

```$ riv -sr date **/*.png```

Set the maximum number of images to be displayed `m` or `--max` flag. 0 means infinitely many images.

```$ riv -m 0 **/*.png```
```$ riv -s alphabetical "**/*.png"```

### Normal Mode Controls

Expand Down Expand Up @@ -82,6 +64,24 @@ Set the maximum number of images to be displayed `m` or `--max` flag. 0 means in
| df OR destfolder [path] | **Required argument** new folder to move/copy images to |
| m OR max [positive integer] | **Required argument** new maximum number of files to view|

### Sorting Options

| Options | Description |
|------------------|------------------------------------------------------------------------------------------|
| Alphabetical | Alphabetically by filename only |
| Date | By date last modified, most recent first |
| Size | By size, largest first |
| DepthFirst | [Default] Ordered by farthest depth from current directory first |
| BreadthFirst | Ordered by farthest depth from current directory last |

Reverse the sorting order with `r` or `--reverse` flag

```$ riv -sr date **/*.png```

Set the maximum number of images to be displayed `m` or `--max` flag. 0 means infinitely many images.

```$ riv -m 0 **/*.png```


## Getting Started

Expand Down
30 changes: 13 additions & 17 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use crate::sort::SortOrder;
use clap::{App, Arg};
use glob::glob;
use std::path::PathBuf;

/// Args contains the arguments that have been successfully parsed by the clap cli app
Expand All @@ -30,7 +31,7 @@ pub fn cli() -> Result<Args, String> {
.about("The command line image viewer")
.arg(
Arg::with_name("paths")
.multiple(true)
.default_value("*")
.help("The directory or files to search for image files. A glob can be used here."),
)
.arg(
Expand Down Expand Up @@ -77,22 +78,17 @@ pub fn cli() -> Result<Args, String> {
.help("Start app in fullscreen mode"),
)
.get_matches();
match matches.values_of("paths") {
Some(path_matches) => {
for path in path_matches {
push_image_path(&mut files, PathBuf::from(path));
}
}
None => {
let path_matches = glob::glob("*").map_err(|e| e.to_string())?;
for path in path_matches {
match path {
Ok(p) => {
push_image_path(&mut files, p);
}
Err(e) => eprintln!("Unexpected path {}", e),
}
}

let path_glob = match matches.value_of("paths") {
Some(v) => v,
None => panic!("No value for paths!"),
};
let path_glob = crate::convert_to_globable(path_glob)?;
let glob_matches = glob(&path_glob).map_err(|e| e.to_string())?;
for path in glob_matches {
match path {
Ok(p) => push_image_path(&mut files, p),
Err(e) => eprintln!("Path not processable {}", e),
}
}

Expand Down
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,20 @@ pub mod program;
pub mod screen;
pub mod sort;
pub mod ui;

use shellexpand::full;
use std::path::PathBuf;

/// Converts the provided path by user to a path that can be glob'd
/// Directories are changed from /home/etc to /home/etc/*
pub fn convert_to_globable(path: &str) -> Result<String, String> {
let expanded_path = full(path).map_err(|e| format!("\"{}\": {}", e.var_name, e.cause))?;
// remove escaped spaces
let absolute_path = String::from(expanded_path).replace(r"\ ", " ");
// If path is a dir, add /* to glob
let mut pathbuf = PathBuf::from(&absolute_path);
if pathbuf.is_dir() {
pathbuf = pathbuf.join("*");
}
Ok(pathbuf.to_string_lossy().to_string())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

      Ok(pathbuf.to_string_lossy().to_string())

Would this mean that we only handle valid UTF-8 paths?
Seems that glob can't accept invalid utf-8 as a pattern anyway.

Because the following path would not work.

python3

invalid_utf8_file = b"\xc3\x28"
with open(invalid_utf8_file, "w+") as f:
    f.write("invalid\n")

}
16 changes: 1 addition & 15 deletions src/program/command_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,13 @@ impl FromStr for Commands {
}
}

/// Converts the provided path by user to a path that can be glob'd
/// Directories are changed from /home/etc to /home/etc/*
fn convert_path_to_globable(path: &str) -> Result<String, String> {
let expanded_path = full(path).map_err(|e| format!("\"{}\": {}", e.var_name, e.cause))?;
// remove escaped spaces
let absolute_path = String::from(expanded_path).replace(r"\ ", " ");
// If path is a dir, add /* to glob
let mut pathbuf = PathBuf::from(&absolute_path);
if pathbuf.is_dir() {
pathbuf = pathbuf.join("*");
}
Ok(pathbuf.to_string_lossy().to_string())
}

/// Globs the passed path, returning an error if no images are in that path, glob::glob fails, or
/// path is unexpected
fn glob_path(path: &str) -> Result<Vec<PathBuf>, String> {
use crate::cli::push_image_path;

let mut new_images: Vec<PathBuf> = Vec::new();
let globable_path = convert_path_to_globable(path)?;
let globable_path = crate::convert_to_globable(path)?;
let path_matches = glob::glob(&globable_path).map_err(|e| e.to_string())?;
for path in path_matches {
match path {
Expand Down