concord 2.5.16

A terminal user interface client for Discord
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
use image::{DynamicImage, ImageBuffer, Rgba, imageops::FilterType};
use ratatui::layout::Size;
use ratatui_image::{
    Resize,
    picker::{Picker, ProtocolType},
};

use crate::tui::text::EmojiImageSize;
use crate::{config::ImageProtocolPreference, logging};

pub(super) const AVATAR_PREVIEW_WIDTH: u16 = 4;
pub(super) const AVATAR_PREVIEW_HEIGHT: u16 = 2;
pub(in crate::tui) const PROFILE_POPUP_AVATAR_WIDTH: u16 = 8;
pub(in crate::tui) const PROFILE_POPUP_AVATAR_HEIGHT: u16 = 4;
const AVATAR_SOURCE_PIXELS_PER_COLUMN: u64 = 10;
const AVATAR_SOURCE_PIXELS_PER_ROW: u64 = AVATAR_SOURCE_PIXELS_PER_COLUMN * 3;
const DISCORD_AVATAR_CDN_PREFIX: &str = "https://cdn.discordapp.com/avatars/";
const DISCORD_GUILD_CDN_PREFIX: &str = "https://cdn.discordapp.com/guilds/";
const DISCORD_AVATAR_MIN_SIZE: u64 = 16;
const DISCORD_AVATAR_MAX_SIZE: u64 = 1024;
pub(in crate::tui) fn query_image_picker(
    protocol_preference: ImageProtocolPreference,
) -> Option<Picker> {
    match Picker::from_query_stdio() {
        Ok(mut picker) => {
            apply_protocol_preference(&mut picker, protocol_preference);
            Some(picker)
        }
        Err(error) => {
            logging::error("media", format!("image picker unavailable: {error}"));
            None
        }
    }
}

fn apply_protocol_preference(picker: &mut Picker, protocol_preference: ImageProtocolPreference) {
    if let Some(protocol_type) =
        protocol_type_for_preference(protocol_preference, is_iterm_terminal())
    {
        picker.set_protocol_type(protocol_type);
    }
}

fn protocol_type_for_preference(
    protocol_preference: ImageProtocolPreference,
    iterm_terminal: bool,
) -> Option<ProtocolType> {
    match protocol_preference {
        // iTerm2 answers ratatui-image's Kitty capability query, but its Kitty
        // implementation does not support the unicode-placeholder mode used by
        // ratatui-image. Prefer the native iTerm2 protocol when auto-detecting
        // inside iTerm so images render instead of selecting a broken Kitty path.
        ImageProtocolPreference::Auto if iterm_terminal => Some(ProtocolType::Iterm2),
        ImageProtocolPreference::Auto => None,
        ImageProtocolPreference::Iterm2 => Some(ProtocolType::Iterm2),
        ImageProtocolPreference::Kitty => Some(ProtocolType::Kitty),
        ImageProtocolPreference::Sixel => Some(ProtocolType::Sixel),
        ImageProtocolPreference::Halfblocks => Some(ProtocolType::Halfblocks),
    }
}

fn is_iterm_terminal() -> bool {
    is_iterm_terminal_values(
        std::env::var("TERM_PROGRAM").ok().as_deref(),
        std::env::var("LC_TERMINAL").ok().as_deref(),
    )
}

fn is_iterm_terminal_values(term_program: Option<&str>, lc_terminal: Option<&str>) -> bool {
    term_program.is_some_and(|value| value.contains("iTerm"))
        || lc_terminal.is_some_and(|value| value.contains("iTerm"))
}

pub(super) fn avatar_preview_url(url: &str, width_columns: u16, height_rows: u16) -> String {
    if !is_discord_avatar_url(url) {
        return url.to_owned();
    }

    let size = avatar_preview_size(width_columns, height_rows);
    let (base, query) = url.split_once('?').unwrap_or((url, ""));
    let mut params = query
        .split('&')
        .filter(|param| !param.is_empty())
        .filter(|param| {
            let key = param.split_once('=').map_or(*param, |(key, _)| key);
            key != "size"
        })
        .map(str::to_owned)
        .collect::<Vec<_>>();
    params.push(format!("size={size}"));

    format!("{base}?{}", params.join("&"))
}

