bkmr 7.4.1

Knowledge management for humans and agents — bookmarks, snippets, etc, searchable, executable.
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
// src/cli/fzf.rs

use std::borrow::Cow;
use std::io::Write;

use crate::cli::bookmark_commands;
use crate::cli::error::CliResult;
use crate::cli::process::{
    clone_bookmark, copy_bookmark_url_to_clipboard, copy_url_to_clipboard, delete_bookmarks,
    edit_bookmarks, execute_bookmark_default_action,
};
use crate::domain::bookmark::Bookmark;
use crate::domain::search::SemanticSearchResult;
use crate::domain::system_tag::SystemTag;
use crate::infrastructure::di::ServiceContainer;
use crate::util::helper::{format_file_path, format_mtime};
use crossterm::event::{KeyCode, KeyModifiers};
use crossterm::style::Stylize;
use crossterm::{
    execute,
    terminal::{Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::style::{Color, Style};
use ratatui::text::{Line, Span};
use skim::{prelude::*, DisplayContext, ItemPreview, PreviewContext, Skim, SkimItem};
use tracing::{debug, instrument};

/// Unified skim item for both classic and enhanced display styles.
/// The difference between styles is in how `display_text` and `preview_text`
/// are constructed at creation time.
#[derive(Clone)]
struct FzfBookmarkItem {
    bookmark: Bookmark,
    display_text: String,
    preview_text: String,
    /// Pre-parsed segments for colored display (start, end, color)
    color_segments: Vec<(usize, usize, Color)>,
}

impl FzfBookmarkItem {
    fn new_classic(
        bookmark: &Bookmark,
        max_id_width: usize,
        action_description: &str,
        settings: &crate::config::Settings,
    ) -> Self {
        let display_text =
            create_bookmark_display_text(bookmark, max_id_width, action_description, settings);
        let preview_text = create_bookmark_preview_text(bookmark, action_description);
        let color_segments =
            compute_color_segments(&display_text, bookmark, max_id_width, &settings.fzf_opts);

        Self {
            bookmark: bookmark.clone(),
            display_text,
            preview_text,
            color_segments,
        }
    }

    fn new_enhanced(
        bookmark: &Bookmark,
        max_id_width: usize,
        action_description: &str,
        services: &ServiceContainer,
        show_action: bool,
    ) -> Self {
        let id = bookmark.id.unwrap_or(0);
        let display_text = format!("{:>width$}: {}", id, bookmark.title, width = max_id_width);
        let preview_text =
            create_enhanced_preview(bookmark, action_description, services, show_action);

        Self {
            bookmark: bookmark.clone(),
            display_text,
            preview_text,
            color_segments: Vec::new(), // Enhanced style uses plain text display
        }
    }
}

impl SkimItem for FzfBookmarkItem {
    fn text(&self) -> Cow<'_, str> {
        Cow::Borrowed(&self.display_text)
    }

    fn display(&self, context: DisplayContext) -> Line<'_> {
        if self.color_segments.is_empty() {
            // Enhanced style: use default highlighting
            return context.to_line(self.text());
        }

        // Classic style: apply color segments
        let text = &self.display_text;
        let mut spans = Vec::new();
        let mut pos = 0;

        for &(start, end, color) in &self.color_segments {
            let start = start.min(text.len());
            let end = end.min(text.len());
            if start > pos {
                spans.push(Span::raw(&text[pos..start]));
            }
            if start < end {
                spans.push(Span::styled(&text[start..end], Style::default().fg(color)));
            }
            pos = end;
        }
        if pos < text.len() {
            spans.push(Span::raw(&text[pos..]));
        }

        Line::from(spans)
    }

    fn preview(&self, _context: PreviewContext) -> ItemPreview {
        ItemPreview::AnsiText(self.preview_text.clone())
    }

    fn output(&self) -> Cow<'_, str> {
        Cow::Owned(self.bookmark.id.unwrap_or(0).to_string())
    }
}

impl SkimItem for SemanticSearchResult {
    fn text(&self) -> Cow<'_, str> {
        Cow::Owned(self.display())
    }
}

