@@ -12,22 +12,26 @@ use ratatui::{
1212 prelude:: * ,
1313 widgets:: { Block , Borders , Padding , Paragraph , Scrollbar , ScrollbarState } ,
1414} ;
15- use ratatui_image:: { FilterType , Resize , StatefulImage , picker:: Picker } ;
15+ use ratatui_image:: {
16+ FilterType , Resize , StatefulImage , picker:: Picker , protocol:: StatefulProtocol ,
17+ } ;
1618use regex:: Regex ;
1719use tui_textarea:: { Input , TextArea } ;
1820use unicode_width:: UnicodeWidthStr ;
1921
2022use crate :: message:: MessageContent ;
2123
22- pub struct Explore { }
24+ pub struct Explore < ' a > {
25+ pub picker : & ' a Picker ,
26+ }
2327
2428#[ derive( Debug ) ]
2529pub struct ExploreState {
2630 pub selected : usize ,
2731 pub paths : Vec < PathBuf > ,
2832 textarea : TextArea < ' static > ,
29- lru : LruCache < PathBuf , Vec < u8 > > ,
30- picker : Picker ,
33+ lru_bytes : LruCache < PathBuf , Option < Vec < u8 > > > ,
34+ lru_image : LruCache < PathBuf , Option < StatefulProtocol > > ,
3135}
3236
3337impl Default for ExploreState {
@@ -36,8 +40,8 @@ impl Default for ExploreState {
3640 selected : 0 ,
3741 paths : collect_paths ( std:: env:: current_dir ( ) . unwrap ( ) ) ,
3842 textarea : Default :: default ( ) ,
39- lru : LruCache :: new ( NonZeroUsize :: new ( 16 ) . unwrap ( ) ) ,
40- picker : Picker :: from_query_stdio ( ) . unwrap ( ) ,
43+ lru_bytes : LruCache :: new ( NonZeroUsize :: new ( 16 ) . unwrap ( ) ) ,
44+ lru_image : LruCache :: new ( NonZeroUsize :: new ( 16 ) . unwrap ( ) ) ,
4145 }
4246 }
4347}
@@ -100,7 +104,7 @@ impl ExploreState {
100104 }
101105}
102106
103- impl StatefulWidget for Explore {
107+ impl StatefulWidget for Explore < ' _ > {
104108 type State = ExploreState ;
105109
106110 fn render ( self , area : Rect , buf : & mut Buffer , state : & mut Self :: State ) {
@@ -168,7 +172,7 @@ impl StatefulWidget for Explore {
168172 if let Some ( mime) = mime_guess:: from_path ( selected) . first ( ) {
169173 match mime. type_ ( ) {
170174 TEXT => {
171- let bytes = state. lru . get_or_insert ( selected. clone ( ) , || {
175+ if let Some ( bytes) = state. lru_bytes . get_or_insert ( selected. clone ( ) , || {
172176 fs:: File :: open ( selected)
173177 . map ( |mut file| {
174178 let mut lines = 0 ;
@@ -189,20 +193,28 @@ impl StatefulWidget for Explore {
189193 }
190194 bytes
191195 } )
192- . unwrap_or_default ( )
193- } ) ;
194- let text = String :: from_utf8_lossy ( bytes) ;
195- let lines = text. lines ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( ) ;
196- Paragraph :: new ( lines) . render ( preview, buf) ;
196+ . ok ( )
197+ } ) {
198+ let text = String :: from_utf8_lossy ( bytes) ;
199+ let lines = text. lines ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( ) ;
200+ Paragraph :: new ( lines) . render ( preview, buf) ;
201+ }
197202 }
198203 IMAGE => {
199- let bytes = state. lru . get_or_insert ( selected. clone ( ) , || {
200- std:: fs:: read ( selected) . unwrap_or_default ( )
201- } ) ;
202- if let Ok ( image) = load_from_memory ( bytes) {
203- let mut image = state. picker . new_resize_protocol ( image) ;
204- image. resize_encode ( & Resize :: Fit ( Some ( FilterType :: CatmullRom ) ) , preview) ;
205- StatefulImage :: default ( ) . render ( preview, buf, & mut image) ;
204+ if let Some ( image) = state. lru_image . get_or_insert_mut ( selected. clone ( ) , || {
205+ std:: fs:: read ( selected)
206+ . ok ( )
207+ . and_then ( |bytes| load_from_memory ( & bytes) . ok ( ) )
208+ . map ( |image| {
209+ let mut image = self . picker . new_resize_protocol ( image) ;
210+ image. resize_encode (
211+ & Resize :: Fit ( Some ( FilterType :: CatmullRom ) ) ,
212+ preview,
213+ ) ;
214+ image
215+ } )
216+ } ) {
217+ StatefulImage :: default ( ) . render ( preview, buf, image) ;
206218 }
207219 }
208220 _ => { }
0 commit comments