Skip to content

Commit

Permalink
v0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
ksk001100 committed Mar 24, 2022
1 parent ddad36d commit 71feae8
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "picterm"
version = "0.0.5"
version = "0.0.6"
authors = ["Keisuke Toyota <[email protected]>"]
edition = "2018"
repository = "https://github.com/ksk001100/picterm"
Expand Down
46 changes: 35 additions & 11 deletions src/app/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use crate::app::{state::AppState, Actions, App};
use byte_unit::Byte;
use tui::{
backend::Backend,
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Style},
layout::{Alignment, Constraint, Direction, Layout},
style::{Color, Modifier, Style},
text::Span,
widgets::{Block, BorderType, Borders, Cell, List, ListItem, ListState, Paragraph, Row, Table},
widgets::{
Block, BorderType, Borders, Cell, List, ListItem, ListState, Paragraph, Row, Table, Wrap,
},
Frame,
};

Expand Down Expand Up @@ -53,8 +55,13 @@ where
let image_list = draw_image_list(app.state());
rect.render_stateful_widget(image_list, body_chunks[0], &mut state);

let image = draw_image(app.state(), body_chunks[1]);
rect.render_widget(image, body_chunks[1]);
if app.is_loading() && app.state.get_current_image().is_some() {
let loading = draw_loading();
rect.render_widget(loading, body_chunks[1]);
} else {
let image = draw_image(app.state());
rect.render_widget(image, body_chunks[1]);
}
}

fn draw_title<'a>() -> Paragraph<'a> {
Expand Down Expand Up @@ -152,12 +159,12 @@ fn draw_image_list<'a>(state: &AppState) -> List<'a> {
)
}

fn draw_image<'a>(state: &'a AppState, _rect: Rect) -> Paragraph<'a> {
let mut result = vec![];

if let Some(current_image) = state.get_current_image() {
result = current_image;
}
fn draw_image<'a>(state: &'a AppState) -> Paragraph<'a> {
let result = if let Some(current_image) = state.get_current_image() {
current_image
} else {
vec![]
};

Paragraph::new(result)
.block(
Expand All @@ -168,3 +175,20 @@ fn draw_image<'a>(state: &'a AppState, _rect: Rect) -> Paragraph<'a> {
)
.alignment(Alignment::Center)
}

fn draw_loading<'a>() -> Paragraph<'a> {
Paragraph::new(Span::styled(
"Loading...",
Style::default()
.fg(Color::White)
.add_modifier(Modifier::BOLD),
))
.block(
Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(Color::White))
.border_type(BorderType::Plain),
)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true })
}
38 changes: 28 additions & 10 deletions src/io/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,30 @@ impl<'a> IoAsyncHandler<'a> {
}

async fn do_load_image(&mut self) -> Result<()> {
let mut app = self.app.lock().await;
let mut result = vec![];
let result = Arc::new(tokio::sync::Mutex::new(vec![]));

let opt_index = {
let app = self.app.lock().await;
app.state.get_index()
};

let opt_path = {
let app = self.app.lock().await;
match opt_index {
Some(index) => app.state.get_path(index),
None => None,
}
};

let opt_term_size = {
let app = self.app.lock().await;
app.state.get_term_size()
};

if let Some(index) = app.state.get_index() {
if let Some(path) = app.state.get_path(index) {
if let Some(term_size) = app.state.get_term_size() {
let p = path.clone();
let img = tokio::task::block_in_place(move || image::open(p))?;
{
if let Some(path) = opt_path {
if let Some(term_size) = opt_term_size {
let img = tokio::task::block_in_place(|| image::open(&path))?;
let name = path
.file_name()
.unwrap_or_default()
Expand All @@ -61,14 +77,14 @@ impl<'a> IoAsyncHandler<'a> {
size,
dimensions: img.dimensions(),
};
app.state.set_current_image_info(info);

let (w, h) = image_fit_size(&img, term_size.width, term_size.height);
let imgbuf = img
.resize_exact(w, h, image::imageops::FilterType::Triangle)
.to_rgba8();
let (width, height) = imgbuf.dimensions();

let mut r = result.lock().await;
for y in 0..height {
let mut line = vec![];
for x in 0..width {
Expand All @@ -84,10 +100,12 @@ impl<'a> IoAsyncHandler<'a> {
));
}
}
result.push(Spans::from(line));
(*r).push(Spans::from(line));
}

app.state.set_current_image(result);
let mut app = self.app.lock().await;
app.state.set_current_image_info(info);
app.state.set_current_image((*r).clone());
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn tui_main(c: &Context) {
});
}

fn check_args(args: &Vec<String>) {
fn check_args(args: &[String]) {
match args.len() {
0 => (),
1 => {
Expand Down

0 comments on commit 71feae8

Please sign in to comment.