/// Compute color segments sorted by position for the classic display style
fn compute_color_segments(
    text: &str,
    bookmark: &Bookmark,
    max_id_width: usize,
    fzf_opts: &crate::config::FzfOpts,
) -> Vec<(usize, usize, Color)> {
    let mut segments = Vec::new();
    let padding = max_id_width + 2; // "ID: "
    let title_end = padding + bookmark.title.len();

    // Title in green
    segments.push((padding, title_end, Color::Green));

    // URL in yellow (within angle brackets)
    if !fzf_opts.no_url {
        if let (Some(start), Some(end)) = (text.find('<'), text.find('>')) {
            segments.push((start, end + 1, Color::Yellow));
        }
    }

    // Tags in magenta (within square brackets)
    if fzf_opts.show_tags && !bookmark.tags.is_empty() {
        if let (Some(start), Some(end)) = (text.find('['), text.find(']')) {
            segments.push((start, end + 1, Color::Magenta));
        }
    }

    // Action in cyan (within parentheses)
    if fzf_opts.show_action {
        if let (Some(start), Some(end)) = (text.rfind('('), text.rfind(')')) {
            segments.push((start, end + 1, Color::Cyan));
        }
    }

    // File info in dark grey
    if fzf_opts.show_file_info {
        if let Some(start) = text.find("📁") {
            let end = text[start..]
                .find('\n')
                .map(|p| start + p)
                .unwrap_or(text.len());
            segments.push((start, end, Color::DarkGray));
        }
    }

    // Sort by start position for correct span construction
    segments.sort_by_key(|s| s.0);
    segments
}

/// Create display text for a bookmark with proper formatting
fn create_bookmark_display_text(
    bookmark: &Bookmark,
    max_id_width: usize,
    action_description: &str,
    settings: &crate::config::Settings,
) -> String {
    let id = bookmark.id.unwrap_or(0);
    let title = &bookmark.title;
    let url = &bookmark.url;
    let binding = bookmark.formatted_tags();
    let tags_str = binding.trim_matches(',');

    let fzf_opts = &settings.fzf_opts;

    let tags_display = if fzf_opts.show_tags {
        format!(" [{}]", tags_str)
    } else {
        String::new()
    };

    let action_display = if fzf_opts.show_action {
        format!(" ({})", action_description)
    } else {
        String::new()
    };

    let mut text = if fzf_opts.no_url {
        format!(
            "{:>width$}: {}{}{}",
            id,
            title,
            action_display,
            tags_display,
            width = max_id_width
        )
    } else {
        format!(
            "{:>width$}: {} <{}>{}{}",
            id,
            title,
            url,
            action_display,
            tags_display,
            width = max_id_width
        )
    };

    if fzf_opts.show_file_info {
        if let (Some(file_path), Some(file_mtime)) = (&bookmark.file_path, bookmark.file_mtime) {
            let padding = " ".repeat(max_id_width + 2);
            let formatted_path = format_file_path(file_path, 120);
            let formatted_time = format_mtime(file_mtime);
            text.push_str(&format!(
                "\n{}📁 {} ({})",
                padding, formatted_path, formatted_time
            ));
        }
    }

    text
}

/// Create preview text for a bookmark
fn create_bookmark_preview_text(bookmark: &Bookmark, action_description: &str) -> String {
    let mut preview_text = format!(
        "ID: {}\nTitle: {}\nURL/Content: {}\nDescription: {}\nTags: {}\nAccess Count: {}\nDefault Action: {}",
        bookmark.id.unwrap_or(0),
        bookmark.title,
        bookmark.url,
        bookmark.description,
        bookmark.formatted_tags().trim_matches(','),
        bookmark.access_count,
        action_description
    );

    if let (Some(file_path), Some(file_mtime)) = (&bookmark.file_path, bookmark.file_mtime) {
        let formatted_path = format_file_path(file_path, 120);
        let formatted_time = format_mtime(file_mtime);
        preview_text.push_str(&format!(
            "\n\nSource: {} ({})",
            formatted_path, formatted_time
        ));
    }

    format!("{}\n{}", "Bookmark Details:".bold(), preview_text)
}

