Skip to content

Commit

Permalink
chore(docs, refactor) update readme and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
josueBarretogit committed Aug 20, 2024
1 parent 479ec4b commit 99c9564
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 84 deletions.
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
<a href="https://crates.io/crates/manga-tui">
<img alt="crates io downloads" src="https://img.shields.io/crates/d/manga-tui?logo=rust&label=crates.io downloads">
</a>
<a href="https://github.com/josueBarretogit/manga-tui/releases/latest">
<img alt="downloads" src="https://img.shields.io/github/downloads/josuebarretogit/manga-tui/total">
</a>
<a href="https://github.com/josueBarretogit/manga-tui/blob/main/LICENSE">
<img alt="License" src="https://img.shields.io/github/license/josueBarretogit/Manga-tui?style=flat-square&color=blue">
</a>
Expand Down Expand Up @@ -48,9 +51,13 @@ https://github.com/user-attachments/assets/70f321ff-13d1-4c4b-9c37-604271456ab2

https://github.com/user-attachments/assets/47e88e89-f73c-4575-9645-2abb80ca7d63

- Download manga
- Download manga

https://github.com/user-attachments/assets/ba785668-7cf1-4367-93f9-6e6e1f72c12c

- Download all chapters of a manga

https://github.com/user-attachments/assets/64880a98-74c8-4656-8cf8-2c1daf5375d2
https://github.com/user-attachments/assets/26ad493f-633c-41fc-9d09-49b316118923


