cliban 0.16.0

Self-hosted, agent-first kanban for the terminal: flat CLI, ratatui board, SQLite storage
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
//! Fuzzy search over issues. The scorer implements the
//! `github.com/sahilm/fuzzy` v0.1.2 algorithm, so per-field scores — and
//! therefore the summed `score` and the result order — are stable across
//! releases.

use cliban_core::contexts::issues::{self, ListOpts};
use cliban_core::contexts::milestones;
use cliban_core::schema::Issue;
use cliban_core::Store;

use crate::errors::{CliError, CliResult};

// ---- sahilm/fuzzy port (v0.1.2 fuzzy.go) ----

const FIRST_CHAR_MATCH_BONUS: i64 = 10;
const MATCH_FOLLOWING_SEPARATOR_BONUS: i64 = 20;
const CAMEL_CASE_MATCH_BONUS: i64 = 20;
const ADJACENT_MATCH_BONUS: i64 = 5;
const UNMATCHED_LEADING_CHAR_PENALTY: i64 = -5;
const MAX_UNMATCHED_LEADING_CHAR_PENALTY: i64 = -15;

const SEPARATORS: &[char] = &['/', '-', '_', ' ', '.', '\\'];

fn is_separator(c: char) -> bool {
    SEPARATORS.contains(&c)
}

/// ASCII-leaning case-insensitive rune comparison: exact for ASCII, simple
/// case folding for the rest — which covers the rune pairs this code
/// actually compares.
fn equal_fold(tr: char, sr: char) -> bool {
    if tr == sr {
        return true;
    }
    // Order so that `tr >= sr`.
    let (tr, sr) = if tr < sr { (sr, tr) } else { (tr, sr) };
    if (tr as u32) < 0x80 {
        // ASCII, sr is upper case → tr must be lower case.
        if sr.is_ascii_uppercase() && tr == ((sr as u8) + b'a' - b'A') as char {
            return true;
        }
        return false;
    }
    // General (non-ASCII) case: compare simple-folded forms. Full Unicode
    // unicode.SimpleFold iteration; for the inputs cliban deals with this
    // lowercase-comparison is equivalent for matching purposes.
    tr.to_lowercase().eq(sr.to_lowercase())
}

fn adjacent_char_bonus(i: usize, last_match: usize, current_bonus: i64) -> i64 {
    if last_match == i {
        current_bonus * 2 + ADJACENT_MATCH_BONUS
    } else {
        0
    }
}

