e62rs 1.5.0

An in-terminal E621/926 browser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//! post viewing stuff
use {
    crate::{
        bail,
        display::{
            dtext::parser::format_text,
            image::{
                animation::{AnimatedImage, is_animated_format, load_animated},
                dimensions::ImageDimensions,
                encoder::SixelEncoder,
                processor::ImageProcessor,
                source::ImageSource,
            },
        },
        error::{Report, Result},
        getopt,
        models::E6Post,
        ui::E6Ui,
    },
    color_eyre::eyre::Context,
    std::{
        io::{self, Write},
        path::Path,
        thread,
        time::Duration,
    },
};

/// load an animation from bytes with explicit extension
///
/// # Arguments
///
/// * `bytes` - the bytes of the animation file
/// * `ext` - the extension to load as (webp/gif)
fn load_animated_from_bytes_with_ext(bytes: &[u8], ext: &str) -> Result<AnimatedImage> {
    match ext.to_lowercase().as_str() {
        "gif" => AnimatedImage::from_gif_bytes(bytes).map_err(Report::new),
        "webp" => AnimatedImage::from_webp_bytes(bytes).map_err(Report::new),
        _ => bail!("Unsupported animation format: {}", ext),
    }
}

/// play an animation in the terminal
///
/// # Arguments
///
/// * `animated` - the animated image to play
/// * `processor` - an image processor
/// * `encoder` - a sixel encoder
fn play_animation(
    animated: AnimatedImage,
    processor: &ImageProcessor,
    encoder: &SixelEncoder,
) -> Result<()> {
    if animated.frame_count() == 1 {
        let frame = animated.get_frame(0).expect("No frame");
        let sixel_str = encoder
            .encode(&frame.data)
            .context("failed to encode frame to sixel")?;
        print!("{}", sixel_str);
        io::stdout().flush()?;
        return Ok(());
    }

    let processed = processor
        .process_animated(animated)
        .context("failed to process animation")?;

    #[allow(clippy::manual_div_ceil, reason = "bro")]
    let term_lines = (processed.height + 5) / 6;

    let mut encoded_frames = Vec::with_capacity(processed.frames.len());
    for frame in &processed.frames {
        let sixel_str = encoder
            .encode(&frame.data)
            .context("failed to encode frame to sixel")?;
        encoded_frames.push((sixel_str, frame.delay));
    }

    print!("\x1b[s");
    io::stdout().flush()?;

    let is_infinite = processed.is_infinite_loop();
    let loop_count = if is_infinite {
        1000
    } else {
        processed.loop_count.max(1)
    };

    for loop_idx in 0..loop_count {
        for (frame_idx, (sixel_str, delay)) in encoded_frames.iter().enumerate() {
            if loop_idx > 0 || frame_idx > 0 {
                print!("\x1B[{}A\x1B[G", term_lines);
            }

            print!("{}", sixel_str);
            println!();
            io::stdout().flush()?;

            thread::sleep(*delay);
        }

        if !is_infinite {
            break;
        }
    }

    thread::sleep(Duration::from_millis(100));
    print!("\x1b[u");
    io::stdout().flush()?;

    Ok(())
}

/// fetch a post image and display it in the terminal
///
/// # Arguments
///
/// * `post` - the post to fetch and display
#[allow(clippy::await_holding_lock)]
pub async fn print_post_to_terminal(post: E6Post) -> Result<()> {
    let post_url = post.file.url.unwrap_or(
        "https://static1.e621.net/data/sample/87/23/872340c066697711a8fe432271ef4768_480p.mp4"
            .to_string(),
    );
    let cfg = crate::config::instance::config()?;
    let processor = ImageProcessor::with_dimensions(ImageDimensions::from_cfg(&cfg)?);
    let encoder = SixelEncoder::new();
    let source = ImageSource::from_url(&post_url)
        .await
        .context("failed to fetch image")?;

    let url_path = Path::new(&post_url);
    if let (true, Some(ext)) = (is_animated_format(url_path), url_path.extension())
        && let ImageSource::Bytes(bytes) = &source
    {
        let ext_str = ext.to_string_lossy();
        match load_animated_from_bytes_with_ext(bytes, &ext_str) {
            Ok(animated) => {
                return play_animation(animated, &processor, &encoder);
            }
            Err(e) => {
                eprintln!(
                    "warning: file appears to be animated but failed to load as animation ({}), \
                     trying as static image...",
                    e
                );
            }
        }
    }

    let image_data = processor
        .process(source)
        .context("failed to process image")?;
    let sixel_str = encoder
        .encode(&image_data)
        .context("failed to encode image to sixel")?;

    print!("{}", sixel_str);
    println!();

    Ok(())
}