/// Create enhanced preview text with ANSI colors
fn create_enhanced_preview(
    bookmark: &Bookmark,
    action_description: &str,
    services: &ServiceContainer,
    show_action: bool,
) -> String {
    // Render template variables in URL if present
    let rendered_url = if bookmark.url.contains("{{") || bookmark.url.contains("{%") {
        services
            .interpolation_service
            .render_bookmark_url(bookmark)
            .unwrap_or_else(|_| bookmark.url.clone())
    } else {
        bookmark.url.clone()
    };

    let tags_str = bookmark
        .formatted_tags()
        .replace(',', " ")
        .trim()
        .to_string();

    let mut preview = format!(
        "{}: {}\n\n{}:\n{}\n\n{}:\n{}",
        "Title".green().bold(),
        bookmark.title,
        "Description".yellow().bold(),
        if bookmark.description.is_empty() {
            "No description"
        } else {
            &bookmark.description
        },
        "URL/Content".cyan().bold(),
        rendered_url,
    );

    if show_action {
        preview.push_str(&format!(
            "\n\n{}: {}",
            "Default Action".magenta().bold(),
            action_description
        ));
    }

    if !tags_str.is_empty() {
        preview.push_str(&format!("\n\n{}: {}", "Tags".blue().bold(), tags_str));
    }

    if let (Some(file_path), Some(file_mtime)) = (&bookmark.file_path, &bookmark.file_mtime) {
        let formatted_path = format_file_path(file_path, 120);
        let formatted_time = format_mtime(*file_mtime);
        preview.push_str(&format!(
            "\n\n{}: {} ({})",
            "Source File".dark_grey().bold(),
            formatted_path,
            formatted_time
        ));
    }

    preview
}

/// Extract selected bookmarks from skim output
fn get_selected_bookmarks(output: &SkimOutput, bookmarks: &[Bookmark]) -> Vec<Bookmark> {
    let selected_ids: Vec<i32> = output
        .selected_items
        .iter()
        .filter_map(|matched| {
            let id_str = matched.item.output();
            id_str.parse::<i32>().ok()
        })
        .collect();

    let selected: Vec<Bookmark> = bookmarks
        .iter()
        .filter(|b| b.id.is_some() && selected_ids.contains(&b.id.unwrap()))
        .cloned()
        .collect();

    if !selected.is_empty() {
        eprintln!("Selected bookmarks:");
        for bookmark in &selected {
            eprintln!(" - {}: {}", bookmark.id.unwrap_or(0), bookmark.title);
        }
    }

    debug!("Selected {} bookmarks", selected.len());
    selected
}