/// Port of sahilm/fuzzy's per-string match (the body of `FindFromNoSort`'s
/// loop, specialized to a single candidate string). Returns `(score,
/// matched_byte_offsets)` when every pattern rune is matched in order, else
/// `None`.
///
/// Indexes are byte offsets into `text`.
pub fn fuzzy_find(pattern: &str, text: &str) -> Option<(i64, Vec<usize>)> {
    if pattern.is_empty() {
        return None;
    }
    let runes: Vec<char> = pattern.chars().collect();

    // Limit matching to the first NUL rune, if any.
    let clean: &str = match text.find('\0') {
        Some(i) => &text[..i],
        None => text,
    };

    // Pre-decode candidate runes with their byte offsets for the `next`
    // look-ahead below.
    let chars: Vec<(usize, char)> = clean.char_indices().collect();
    let n = chars.len();

    let mut matched_indexes: Vec<usize> = Vec::with_capacity(runes.len());
    let mut total_score: i64 = 0;

    let mut score: i64;
    let mut pattern_index = 0usize;
    let mut best_score: i64 = -1;
    let mut matched_index: i64 = -1;
    let mut curr_adjacent_match_bonus: i64 = 0;
    let mut last: char = '\0';
    let mut have_last = false;
    let mut last_index: usize = 0;

    for idx in 0..n {
        let (j, candidate) = chars[idx];

        if equal_fold(candidate, runes[pattern_index]) {
            score = 0;
            if j == 0 {
                score += FIRST_CHAR_MATCH_BONUS;
            }
            if have_last && last.is_lowercase() && candidate.is_uppercase() {
                score += CAMEL_CASE_MATCH_BONUS;
            }
            if j != 0 && have_last && is_separator(last) {
                score += MATCH_FOLLOWING_SEPARATOR_BONUS;
            }
            if let Some(&last_match) = matched_indexes.last() {
                let bonus = adjacent_char_bonus(last_index, last_match, curr_adjacent_match_bonus);
                score += bonus;
                curr_adjacent_match_bonus += bonus;
            }
            if score > best_score {
                best_score = score;
                matched_index = j as i64;
            }
        }

        // Determine the next pattern rune and the next candidate rune (the
        // look-ahead compares byte values; for matching purposes the decoded
        // rune is what matters).
        let nextp: char = if pattern_index < runes.len() - 1 {
            runes[pattern_index + 1]
        } else {
            '\0'
        };
        let nextc: char = if idx + 1 < n { chars[idx + 1].1 } else { '\0' };

        if (equal_fold(nextp, nextc) || nextc == '\0') && matched_index > -1 {
            if matched_indexes.is_empty() {
                let penalty = matched_index * UNMATCHED_LEADING_CHAR_PENALTY;
                best_score += penalty.max(MAX_UNMATCHED_LEADING_CHAR_PENALTY);
            }
            total_score += best_score;
            matched_indexes.push(matched_index as usize);
            best_score = -1;
            matched_index = -1;
            pattern_index += 1;
            if pattern_index >= runes.len() {
                // All pattern runes consumed. The loop continues to the
                // end of the string but never matches again (pattern_index
                // out of range); replicate by breaking after the final
                // per-char penalty accounting below.
                // We still must apply the trailing unmatched-char penalty,
                // handled after the loop.
            }
        }

        last_index = j;
        last = candidate;
        have_last = true;

        if pattern_index >= runes.len() {
            break;
        }
    }

    // Apply penalty for each unmatched character: len(matched) - len(clean
    // runes).
    let penalty = matched_indexes.len() as i64 - n as i64;
    total_score += penalty;

    if matched_indexes.len() == runes.len() {
        Some((total_score, matched_indexes))
    } else {
        None
    }
}

// ---- description stripping ----

const MAX_DESC_BYTES: usize = 4096;

/// Port of `stripDescription`: collapse fenced code blocks to a single space,
/// drop heading markers, unwrap `[text](url)` to `text`, cap at 4096 bytes.
pub fn strip_description(s: &str) -> String {
    use std::sync::OnceLock;
    static RE_FENCE: OnceLock<regex::Regex> = OnceLock::new();
    static RE_HEADING: OnceLock<regex::Regex> = OnceLock::new();
    static RE_LINK: OnceLock<regex::Regex> = OnceLock::new();

    // (?s)```.*?``` — DOTALL, non-greedy fenced block.
    let re_fence = RE_FENCE.get_or_init(|| regex::Regex::new(r"(?s)```.*?```").unwrap());
    // (?m)^#+\s* — heading markers at line start.
    let re_heading = RE_HEADING.get_or_init(|| regex::Regex::new(r"(?m)^#+\s*").unwrap());
    // \[([^\]]+)\]\([^)]+\) — markdown link.
    let re_link = RE_LINK.get_or_init(|| regex::Regex::new(r"\[([^\]]+)\]\([^)]+\)").unwrap());

    let s = re_fence.replace_all(s, " ");
    let s = re_heading.replace_all(&s, "");
    let s = re_link.replace_all(&s, "$1");
    let mut s = s.into_owned();
    if s.len() > MAX_DESC_BYTES {
        // Byte-truncate, then back off to a char boundary — fuzzy matching
        // is unaffected by a dropped partial rune.
        let mut cut = MAX_DESC_BYTES;
        while cut > 0 && !s.is_char_boundary(cut) {
            cut -= 1;
        }
        s.truncate(cut);
    }
    s
}

// ---- weighted multi-field search ----