## Installation
Expand All @@ -75,10 +82,14 @@ Download a binary from the [releases page](https://github.com/josueBarretogit/ma

## Image rendering

Use a terminal that can render images such as Wezterm (Personally I recommend using this one It's the one used in the videos), iTerm2 or Kitty, <br />
Use a terminal that can render images such as Wezterm (Personally I recommend using this one It's the one used in the videos), iTerm2<br />

For more information see : [image-support](https://github.com/benjajaja/ratatui-image?tab=readme-ov-file#compatibility-matrix)

> [!NOTE]
> There is an issue with kitty terminal, see [#12](https://github.com/josueBarretogit/manga-tui/issues/12)

No images will be displayed if the terminal does not have image support (but `manga-tui` will still work as a manga downloader)

## Usage
Expand All @@ -102,8 +113,9 @@ manga-tui -d

On linux it will output something like: `~/.local/share/manga-tui` <br />

On the `manga-tui` directory there will be 3 directories
On the `manga-tui` directory there will be 4 directories
- `history`, which contains a sqlite database to store reading history
- `config`, which contains a TOML file with extra configuration
- `mangaDownloads`, where manga will be downloaded
- `errorLogs`, for storing posible errors / bugs

Expand All @@ -116,6 +128,11 @@ export MANGA_TUI_DATA_DIR="/home/user/Desktop/mangas"

## Configuration

Go to the TOML file located at `config`, there you can change download format and image quality to know where it is run:
```shell
manga-tui --data-dir
```

By default `manga-tui` will search mangas in english, you can change the language by running:


Expand Down
38 changes: 12 additions & 26 deletions src/backend/download.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
use image::io::Reader;
use crate::view::pages::manga::MangaPageEvents;
use manga_tui::exists;
use std::fs::{create_dir, File};
use std::io::{BufRead, BufReader, Cursor, Write};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::time::Duration;
use tokio::sync::mpsc::UnboundedSender;
use zip::write::{FileOptions, SimpleFileOptions};
use zip::write::SimpleFileOptions;
use zip::ZipWriter;

use crate::common::PageType;
use crate::config::{DownloadType, ImageQuality, MangaTuiConfig};
use crate::utils::to_filename;
use crate::view::pages::manga::MangaPageEvents;

use super::error_log::{write_to_error_log, ErrorType};
use super::fetch::MangadexClient;
use super::filter::Languages;
use super::{ChapterPagesResponse, ChapterResponse, APP_DATA_DIR};
use super::APP_DATA_DIR;

pub struct DownloadChapter<'a> {
pub id_chapter: &'a str,
Expand All @@ -28,9 +20,8 @@ pub struct DownloadChapter<'a> {
pub lang: &'a str,
}

fn create_manga_directory(chapter: &DownloadChapter<'_>) -> Result<(PathBuf), std::io::Error> {
fn create_manga_directory(chapter: &DownloadChapter<'_>) -> Result<PathBuf, std::io::Error> {
// need directory with the manga's title, and its id to make it unique
let chapter_id = chapter.id_chapter.to_string();

let dir_manga_downloads = APP_DATA_DIR.as_ref().unwrap().join("mangaDownloads");

Expand Down Expand Up @@ -128,7 +119,7 @@ pub fn download_chapter_epub(
endpoint: String,
tx: UnboundedSender<MangaPageEvents>,
) -> Result<(), std::io::Error> {
let (chapter_dir_language) = create_manga_directory(&chapter)?;
let chapter_dir_language = create_manga_directory(&chapter)?;

let chapter_id = chapter.id_chapter.to_string();
let chapter_name = format!(
Expand All @@ -150,8 +141,7 @@ pub fn download_chapter_epub(

epub.epub_version(epub_builder::EpubVersion::V30);

epub.metadata("title", chapter_name);

let _ = epub.metadata("title", chapter_name);

for (index, file_name) in files.into_iter().enumerate() {
let image_response = MangadexClient::global()
Expand Down Expand Up @@ -232,7 +222,7 @@ pub fn download_chapter_cbz(
endpoint: String,
tx: UnboundedSender<MangaPageEvents>,
) -> Result<(), std::io::Error> {
let (chapter_dir_language) = create_manga_directory(&chapter)?;
let chapter_dir_language = create_manga_directory(&chapter)?;

let chapter_id = chapter.id_chapter.to_string();
let chapter_name = format!(
Expand Down Expand Up @@ -270,11 +260,13 @@ pub fn download_chapter_cbz(
file_name.extension().unwrap().to_str().unwrap()
);

zip.start_file(
let _ = zip.start_file(
chapter_dir_language.join(image_name).to_str().unwrap(),
options,
);
zip.write_all(&bytes).unwrap();

let _ = zip.write_all(&bytes);


if !is_downloading_all_chapters {
tx.send(MangaPageEvents::SetDownloadProgress(
Expand All @@ -301,9 +293,3 @@ pub fn download_chapter_cbz(
Ok(())
}

#[derive(Default)]
pub struct DownloadAllChapters {
pub manga_id: String,
pub manga_title: String,
pub lang: Languages,
}
13 changes: 9 additions & 4 deletions src/backend/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use std::time::Duration as StdDuration;

use super::filter::Languages;
use super::{ChapterPagesResponse, ChapterResponse, MangaStatisticsResponse, SearchMangaResponse};
use crate::backend::filter::{Filters, IntoParam};
use crate::view::pages::manga::ChapterOrder;
use bytes::Bytes;
use chrono::{Duration, Months};
use chrono::Months;
use once_cell::sync::OnceCell;
use reqwest::StatusCode;
use std::time::Duration as StdDuration;

#[derive(Clone, Debug)]
pub struct MangadexClient {
Expand Down Expand Up @@ -224,6 +223,12 @@ impl MangadexClient {
API_URL_BASE, id, order, language
);

self.client.get(endpoint).send().await?.json().await
self.client
.get(endpoint)
.timeout(StdDuration::from_secs(10))
.send()
.await?
.json()
.await
}
}
2 changes: 0 additions & 2 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use ratatui::widgets::ListItem;
use strum::{Display, EnumIter};

use crate::backend::filter::Languages;

#[derive(Default, Clone, Debug)]
Expand Down
5 changes: 1 addition & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use std::fmt::Display;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

use manga_tui::exists;
use once_cell::sync::{Lazy, OnceCell};
use once_cell::sync::{ OnceCell};
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter};

use crate::backend::AppDirectories;

#[derive(Default, Debug, Serialize, Deserialize, Display, EnumIter)]
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![forbid(unsafe_code)]
#![allow(dead_code)]
#![allow(unused)]
use std::time::Duration;

use self::backend::error_log::init_error_hooks;
Expand Down
20 changes: 7 additions & 13 deletions src/view/pages/manga.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use std::time::Duration;

use crate::backend::database::{get_chapters_history_status, save_history, SetChapterDownloaded};
use crate::backend::database::{set_chapter_downloaded, MangaReadingHistorySave};
use crate::backend::download::{
download_chapter_cbz, download_chapter_epub, download_chapter_raw_images, DownloadAllChapters,
download_chapter_cbz, download_chapter_epub, download_chapter_raw_images,
DownloadChapter,
};
use crate::backend::error_log::{self, write_to_error_log, ErrorType};
use crate::backend::error_log::{self, write_to_error_log};
use crate::backend::fetch::{MangadexClient, ITEMS_PER_PAGE_CHAPTERS};
use crate::backend::filter::Languages;
use crate::backend::tui::Events;
Expand All @@ -25,7 +23,7 @@ use crate::view::widgets::manga::{
use crate::view::widgets::Component;
use crate::PICKER;
use crossterm::event::{
KeyCode, KeyEvent, KeyModifiers, ModifierKeyCode, MouseEvent, MouseEventKind,
KeyCode, KeyEvent, MouseEvent, MouseEventKind,
};
use ratatui::{prelude::*, widgets::*};
use ratatui_image::protocol::StatefulProtocol;
Expand All @@ -36,7 +34,6 @@ use tokio::task::JoinSet;

use self::text::ToSpan;

use super::reader::MangaReaderEvents;

#[derive(PartialEq, Eq, Debug)]
pub enum PageState {
Expand Down Expand Up @@ -453,7 +450,7 @@ impl MangaPage {
if self.download_process_started() {
match key_event.code {
KeyCode::Esc => {
if self.is_downloading_all_chapters() {
if self.download_all_chapters_state.phase == DownloadPhase::DownloadingChapters {
self.local_action_tx
.send(MangaPageActions::AskAbortProcces)
.ok();
Expand Down Expand Up @@ -1016,9 +1013,6 @@ impl MangaPage {
.ok();
}

fn continue_downloading_all_chapters(&mut self) {
self.download_all_chapters_state.continue_download();
}

fn set_download_all_chapters_error(&mut self) {
self.download_all_chapters_state.set_download_error();
Expand Down Expand Up @@ -1181,14 +1175,14 @@ impl Component for MangaPage {
#[cfg(test)]
mod test {

use crate::backend::{ChapterData, Data};
use crate::backend::{ChapterData};
use crate::view::widgets::press_key;

use super::*;

fn get_manga_page() -> MangaPage {
let manga = Manga::default();
let (tx, mut rx) = mpsc::unbounded_channel::<Events>();
let (tx, _) = mpsc::unbounded_channel::<Events>();
MangaPage::new(manga, None, tx)
}

Expand Down Expand Up @@ -1418,7 +1412,7 @@ mod test {
let action = MangaPageActions::ToggleOrder;
manga_page.update(action);

/// when searching chapters avoid triggering another search by toggling order
// when searching chapters avoid triggering another search by toggling order
assert_eq!(ChapterOrder::default(), manga_page.chapter_order);

manga_page.state = PageState::DisplayingChapters;
Expand Down
1 change: 0 additions & 1 deletion src/view/pages/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use image::GenericImageView;
use ratatui::{prelude::*, widgets::*};
use ratatui_image::protocol::StatefulProtocol;
use ratatui_image::{Resize, StatefulImage};
use strum::Display;
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
use tokio::task::JoinSet;

Expand Down
Loading

0 comments on commit 99c9564

Please sign in to comment.