fn is_discord_avatar_url(url: &str) -> bool {
    url.starts_with(DISCORD_AVATAR_CDN_PREFIX) || is_discord_guild_member_avatar_url(url)
}

fn is_discord_guild_member_avatar_url(url: &str) -> bool {
    let Some(path) = url
        .split_once('?')
        .map_or(url, |(path, _)| path)
        .strip_prefix(DISCORD_GUILD_CDN_PREFIX)
    else {
        return false;
    };
    let mut segments = path.split('/');
    matches!(
        (
            segments.next(),
            segments.next(),
            segments.next(),
            segments.next(),
            segments.next(),
            segments.next(),
        ),
        (Some(guild_id), Some("users"), Some(user_id), Some("avatars"), Some(avatar), None)
            if !guild_id.is_empty() && !user_id.is_empty() && !avatar.is_empty()
    )
}

fn avatar_preview_size(width_columns: u16, height_rows: u16) -> u64 {
    let width = u64::from(width_columns).saturating_mul(AVATAR_SOURCE_PIXELS_PER_COLUMN);
    let height = u64::from(height_rows).saturating_mul(AVATAR_SOURCE_PIXELS_PER_ROW);
    let needed = width.max(height).max(1);
    needed
        .clamp(DISCORD_AVATAR_MIN_SIZE, DISCORD_AVATAR_MAX_SIZE)
        .next_power_of_two()
        .min(DISCORD_AVATAR_MAX_SIZE)
}

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub(in crate::tui) struct MediaProtocolRenderSpec {
    pub(super) width: u16,
    pub(super) height: u16,
    pub(super) visible_height: u16,
    pub(super) top_clip_rows: u16,
    pub(super) show_play_marker: bool,
    pub(super) mask_circular: bool,
}

/// Estimated bytes a built protocol retains. The encoded frame is the resized
/// RGBA image inflated by the protocol's text encoding (base64 for kitty and
/// iTerm2), which measures within a few percent of this. It only bounds caches,
/// so being close is enough.
pub(in crate::tui) fn estimated_media_protocol_bytes(
    spec: MediaProtocolRenderSpec,
    font_size: (u16, u16),
) -> u64 {
    let pixels = u64::from(spec.width)
        .saturating_mul(u64::from(font_size.0))
        .saturating_mul(u64::from(spec.height))
        .saturating_mul(u64::from(font_size.1));
    pixels.saturating_mul(4).saturating_mul(4) / 3
}

pub(in crate::tui) fn fixed_media_protocol_render_spec(
    width: u16,
    height: u16,
) -> MediaProtocolRenderSpec {
    MediaProtocolRenderSpec {
        width,
        height,
        visible_height: height,
        top_clip_rows: 0,
        show_play_marker: false,
        mask_circular: false,
    }
}

/// `Picker::font_size` returns a `FontSize` struct as of ratatui-image 11; the
/// rest of our pixel math works in `(width, height)` tuples, so convert here.
pub(in crate::tui) fn picker_font_size(picker: &Picker) -> (u16, u16) {
    let font_size = picker.font_size();
    (font_size.width, font_size.height)
}

pub(super) fn clipped_media_image(
    image: &DynamicImage,
    font_size: (u16, u16),
    spec: MediaProtocolRenderSpec,
) -> Option<DynamicImage> {
    if spec.width == 0 || spec.height == 0 || spec.visible_height == 0 {
        return None;
    }

    let (font_width, font_height) = font_size;
    let full_width = u32::from(spec.width).checked_mul(u32::from(font_width))?;
    let full_height = u32::from(spec.height).checked_mul(u32::from(font_height))?;
    let crop_top = u32::from(spec.top_clip_rows).checked_mul(u32::from(font_height))?;
    let crop_height = u32::from(spec.visible_height)
        .checked_mul(u32::from(font_height))?
        .min(full_height.saturating_sub(crop_top));
    if full_width == 0 || crop_height == 0 {
        return None;
    }

    let mut fitted = fit_image_to_canvas(image, full_width, full_height);
    if spec.show_play_marker {
        apply_video_play_marker(&mut fitted);
    }
    let mut cropped = fitted.crop_imm(0, crop_top, full_width, crop_height);
    if spec.mask_circular {
        apply_circular_alpha_mask(&mut cropped, full_width, full_height, crop_top);
    }
    Some(cropped)
}

