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 fix #45

Merged
merged 4 commits into from
May 14, 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
8 changes: 4 additions & 4 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 in quotes. For example:-
Riv on the other hand runs from the command line, and accepts a glob. For example:-

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

## Manual

Start riv with

```$ riv```.

As an optional second parameter you can add a glob in quotes.
As an optional second parameter you can add a glob, space separated filepaths or even a single filepath.

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

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

Expand Down
62 changes: 29 additions & 33 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! The cli module is used for setting up the command line app and parsing the arguments.

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 @@ -12,8 +11,6 @@ pub struct Args {
pub files: Vec<PathBuf>,
/// dest_folder is the supplied or default folder for moving files
pub dest_folder: PathBuf,
/// glob is the glob used in the image search
pub search: String,
}

/// cli sets up the command line app and parses the arguments, using clap.
Expand All @@ -23,10 +20,9 @@ pub fn cli() -> Result<Args, String> {
.version("0.2.0")
.about("The command line image viewer")
.arg(
Arg::with_name("path")
.required(true)
.default_value("*")
.help("The directory or files to search for image files"),
Arg::with_name("paths")
.multiple(true)
.help("The directory or files to search for image files. A glob can be used here."),
)
.arg(
Arg::with_name("dest-folder")
Expand All @@ -37,39 +33,39 @@ pub fn cli() -> Result<Args, String> {
.takes_value(true),
)
.get_matches();
let glob_value = match matches.value_of("path") {
Some(v) => v,
None => return Err("Failed to determine glob value".to_string()),
};
let glob_matches = glob(glob_value).map_err(|e| e.to_string())?;
for path in glob_matches {
match path {
Ok(p) => {
if let Some(ext) = p.extension() {
if let Some(ext_str) = ext.to_str() {
let low = ext_str.to_string().to_lowercase();
if low == "jpg"
|| low == "jpeg"
|| low == "png"
|| low == "bmp"
|| low == "webp"
{
files.push(p)
}
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),
}
}
Err(e) => eprintln!("{}", e),
}
}

let dest_folder = match matches.value_of("dest-folder") {
Some(f) => PathBuf::from(f),
None => return Err("failed to determine destintation folder".to_string()),
};
let search = glob_value.to_owned();
Ok(Args {
files,
dest_folder,
search,
})
Ok(Args { files, dest_folder })
}

fn push_image_path(v: &mut Vec<PathBuf>, p: PathBuf) {
if let Some(ext) = p.extension() {
if let Some(ext_str) = ext.to_str() {
let low = ext_str.to_string().to_lowercase();
if low == "jpg" || low == "jpeg" || low == "png" || low == "bmp" || low == "webp" {
v.push(p)
}
}
}
}
9 changes: 0 additions & 9 deletions src/infobar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use crate::paths::Paths;
pub struct Text {
/// current image is the string of the current image path
pub current_image: String,
/// context is the string of the current directory + the glob from where the program was launched
pub context: String,
/// index is the string represention of the index.
pub index: String,
}
Expand All @@ -23,20 +21,13 @@ impl From<&Paths> for Text {
},
None => "No file selected".to_string(),
};
let current_dir = match p.current_dir.to_str() {
Some(dir) => dir.to_string(),
None => "Unknown directory".to_string(),
};
let glob = p.glob.clone();
let context = current_dir + "/" + &glob;
let index = if p.images.is_empty() {
"No files in path".to_string()
} else {
format!("{} of {}", p.index + 1, p.images.len())
};
Text {
current_image,
context,
index,
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ pub struct Paths {
pub dest_folder: PathBuf,
/// current_dir is the path of the current directory where the program was launched from
pub current_dir: PathBuf,
/// glob is the glob used in the path search. Default is "*".
pub glob: String,
/// index is the index of the images vector of the current image to be displayed.
pub index: usize,
}
2 changes: 0 additions & 2 deletions src/program/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ impl<'a> Program<'a> {
let args = cli::cli()?;
let images = args.files;
let dest_folder = args.dest_folder;
let glob = args.search;

let current_dir = match std::env::current_dir() {
Ok(c) => c,
Expand Down Expand Up @@ -74,7 +73,6 @@ impl<'a> Program<'a> {
images,
dest_folder,
index: 0,
glob,
current_dir,
},
ui_state: ui::State {
Expand Down
44 changes: 4 additions & 40 deletions src/program/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,46 +95,20 @@ impl<'a> Program<'a> {
.create_texture_from_surface(&index_surface)
.map_err(|e| e.to_string())?;
let index_dimensions = index_texture.query();
// Load the context texture
let context_surface = self
.screen
.font
.render(&text.context)
.blended(text_color())
.map_err(|e| e.to_string())?;
let context_texture = self
.screen
.texture_creator
.create_texture_from_surface(&context_surface)
.map_err(|e| e.to_string())?;
let context_dimensions = context_texture.query();
// Draw the Bar
let dims = (
index_dimensions.height,
context_dimensions.width,
index_dimensions.width,
filename_dimensions.width,
);
self.render_bar(dims)?;
// Copy the text textures
let y = (self.screen.canvas.viewport().height() - index_dimensions.height) as i32;
if let Err(e) = self.screen.canvas.copy(
&context_texture,
None,
Rect::new(
PADDING,
y,
context_dimensions.width,
context_dimensions.height,
),
) {
eprintln!("Failed to copy text to screen {}", e);
}
if let Err(e) = self.screen.canvas.copy(
&index_texture,
None,
Rect::new(
(context_dimensions.width + PADDING as u32 * 2) as i32,
PADDING as i32,
y,
index_dimensions.width,
index_dimensions.height,
Expand All @@ -146,7 +120,7 @@ impl<'a> Program<'a> {
&filename_texture,
None,
Rect::new(
(context_dimensions.width + index_dimensions.width + PADDING as u32 * 3) as i32,
(index_dimensions.width + PADDING as u32 * 2) as i32,
y,
filename_dimensions.width,
filename_dimensions.height,
Expand All @@ -158,7 +132,7 @@ impl<'a> Program<'a> {
Ok(())
}

fn render_bar(&mut self, dims: (u32, u32, u32, u32)) -> Result<(), String> {
fn render_bar(&mut self, dims: (u32, u32, u32)) -> Result<(), String> {
let height = dims.0;
let width = self.screen.canvas.viewport().width();
let y = (self.screen.canvas.viewport().height() - height) as i32;
Expand All @@ -169,18 +143,12 @@ impl<'a> Program<'a> {
eprintln!("Failed to draw bar {}", e);
}
x += w as i32;
w = dims.2 + PADDING as u32;
w = dims.2 + PADDING as u32 * 2;
self.screen.canvas.set_draw_color(blue());
if let Err(e) = self.screen.canvas.fill_rect(Rect::new(x, y, w, height)) {
eprintln!("Failed to draw bar {}", e);
}
x += w as i32;
w = dims.3 + PADDING as u32;
self.screen.canvas.set_draw_color(dark_blue());
if let Err(e) = self.screen.canvas.fill_rect(Rect::new(x, y, w, height)) {
eprintln!("Failed to draw bar {}", e);
}
x += w as i32;
w = width;
self.screen.canvas.set_draw_color(grey());
if let Err(e) = self.screen.canvas.fill_rect(Rect::new(x, y, w, height)) {
Expand Down Expand Up @@ -215,10 +183,6 @@ fn blue() -> Color {
Color::RGB(0, 180, 204)
}

fn dark_blue() -> Color {
Color::RGB(0, 140, 158)
}

fn grey() -> Color {
Color::RGB(52, 56, 56)
}