/// Processes bookmarks using the fzf-like selector interface
#[instrument(skip(bookmarks), level = "debug")]
pub fn fzf_process(
    bookmarks: &[Bookmark],
    style: &str,
    services: &ServiceContainer,
    settings: &crate::config::Settings,
    stdout: bool,
) -> CliResult<()> {
    if bookmarks.is_empty() {
        eprintln!("No bookmarks to display");
        return Ok(());
    }

    // Bookmarks arrive pre-sorted by the query layer
    let max_id_width = bookmarks
        .iter()
        .map(|b| b.id.unwrap_or(0).to_string().len())
        .max()
        .unwrap_or(0);

    let fzf_opts = &settings.fzf_opts;

    // Build skim options
    let mut options = SkimOptions::default();
    options.height = fzf_opts.height.clone();
    options.reverse = fzf_opts.reverse;
    options.multi = false;
    options.ansi = true;
    options.query = Some("".to_string());

    if style == "enhanced" {
        options.preview = Some("".to_string());
        options.preview_window = "right:70%:wrap".into();
    }

    options.bind = vec![
        "ctrl-a:accept".to_string(),
        "ctrl-o:accept".to_string(),
        "ctrl-y:accept".to_string(),
        "ctrl-e:accept".to_string(),
        "ctrl-d:accept".to_string(),
        "ctrl-p:accept".to_string(),
        "enter:accept".to_string(),
        "esc:abort".to_string(),
    ];

    // Finalize options (processes bind strings into keymap, applies defaults)
    let options = options.build();

    // Build items based on style
    let items: Vec<FzfBookmarkItem> = bookmarks
        .iter()
        .map(|bookmark| {
            let base_description = services
                .action_service
                .get_default_action_description(bookmark);
            let action_description = bookmark_commands::format_action_description(
                base_description,
                bookmark.opener.as_ref(),
            );

            if style == "enhanced" {
                FzfBookmarkItem::new_enhanced(
                    bookmark,
                    max_id_width,
                    &action_description,
                    services,
                    fzf_opts.show_action,
                )
            } else {
                FzfBookmarkItem::new_classic(
                    bookmark,
                    max_id_width,
                    &action_description,
                    settings,
                )
            }
        })
        .collect();

    // Alternate screen for non-fullscreen, non-stdout mode
    let use_alternate_screen = !stdout && fzf_opts.height != "100%" && fzf_opts.height != "100";
    if use_alternate_screen {
        execute!(std::io::stdout(), EnterAlternateScreen)?;
    }

    let skim_output = Skim::run_items(options, items).map_err(|e| {
        crate::cli::error::CliError::CommandFailed(format!("Skim failed: {}", e))
    });

    if use_alternate_screen {
        execute!(std::io::stdout(), LeaveAlternateScreen)?;
    }

    let output = match skim_output {
        Ok(output) => output,
        Err(e) => {
            reset_terminal_state(stdout);
            return Err(e);
        }
    };

    debug!("Final key: {:?}", output.final_key);

    if output.is_abort {
        debug!("Selection aborted");
        reset_terminal_state(stdout);
        return Ok(());
    }

    let selected_bookmarks = get_selected_bookmarks(&output, bookmarks);

    if selected_bookmarks.is_empty() {
        debug!("No bookmarks selected");
        return Ok(());
    }

    let ids: Vec<i32> = selected_bookmarks.iter().filter_map(|bm| bm.id).collect();
    debug!("Selected bookmark IDs: {:?}", ids);

    println!();

    let key = output.final_key;
    match (key.code, key.modifiers) {
        (KeyCode::Enter, _) => {
            if stdout {
                for bookmark in &selected_bookmarks {
                    let content = services
                        .interpolation_service
                        .render_bookmark_url(bookmark)
                        .map_err(|e| {
                            crate::cli::error::CliError::CommandFailed(format!(
                                "Failed to render content: {}",
                                e
                            ))
                        })?;
                    println!("{}", content);
                }
            } else {
                for bookmark in &selected_bookmarks {
                    execute_bookmark_default_action(bookmark, services.action_service.clone())?;
                }
            }
        }
        (KeyCode::Char('y'), m) | (KeyCode::Char('o'), m) if m.contains(KeyModifiers::CONTROL) => {
            if let Some(bookmark) = selected_bookmarks.first() {
                let is_shell_script = bookmark
                    .tags
                    .iter()
                    .any(|tag| tag.is_system_tag_of(SystemTag::Shell));

                if is_shell_script {
                    let command = format!("bkmr open --no-edit {} --", bookmark.id.unwrap_or(0));
                    copy_url_to_clipboard(&command, services.clipboard_service.clone())?;
                } else {
                    copy_bookmark_url_to_clipboard(
                        bookmark,
                        services.interpolation_service.clone(),
                        services.clipboard_service.clone(),
                    )?;
                }
            }
        }
        (KeyCode::Char('e'), m) if m.contains(KeyModifiers::CONTROL) => {
            edit_bookmarks(
                ids,
                false,
                services.bookmark_service.clone(),
                services.template_service.clone(),
                settings,
            )?;
        }
        (KeyCode::Char('d'), m) if m.contains(KeyModifiers::CONTROL) => {
            delete_bookmarks(ids, services.bookmark_service.clone(), settings)?;
        }
        (KeyCode::Char('a'), m) if m.contains(KeyModifiers::CONTROL) => {
            if let Some(bookmark) = selected_bookmarks.first() {
                if let Some(id) = bookmark.id {
                    clone_bookmark(
                        id,
                        services.bookmark_service.clone(),
                        services.template_service.clone(),
                    )?;
                }
            }
        }
        (KeyCode::Char('p'), m) if m.contains(KeyModifiers::CONTROL) => {
            if let Some(bookmark) = selected_bookmarks.first() {
                let _ = execute!(std::io::stdout(), Clear(ClearType::FromCursorDown));
                let details = bookmark_commands::show_bookmark_details(bookmark, services);
                print!("{}", details);
            }
        }
        _ => {
            debug!("Unhandled key: {:?}", key);
        }
    }

    reset_terminal_state(stdout);

    Ok(())
}

/// Reset terminal state after skim exit.
/// Skipped in --stdout mode to avoid polluting captured output.
fn reset_terminal_state(skip: bool) {
    if skip {
        return;
    }
    let mut stdout = std::io::stdout();
    let _ = execute!(
        stdout,
        crossterm::style::ResetColor,
        crossterm::cursor::Show
    );
    let _ = stdout.flush();
}