fn apply_video_play_marker(image: &mut DynamicImage) {
    let mut rgba = image.to_rgba8();
    let width = rgba.width();
    let height = rgba.height();
    let min_dimension = width.min(height);
    if min_dimension < 24 {
        return;
    }

    let cx = width as f32 / 2.0 - 0.5;
    let cy = height as f32 / 2.0 - 0.5;
    let radius = (min_dimension as f32 * 0.14).clamp(10.0, 56.0);
    let radius_sq = radius * radius;

    for (x, y, pixel) in rgba.enumerate_pixels_mut() {
        let dx = x as f32 - cx;
        let dy = y as f32 - cy;
        if dx * dx + dy * dy <= radius_sq {
            blend_pixel(pixel, Rgba([0, 0, 0, 135]));
        }
    }

    let left = cx - radius * 0.24;
    let right = cx + radius * 0.42;
    let top = cy - radius * 0.42;
    let bottom = cy + radius * 0.42;
    let triangle_min_x = left.floor().max(0.0) as u32;
    let triangle_max_x = right.ceil().min(width.saturating_sub(1) as f32) as u32;
    let triangle_min_y = top.floor().max(0.0) as u32;
    let triangle_max_y = bottom.ceil().min(height.saturating_sub(1) as f32) as u32;

    for y in triangle_min_y..=triangle_max_y {
        let vertical = if y as f32 <= cy {
            ((y as f32 - top) / (cy - top)).clamp(0.0, 1.0)
        } else {
            ((bottom - y as f32) / (bottom - cy)).clamp(0.0, 1.0)
        };
        let row_left = left;
        let row_right = left + (right - left) * vertical;
        for x in triangle_min_x..=triangle_max_x {
            let xf = x as f32;
            if xf >= row_left && xf <= row_right {
                blend_pixel(rgba.get_pixel_mut(x, y), Rgba([245, 247, 250, 230]));
            }
        }
    }

    *image = DynamicImage::ImageRgba8(rgba);
}

fn blend_pixel(pixel: &mut Rgba<u8>, overlay: Rgba<u8>) {
    let alpha = u16::from(overlay.0[3]);
    let inverse_alpha = 255u16.saturating_sub(alpha);
    for channel in 0..3 {
        pixel.0[channel] = ((u16::from(overlay.0[channel]) * alpha
            + u16::from(pixel.0[channel]) * inverse_alpha
            + 127)
            / 255) as u8;
    }
    pixel.0[3] = pixel.0[3].max(overlay.0[3]);
}

/// Zeroes the alpha channel for pixels outside the circle inscribed in the
/// full (uncropped) image bounds. The mask is computed against the full image
/// because vertical clipping (`top_clip_rows`) shifts the crop window, but the
/// circle should stay anchored to the original avatar — otherwise scrolling
/// would deform it.
fn apply_circular_alpha_mask(
    image: &mut DynamicImage,
    full_width: u32,
    full_height: u32,
    crop_top: u32,
) {
    let mut rgba = image.to_rgba8();
    let cx = full_width as f32 / 2.0 - 0.5;
    let cy = full_height as f32 / 2.0 - 0.5;
    let radius = (full_width.min(full_height) as f32 / 2.0) - 0.5;
    let radius_sq = radius * radius;
    for (x, y, pixel) in rgba.enumerate_pixels_mut() {
        let dx = x as f32 - cx;
        let dy = (y + crop_top) as f32 - cy;
        if dx * dx + dy * dy > radius_sq {
            pixel.0[3] = 0;
        }
    }
    *image = DynamicImage::ImageRgba8(rgba);
}