const WEIGHT_TITLE: i64 = 30;
const WEIGHT_KEY: i64 = 25;
const WEIGHT_LABEL: i64 = 20;
const WEIGHT_DESC: i64 = 10;

/// A single search hit: the matched issue plus its summed weighted score.
pub struct Match {
    pub issue: Issue,
    pub score: i64,
}

/// Options for [`search`]. Each filter field is
/// the resolved (parsed/normalized) value, an empty option meaning "no filter".
pub struct Options {
    pub query: String,
    pub project: Option<String>,
    pub label: Vec<String>,
    pub milestone: Option<String>,
    pub status: Option<String>,
    pub priority: Option<String>,
    pub parent: Option<String>,
    pub include_archived: bool,
    pub exclude_subs: bool,
    pub limit: i64,
}

/// Run a fuzzy search according to `opts`. Fetches the candidate set with the
/// same project/status/priority/milestone/label/parent/no-subs/archived
/// filters as `issue ls`, scores each candidate's title/key/labels/description
/// against the (trimmed) query, keeps any issue with ≥1 matched field, and
/// sorts by score desc with updated_at desc as the tiebreak. An empty query
/// returns every candidate sorted by updated_at desc with score 0.
pub async fn search(store: &Store, opts: Options) -> CliResult<Vec<Match>> {
    // ---- candidate fetch + filtering (mirrors `issue ls`) ----
    let project = opts.project.clone().filter(|p| !p.is_empty());
    let status = opts.status.clone().filter(|s| !s.is_empty());
    let milestone = opts.milestone.clone().filter(|m| !m.is_empty());
    let priority = opts.priority.clone().filter(|p| !p.is_empty());
    let parent_key = opts.parent.clone().filter(|p| !p.is_empty());

    let list_project = project.clone();
    let list_status = status.clone();
    let list_milestone = milestone.clone();
    let include_archived = opts.include_archived;
    let mut issues = store
        .call(move |conn| {
            let mut out = issues::list(
                conn,
                ListOpts {
                    project: list_project.as_deref(),
                    status: list_status.as_deref(),
                    milestone: list_milestone.as_deref(),
                    archived: false,
                },
            )?;
            if include_archived {
                let archived = issues::list(
                    conn,
                    ListOpts {
                        project: list_project.as_deref(),
                        status: list_status.as_deref(),
                        milestone: list_milestone.as_deref(),
                        archived: true,
                    },
                )?;
                out.extend(archived);
            }
            Ok(out)
        })
        .await?;

    if let Some(pr) = &priority {
        issues.retain(|i| &i.priority == pr);
    }
    if let Some(pk) = &parent_key {
        // Parse the parent key up front so a malformed key errors early.
        let parsed = crate::cmd::issue::parse_issue_key_pub(pk)
            .map_err(|e| CliError::other(format!("parent key {pk:?}: {}", e.message())))?;
        let lookup = parsed.clone();
        let parent_id = store
            .call(move |conn| issues::get_by_key(conn, &lookup).map(|o| o.map(|i| i.id)))
            .await?;
        match parent_id {
            Some(pid) => issues.retain(|i| i.parent_id == Some(pid)),
            None => issues.clear(),
        }
    }
    if opts.exclude_subs {
        issues.retain(|i| i.parent_id.is_none());
    }
    if !opts.label.is_empty() {
        let want = opts.label.clone();
        let mut kept = Vec::with_capacity(issues.len());
        for i in issues.into_iter() {
            let id = i.id;
            let names = store
                .call(move |conn| issues::label_names(conn, id))
                .await?;
            if want.iter().all(|w| names.iter().any(|n| n == w)) {
                kept.push(i);
            }
        }
        issues = kept;
    }

    // Establish the base input ordering before the stable result sort.
    crate::cmd::issue::base_order(&mut issues);

    let q = opts.query.trim();

    let mut matches: Vec<Match>;
    if q.is_empty() {
        matches = issues
            .into_iter()
            .map(|issue| Match { issue, score: 0 })
            .collect();
        // stable sort by updated_at desc
        matches.sort_by_key(|m| std::cmp::Reverse(m.issue.updated_at));
    } else {
        // Resolve labels per issue (one lookup each — same result as a bulk path
        // result, just N round-trips on our writer thread).
        matches = Vec::with_capacity(issues.len());
        for issue in issues.into_iter() {
            let id = issue.id;
            let labels = store
                .call(move |conn| issues::label_names(conn, id))
                .await?;
            let label_str = labels.join(" ");
            let desc = strip_description(&issue.description);

            let fields: [(&str, i64); 4] = [
                (issue.title.as_str(), WEIGHT_TITLE),
                (issue.key.as_str(), WEIGHT_KEY),
                (label_str.as_str(), WEIGHT_LABEL),
                (desc.as_str(), WEIGHT_DESC),
            ];

            let mut total = 0i64;
            let mut matched = false;
            for (text, weight) in fields {
                if text.is_empty() {
                    continue;
                }
                if let Some((s, _idx)) = fuzzy_find(q, text) {
                    total += s * weight;
                    matched = true;
                }
            }
            if matched {
                matches.push(Match {
                    issue,
                    score: total,
                });
            }
        }
        // stable sort: score desc, updated_at desc tiebreak
        matches.sort_by(|a, b| {
            b.score
                .cmp(&a.score)
                .then_with(|| b.issue.updated_at.cmp(&a.issue.updated_at))
        });
    }

    if opts.limit > 0 && matches.len() > opts.limit as usize {
        matches.truncate(opts.limit as usize);
    }
    Ok(matches)
}