/// print an image to the terminal
///
/// # Arguments
///
/// * `path` - the path to the image to display
pub fn print_dl_to_terminal(path: &Path) -> Result<()> {
    let cfg = getopt!()?;
    let processor = ImageProcessor::with_dimensions(ImageDimensions::from_cfg(&cfg)?);
    let encoder = SixelEncoder::new();

    if is_animated_format(path) {
        match load_animated(path) {
            Ok(animated) => {
                return play_animation(animated, &processor, &encoder);
            }
            Err(e) => {
                eprintln!(
                    "warning: file appears to be animated but failed to load as animation ({}), \
                     trying as static image...",
                    e
                );
            }
        }
    }

    let source = ImageSource::from_path(path).context("failed to load image")?;
    let image_data = processor
        .process(source)
        .context("failed to process image")?;
    let sixel_str = encoder
        .encode(&image_data)
        .context("failed to encode to sixel")?;

    print!("{}", sixel_str);
    println!();

    Ok(())
}

/// fetch multiple posts and display them in the terminal
///
/// # Arguments
///
/// * `posts` - a list of posts to fetch and print
pub async fn print_posts_to_terminal(posts: Vec<E6Post>) -> Result<()> {
    for post in posts {
        print_post_to_terminal(post).await?;
    }

    Ok(())
}

/// functions for viewing posts
pub trait ViewMenu {
    /// open a post in the users default browser
    ///
    /// # Arguments
    ///
    /// * `post` - the post to open
    fn open_in_browser(&self, post: &E6Post) -> Result<()>;

    /// open multiple posts in the users default browser
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to open
    fn open_posts_in_browser(&self, posts: &[E6Post]) -> Result<()>;

    /// display post info
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to display the info for
    /// * `column_width` - the width for each column of the displayed info
    fn display_posts_row(&self, posts: &[E6Post], column_width: usize);

    /// print a post field
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to display the info for
    /// * `column_width` - the width for each column of the displayed info
    /// * `field_fn` - a function that returns the data to display in the field
    fn print_posts_field<F>(&self, posts: &[E6Post], column_width: usize, field_fn: F)
    where
        F: Fn(&E6Post) -> String;

    /// print a row separator
    ///
    /// # Arguments
    ///
    /// * `count` - the number of separators to make
    /// * `column_width` - the width for each column of the displayed info
    /// * `left` - the character to use for the left of the separator
    /// * `mid` - the character to use for the middle of the separator
    /// * `right` - the character to use for the right of the separator
    /// * `fill` - the character to use for filling in blank space
    fn print_row_separator(
        &self,
        count: usize,
        column_width: usize,
        left: &str,
        mid: &str,
        right: &str,
        fill: &str,
    );

    /// truncate a string
    ///
    /// # Arguments
    ///
    /// * `s` - the string to truncate
    /// * `max_width` - the width to truncate it to
    fn truncate_string(&self, s: &str, max_width: usize) -> String;

    /// display multiple posts
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to display
    fn display_posts(&self, posts: &[E6Post]);

    /// display the latest posts
    fn display_latest_posts(&self) -> impl Future<Output = Result<()>>;

    /// display an individual post
    ///
    /// # Arguments
    ///
    /// * `post` - the post to display
    fn display_post(&self, post: &E6Post);
}

impl ViewMenu for E6Ui {
    /// open a post in the users default browser
    ///
    /// # Arguments
    ///
    /// * `post` - the post to open
    fn open_in_browser(&self, post: &E6Post) -> Result<()> {
        if post.id <= 0 {
            bail!("invalid post id: {}", post.id);
        }

        let url = format!("https://e621.net/posts/{}", post.id);
        open::that(&url).context("failed to open post in browser")?;
        println!("Opened post in browser: {}", url);
        Ok(())
    }

    /// open multiple posts in the users default browser
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to open
    fn open_posts_in_browser(&self, posts: &[E6Post]) -> Result<()> {
        println!("Opening {} posts in browser...", posts.len());
        for post in posts {
            let url = format!("https://e621.net/posts/{}", post.id);
            open::that(&url).context("Failed to open post in browser")?;
            println!("Opened post {} in browser", post.id);
            std::thread::sleep(std::time::Duration::from_millis(500));
        }
        println!("✓ Opened all {} posts in browser", posts.len());
        Ok(())
    }