pub(in crate::tui) fn clipped_media_protocol(
    picker: &Picker,
    image: &DynamicImage,
    spec: MediaProtocolRenderSpec,
) -> Option<ratatui_image::protocol::Protocol> {
    let image = clipped_media_image(image, picker_font_size(picker), spec)?;
    picker
        .new_protocol(
            image,
            Size::new(spec.width, spec.visible_height),
            Resize::Fit(None),
        )
        .ok()
}

fn fit_image_to_canvas(image: &DynamicImage, width: u32, height: u32) -> DynamicImage {
    let resized = image.resize(width, height, FilterType::Nearest);
    if resized.width() == width && resized.height() == height {
        return resized;
    }

    let mut canvas =
        DynamicImage::ImageRgba8(ImageBuffer::from_pixel(width, height, Rgba([0, 0, 0, 0])));
    let x_offset = width.saturating_sub(resized.width()) / 2;
    let y_offset = height.saturating_sub(resized.height()) / 2;
    image::imageops::overlay(
        &mut canvas,
        &resized,
        i64::from(x_offset),
        i64::from(y_offset),
    );
    canvas
}

pub(super) fn emoji_protocol(
    picker: &Picker,
    img: &DynamicImage,
    image_size: EmojiImageSize,
) -> Option<ratatui_image::protocol::Protocol> {
    let (font_width, font_height) = picker_font_size(picker);
    let canvas_w = u32::from(image_size.width()) * u32::from(font_width);
    let canvas_h = u32::from(image_size.height()) * u32::from(font_height);

    let scaled = img.resize(canvas_w, canvas_h, FilterType::Lanczos3);
    let scaled_rgba = scaled.to_rgba8();

    let x_off = ((canvas_w.saturating_sub(scaled_rgba.width())) / 2) as i64;
    let y_off = ((canvas_h.saturating_sub(scaled_rgba.height())) / 2) as i64;

    let mut canvas = image::RgbaImage::new(canvas_w, canvas_h);
    image::imageops::overlay(&mut canvas, &scaled_rgba, x_off, y_off);

    picker
        .new_protocol(
            DynamicImage::ImageRgba8(canvas),
            Size::new(image_size.width(), image_size.height()),
            Resize::Fit(None),
        )
        .ok()
}

#[cfg(test)]
mod tests {
    use super::{is_iterm_terminal_values, protocol_type_for_preference};
    use crate::config::ImageProtocolPreference;
    use ratatui_image::picker::ProtocolType;

    #[test]
    fn auto_protocol_forces_iterm2_inside_iterm() {
        assert_eq!(
            protocol_type_for_preference(ImageProtocolPreference::Auto, true),
            Some(ProtocolType::Iterm2)
        );
        assert_eq!(
            protocol_type_for_preference(ImageProtocolPreference::Auto, false),
            None
        );
    }

    #[test]
    fn explicit_protocol_preference_overrides_terminal_detection() {
        let cases = [
            (ImageProtocolPreference::Iterm2, ProtocolType::Iterm2),
            (ImageProtocolPreference::Kitty, ProtocolType::Kitty),
            (ImageProtocolPreference::Sixel, ProtocolType::Sixel),
            (
                ImageProtocolPreference::Halfblocks,
                ProtocolType::Halfblocks,
            ),
        ];

        for (preference, expected) in cases {
            assert_eq!(
                protocol_type_for_preference(preference, true),
                Some(expected)
            );
            assert_eq!(
                protocol_type_for_preference(preference, false),
                Some(expected)
            );
        }
    }

    #[test]
    fn iterm_detection_accepts_term_program_or_lc_terminal() {
        assert!(is_iterm_terminal_values(Some("iTerm.app"), None));
        assert!(is_iterm_terminal_values(None, Some("iTerm2")));
        assert!(!is_iterm_terminal_values(Some("WezTerm"), None));
    }
}