@@ -18,6 +18,8 @@ pub struct BookManager {
1818 pub books : Vec < BookInfo > ,
1919 scan_directory : String ,
2020 pub library_mode : LibraryMode ,
21+ #[ cfg( feature = "pdf" ) ]
22+ pub supports_graphics : bool ,
2123}
2224
2325/// Format of a book file
@@ -71,6 +73,8 @@ impl BookManager {
7173 books,
7274 scan_directory,
7375 library_mode,
76+ #[ cfg( feature = "pdf" ) ]
77+ supports_graphics : false ,
7478 }
7579 }
7680
@@ -593,11 +597,13 @@ impl BookManager {
593597 let mut books: Vec < BookInfo > ;
594598 #[ cfg( feature = "pdf" ) ]
595599 {
596- if !is_pdf_enabled ( ) {
600+ if !is_pdf_enabled ( ) || ! self . supports_graphics {
597601 books = self
598602 . books
599603 . iter ( )
600- . filter ( |book| book. format != BookFormat :: Pdf )
604+ . filter ( |book| {
605+ book. format != BookFormat :: Pdf && book. format != BookFormat :: Djvu
606+ } )
601607 . cloned ( )
602608 . collect ( ) ;
603609 } else {
@@ -693,6 +699,75 @@ mod tests {
693699 fs:: write ( path, contents. as_bytes ( ) ) . unwrap ( ) ;
694700 }
695701
702+ #[ cfg( feature = "pdf" ) ]
703+ #[ test]
704+ fn get_books_filters_pdf_and_djvu_when_pdf_disabled ( ) {
705+ use crate :: settings:: { is_pdf_enabled, set_pdf_enabled} ;
706+
707+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
708+ // Create dummy files so discover_books_in_dir picks them up
709+ fs:: write ( temp_dir. path ( ) . join ( "novel.epub" ) , b"fake" ) . unwrap ( ) ;
710+ fs:: write ( temp_dir. path ( ) . join ( "paper.pdf" ) , b"fake" ) . unwrap ( ) ;
711+ fs:: write ( temp_dir. path ( ) . join ( "scan.djvu" ) , b"fake" ) . unwrap ( ) ;
712+
713+ let manager = BookManager :: new_with_directory ( temp_dir. path ( ) . to_str ( ) . unwrap ( ) ) ;
714+ assert_eq ! ( manager. books. len( ) , 3 , "all 3 files should be discovered" ) ;
715+
716+ let prev = is_pdf_enabled ( ) ;
717+ set_pdf_enabled ( false ) ;
718+
719+ let filtered = manager. get_books ( ) ;
720+ let formats: Vec < _ > = filtered. iter ( ) . map ( |b| b. format ) . collect ( ) ;
721+
722+ set_pdf_enabled ( prev) ;
723+
724+ assert ! (
725+ !formats. contains( & BookFormat :: Pdf ) ,
726+ "PDF books should be filtered out when pdf is disabled"
727+ ) ;
728+ assert ! (
729+ !formats. contains( & BookFormat :: Djvu ) ,
730+ "DJVU books should be filtered out when pdf is disabled"
731+ ) ;
732+ assert_eq ! ( filtered. len( ) , 1 , "only the epub should remain" ) ;
733+ }
734+
735+ #[ cfg( feature = "pdf" ) ]
736+ #[ test]
737+ fn get_books_filters_pdf_and_djvu_when_no_graphics_support ( ) {
738+ use crate :: settings:: { is_pdf_enabled, set_pdf_enabled} ;
739+
740+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
741+ fs:: write ( temp_dir. path ( ) . join ( "novel.epub" ) , b"fake" ) . unwrap ( ) ;
742+ fs:: write ( temp_dir. path ( ) . join ( "paper.pdf" ) , b"fake" ) . unwrap ( ) ;
743+ fs:: write ( temp_dir. path ( ) . join ( "scan.djvu" ) , b"fake" ) . unwrap ( ) ;
744+
745+ let manager = BookManager :: new_with_directory ( temp_dir. path ( ) . to_str ( ) . unwrap ( ) ) ;
746+ assert_eq ! ( manager. books. len( ) , 3 , "all 3 files should be discovered" ) ;
747+
748+ // pdf_enabled is true (default) — simulating a user who has never toggled the setting.
749+ // But the terminal doesn't support graphics, so PDFs/DJVUs should still be hidden.
750+ let prev = is_pdf_enabled ( ) ;
751+ set_pdf_enabled ( true ) ;
752+
753+ let filtered = manager. get_books ( ) ;
754+ let formats: Vec < _ > = filtered. iter ( ) . map ( |b| b. format ) . collect ( ) ;
755+
756+ set_pdf_enabled ( prev) ;
757+
758+ // This must hold regardless of the pdf_enabled setting:
759+ // a terminal without graphics cannot render PDFs/DJVUs.
760+ assert ! (
761+ !formats. contains( & BookFormat :: Pdf ) ,
762+ "PDF books should be filtered out when terminal has no graphics support"
763+ ) ;
764+ assert ! (
765+ !formats. contains( & BookFormat :: Djvu ) ,
766+ "DJVU books should be filtered out when terminal has no graphics support"
767+ ) ;
768+ assert_eq ! ( filtered. len( ) , 1 , "only the epub should remain" ) ;
769+ }
770+
696771 #[ test]
697772 fn load_exploded_epub_directory ( ) {
698773 let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
0 commit comments