    /// display post info
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to display the info for
    /// * `column_width` - the width for each column of the displayed info
    fn display_posts_row(&self, posts: &[E6Post], column_width: usize) {
        self.print_row_separator(posts.len(), column_width, "", "", "", "");
        self.print_posts_field(posts, column_width, |post| format!("ID: {}", post.id));
        self.print_row_separator(posts.len(), column_width, "", "", "", "");
        self.print_posts_field(posts, column_width, |post| {
            format!("Rating: {} | Score: {}", post.rating, post.score.total)
        });
        self.print_posts_field(posts, column_width, |post| {
            let uploader = self.truncate_string(&post.uploader_name, 15);
            format!("{} | By: {}", post.fav_count, uploader)
        });
        self.print_posts_field(posts, column_width, |post| {
            if !post.tags.artist.is_empty() {
                let artists = post.tags.artist.join(", ");
                format!("{}", self.truncate_string(&artists, column_width - 4))
            } else {
                "󰏫 Unknown artist".to_string()
            }
        });

        self.print_posts_field(posts, column_width, |post| {
            let tags = post
                .tags
                .general
                .iter()
                .take(3)
                .cloned()
                .collect::<Vec<_>>()
                .join(", ");
            format!("󰓹 {}", self.truncate_string(&tags, column_width - 4))
        });

        self.print_row_separator(posts.len(), column_width, "", "", "", "");
    }

    /// print a post field
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to display the info for
    /// * `column_width` - the width for each column of the displayed info
    /// * `field_fn` - a function that returns the data to display in the field
    fn print_posts_field<F>(&self, posts: &[E6Post], column_width: usize, field_fn: F)
    where
        F: Fn(&E6Post) -> String,
    {
        print!("");
        for (i, post) in posts.iter().enumerate() {
            let content = field_fn(post);
            let truncated = self.truncate_string(&content, column_width - 2);
            print!(" {:width$} ", truncated, width = column_width - 2);
            print!("");
            if i < posts.len() - 1 {}
        }
        println!();
    }

    /// print a row separator
    ///
    /// # Arguments
    ///
    /// * `count` - the number of separators to make
    /// * `column_width` - the width for each column of the displayed info
    /// * `left` - the character to use for the left of the separator
    /// * `mid` - the character to use for the middle of the separator
    /// * `right` - the character to use for the right of the separator
    /// * `fill` - the character to use for filling in blank space
    fn print_row_separator(
        &self,
        count: usize,
        column_width: usize,
        left: &str,
        mid: &str,
        right: &str,
        fill: &str,
    ) {
        print!("{}", left);
        for i in 0..count {
            print!("{}", fill.repeat(column_width));
            if i < count - 1 {
                print!("{}", mid);
            }
        }
        print!("{}", right);
        println!();
    }

    /// truncate a string
    ///
    /// # Arguments
    ///
    /// * `s` - the string to truncate
    /// * `max_width` - the width to truncate it to
    fn truncate_string(&self, s: &str, max_width: usize) -> String {
        if s.len() <= max_width {
            format!("{:width$}", s, width = max_width)
        } else {
            format!("{}...", &s[..max_width.saturating_sub(3)])
        }
    }

    /// display multiple posts
    ///
    /// # Arguments
    ///
    /// * `posts` - a list of posts to display
    fn display_posts(&self, posts: &[E6Post]) {
        let posts_per_row = 3;
        let column_width = 28;

        for chunk in posts.chunks(posts_per_row) {
            self.display_posts_row(chunk, column_width);
            println!();
        }
    }

    /// display the latest posts
    async fn display_latest_posts(&self) -> Result<()> {
        let results = self
            .client
            .get_latest_posts()
            .await
            .context("Failed to fetch latest posts")?;

        if results.posts.is_empty() {
            println!("No latest posts found.");
            return Ok(());
        }

        println!("\n Latest Posts:");
        self.display_posts(&results.posts);
        Ok(())
    }

    /// display an individual post
    ///
    /// # Arguments
    ///
    /// * `post` - the post to display
    fn display_post(&self, post: &E6Post) {
        println!("\n{}", "=".repeat(50));
        println!("Post ID: {}", post.id);
        println!("Rating: {}", post.rating);
        println!(
            "Score: ↑{}{} = {}",
            post.score.up, post.score.down, post.score.total
        );
        println!("Tags: {}", post.tags.general[0..=3].join(", "));
        println!("Favorites: {}", post.fav_count);
        println!("Uploaded by: {}", post.uploader_name);

        if !post.tags.artist.is_empty() {
            println!("Artists: {}", post.tags.artist.join(", "));
        }

        if !post.description.is_empty() {
            println!("Description: {}", format_text(&post.description));
        }

        println!("{}", "=".repeat(50));
    }
}