/// Resolve milestone name + parent key for an issue (empty when unset). Local
/// copy so `search` does not depend on the `cmd::issue` module's private
/// helper.
pub async fn resolve_refs(store: &Store, issue: &Issue) -> CliResult<(String, String)> {
    let milestone_id = issue.milestone_id;
    let parent_id = issue.parent_id;
    let pair = store
        .call(move |conn| {
            let milestone = match milestone_id {
                Some(mid) => milestones::get_by_id(conn, mid)?.map(|m| m.name),
                None => None,
            };
            let parent = match parent_id {
                Some(pid) => issues::get_by_id(conn, pid)?.map(|i| i.key),
                None => None,
            };
            Ok((milestone.unwrap_or_default(), parent.unwrap_or_default()))
        })
        .await?;
    Ok(pair)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fuzzy_no_match_returns_none() {
        assert!(fuzzy_find("xyz", "abc").is_none());
        assert!(fuzzy_find("abcd", "abc").is_none()); // pattern longer
        assert!(fuzzy_find("", "abc").is_none());
    }

    #[test]
    fn fuzzy_exhaustive_picks_best_k() {
        // From the sahilm/fuzzy doc: "tk" against "The Black Knight" should
        // match the second k (the K after a separator) for a higher score.
        let (score, idx) = fuzzy_find("tk", "The Black Knight").unwrap();
        // 't' at 0 (first-char bonus 10, leading penalty 0); 'K' at index 10
        // follows a separator (bonus 20). Verify the K chosen is the Knight K.
        assert_eq!(idx[0], 0);
        assert_eq!(idx[1], 10);
        assert!(score > 0, "score {score}");
    }

    #[test]
    fn fuzzy_first_char_and_full_word() {
        // "search" fully matching "search" → strong positive score.
        let (full, _) = fuzzy_find("search", "search").unwrap();
        let (partial, _) = fuzzy_find("srch", "search").unwrap();
        assert!(full > partial, "full {full} partial {partial}");
    }

    #[test]
    fn strip_collapses_fences_headings_links() {
        let s = strip_description("## Spec\nhello [text](http://x)\n```\ncode\n```\nbye");
        assert!(!s.contains("##"));
        assert!(!s.contains("```"));
        assert!(!s.contains("http"));
        assert!(s.contains("text"));
        assert!(s.contains("hello") && s.contains("bye"));
    }

    #[test]
    fn strip_caps_at_4096_bytes() {
        let big = "a".repeat(5000);
        assert_eq!(strip_description(&big).len(), 4096);
    }
}