linear-tui 0.13.0

A TUI client for Linear.app — manage issues, projects, and cycles from your terminal
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
//! Turning the names an agent types into what Linear knows: a person, a
//! label, a project, a cycle, a parent issue. Names resolve within one
//! team — the issue's own, or the team an issue is filed in — in any case.
//! A name that matches nothing lists what would; one that matches more than
//! one thing lists those.

use anyhow::{Result, bail};

use super::Linear;
use crate::core::entity::timestamp::parse_timestamp;
use crate::core::entity::{
    CustomView, Cycle, IssueRef, Label, Milestone, Priority, Project, ProjectId, ProjectStatus,
    Team, TeamId, User,
};
use crate::core::message::Message;
use crate::core::store::Store;
use crate::core::usecase;

/// A value given as `none` empties the field.
pub fn is_none(text: &str) -> bool {
    text.trim().eq_ignore_ascii_case("none")
}

/// The one of `items` that `wanted` names, by any of its `names`. `what`
/// names the kind of thing in the error, and `label` each candidate.
pub fn pick<'a, T>(
    what: &str,
    wanted: &str,
    items: &'a [T],
    names: impl Fn(&T) -> Vec<String>,
    label: impl Fn(&T) -> String,
) -> Result<&'a T> {
    let wanted = wanted.trim();
    let found: Vec<&T> = items
        .iter()
        .filter(|item| names(item).iter().any(|n| n.eq_ignore_ascii_case(wanted)))
        .collect();
    match found.as_slice() {
        [one] => Ok(one),
        [] => {
            let mut choices: Vec<String> = items.iter().map(&label).collect();
            choices.dedup();
            if choices.is_empty() {
                bail!("no {what} {wanted}: this team has none")
            }
            bail!("no {what} {wanted} (choices: {})", choices.join(", "))
        }
        many => {
            let candidates: Vec<String> = many.iter().map(|item| label(item)).collect();
            bail!(
                "{what} {wanted} is ambiguous: it could be {}",
                candidates.join(", ")
            )
        }
    }
}

pub fn priority(level: &str) -> Result<Priority> {
    Priority::ALL
        .into_iter()
        .find(|p| p.label().eq_ignore_ascii_case(level.trim()))
        .ok_or_else(|| {
            anyhow::anyhow!("no priority {level} (choices: urgent, high, medium, low, none)")
        })
}

/// `3`, or `none`.
pub fn estimate(text: &str) -> Result<Option<u32>> {
    if is_none(text) {
        return Ok(None);
    }
    text.trim()
        .parse()
        .map(Some)
        .map_err(|_| anyhow::anyhow!("not an estimate: {text} (a whole number, or none)"))
}

fn person_name(user: &User) -> String {
    user.display_name
        .clone()
        .unwrap_or_else(|| user.name.clone())
}

/// What a team offers to fill an issue's fields with, fetched as asked for.
pub struct InTeam<'a, L: Linear> {
    linear: &'a L,
    pub id: TeamId,
    members: Option<Vec<User>>,
}

impl<'a, L: Linear> InTeam<'a, L> {
    pub fn new(linear: &'a L, id: TeamId) -> Self {
        Self {
            linear,
            id,
            members: None,
        }
    }

    async fn members(&mut self) -> Result<&[User]> {
        if self.members.is_none() {
            let request = usecase::team::Request::Context {
                team_id: self.id.clone(),
            };
            let Message::TeamContext { members, .. } = self.linear.run(request).await? else {
                bail!("unexpected answer to a team request");
            };
            self.members = Some(members);
        }
        Ok(self.members.as_deref().unwrap_or_default())
    }

    /// `me`, a member of the team by name, display name, or email, or
    /// `none` for nobody.
    pub async fn assignee(&mut self, text: &str) -> Result<Option<User>> {
        if is_none(text) {
            return Ok(None);
        }
        if text.trim().eq_ignore_ascii_case("me") {
            let Message::Viewer { id, .. } = self.linear.run(usecase::user::load()).await? else {
                bail!("unexpected answer to a viewer request");
            };
            let me = self.members().await?.iter().find(|u| u.id == id).cloned();
            return Ok(Some(me.unwrap_or(User {
                id,
                name: "me".into(),
                email: None,
                display_name: None,
            })));
        }
        let members = self.members().await?;
        let user = pick(
            "member",
            text,
            members,
            |u| {
                let mut names = vec![u.name.clone()];
                names.extend(u.display_name.clone());
                names.extend(u.email.clone());
                names
            },
            |u| match &u.email {
                Some(email) => format!("{} <{email}>", person_name(u)),
                None => person_name(u),
            },
        )?;
        Ok(Some(user.clone()))
    }

    /// Labels by name: the team's own and the workspace's.
    pub async fn labels(&self, names: &[&str]) -> Result<Vec<Label>> {
        if names.is_empty() {
            return Ok(Vec::new());
        }
        let request = usecase::team::load_labels(self.id.clone());
        let Message::Labels { labels, team_id } = self.linear.run(request).await? else {
            bail!("unexpected answer to a labels request");
        };
        if team_id != self.id {
            bail!("Linear answered with another team's labels");
        }
        names
            .iter()
            .map(|name| {
                pick(
                    "label",
                    name,
                    &labels,
                    |l| vec![l.name.clone()],
                    |l| l.name.clone(),
                )
                .cloned()
            })
            .collect()
    }

    /// A project of the team by name, or `none`.
    pub async fn project(&self, text: &str) -> Result<Option<Project>> {
        if is_none(text) {
            return Ok(None);
        }
        let open = usecase::project::open_team_projects(&mut Store::default(), self.id.clone());
        let Some(request) = open.request() else {
            bail!("the team's projects could not be asked for");
        };
        let Message::Projects { page, .. } = self.linear.run(request).await? else {
            bail!("unexpected answer to a projects request");
        };
        let project = pick(
            "project",
            text,
            &page.items,
            |p| vec![p.name.clone()],
            |p| p.name.clone(),
        )?;
        Ok(Some(project.clone()))
    }

    /// A cycle of the team by name or number, `current` for the one under
    /// way at `now` (seconds since the epoch), or `none`.
    pub async fn cycle(&self, text: &str, now: u64) -> Result<Option<Cycle>> {
        if is_none(text) {
            return Ok(None);
        }
        let open = usecase::cycle::open_team_cycles(&mut Store::default(), self.id.clone());
        let Some(request) = open.request() else {
            bail!("the team's cycles could not be asked for");
        };
        let Message::Cycles { page, .. } = self.linear.run(request).await? else {
            bail!("unexpected answer to a cycles request");
        };
        if text.trim().eq_ignore_ascii_case("current") {
            return current_cycle(&page.items, now)
                .cloned()
                .map(Some)
                .ok_or_else(|| anyhow::anyhow!("the team has no cycle under way"));
        }
        let cycle = pick("cycle", text, &page.items, cycle_names, Cycle::label)?;
        Ok(Some(cycle.clone()))
    }
}

fn cycle_names(cycle: &Cycle) -> Vec<String> {
    let mut names = vec![cycle.label()];
    names.extend(cycle.name.clone());
    if let Some(number) = cycle.number {
        names.push(format!("{number}"));
        names.push(format!("Cycle {number}"));
    }
    names
}

/// The cycle whose dates hold `now`.
pub fn current_cycle(cycles: &[Cycle], now: u64) -> Option<&Cycle> {
    // Linear writes `2026-09-26T00:00:00.000Z`; the seconds are enough.
    let at = |text: &Option<String>| {
        let text = text.as_deref()?;
        parse_timestamp(&format!("{}Z", text.get(..19)?))
    };
    cycles.iter().find(|c| {
        matches!((at(&c.starts_at), at(&c.ends_at)), (Some(start), Some(end)) if start <= now && now < end)
    })
}

/// The issue `key` names, as a parent: `none` for no parent.
pub async fn parent(linear: &impl Linear, key: &str) -> Result<Option<IssueRef>> {
    if is_none(key) {
        return Ok(None);
    }
    let issue = super::issue::fetch(linear, key).await?;
    Ok(Some(IssueRef {
        id: issue.id,
        identifier: issue.identifier,
        title: issue.title,
        state: issue.state,
    }))
}

/// Every team the user is in.
pub async fn teams(linear: &impl Linear) -> Result<Vec<Team>> {
    let Message::Teams(teams) = linear.run(usecase::team::load()).await? else {
        bail!("unexpected answer to a teams request");
    };
    Ok(teams)
}

/// A team by key or name.
pub fn team<'a>(teams: &'a [Team], wanted: &str) -> Result<&'a Team> {
    pick(
        "team",
        wanted,
        teams,
        |t| vec![t.key.clone(), t.name.clone()],
        |t| t.key.clone(),
    )
}

/// A team of the user's by key or name, fetched.
pub async fn one_team(linear: &impl Linear, wanted: &str) -> Result<Team> {
    Ok(team(&teams(linear).await?, wanted)?.clone())
}

/// A project anywhere in the workspace, by name in any case or by id. Two
/// projects of one name are told apart by their teams.
pub async fn project(linear: &impl Linear, wanted: &str) -> Result<Project> {
    let request = usecase::project::find(wanted)?;
    let Message::ProjectsFound { projects, name } = linear.run(request).await? else {
        bail!("unexpected answer to a project search");
    };
    let teams_of = |p: &Project| {
        p.teams.as_ref().map_or(String::new(), |t| {
            let keys: Vec<&str> = t.nodes.iter().map(|t| t.key.as_str()).collect();
            format!(" ({})", keys.join(", "))
        })
    };
    match projects.as_slice() {
        [one] => return Ok(one.clone()),
        [] => {}
        many => {
            let candidates: Vec<String> = many
                .iter()
                .map(|p| format!("{}{} — id {}", p.name, teams_of(p), p.id))
                .collect();
            bail!(
                "project {wanted} is ambiguous: it could be {}; name it by id",
                candidates.join(", ")
            )
        }
    }
    // Not a name: perhaps an id.
    let looks_like_id = wanted.len() >= 8
        && wanted
            .trim()
            .chars()
            .all(|c| c.is_ascii_hexdigit() || c == '-');
    if looks_like_id {
        return project_detail(linear, &ProjectId::new(wanted.trim())).await;
    }
    bail!("no project {name} in this workspace")
}

/// A project read with its teams and milestones.
pub async fn project_detail(linear: &impl Linear, id: &ProjectId) -> Result<Project> {
    let Message::ProjectDetail(project) = linear.run(usecase::project::open(id.clone())).await?
    else {
        bail!("unexpected answer to a project request");
    };
    Ok(*project)
}

/// A milestone of `project` (read with its milestones) by name or id, or
/// `none`.
pub fn milestone(project: &Project, wanted: &str) -> Result<Option<Milestone>> {
    if is_none(wanted) {
        return Ok(None);
    }
    let milestones = project
        .milestones
        .as_ref()
        .map_or(&[][..], |m| m.nodes.as_slice());
    let found = pick(
        "milestone",
        wanted,
        milestones,
        |m| vec![m.name.clone(), m.id.to_string()],
        |m| m.name.clone(),
    )
    .map_err(|e| anyhow::anyhow!("{e}, in project {}", project.name))?;
    Ok(Some(found.clone()))
}

/// One of the workspace's project statuses, by name.
pub async fn project_status(linear: &impl Linear, wanted: &str) -> Result<ProjectStatus> {
    let request = usecase::project::load_statuses();
    let Message::ProjectStatuses(statuses) = linear.run(request).await? else {
        bail!("unexpected answer to a project statuses request");
    };
    Ok(pick(
        "project status",
        wanted,
        &statuses,
        |s| vec![s.name.clone()],
        |s| s.name.clone(),
    )?
    .clone())
}

/// A date as Linear takes it, `YYYY-MM-DD`, or `none`.
pub fn date(text: &str) -> Result<Option<String>> {
    if is_none(text) {
        return Ok(None);
    }
    let text = text.trim();
    let digits = |r: std::ops::Range<usize>| {
        text.get(r)
            .is_some_and(|s| s.chars().all(|c| c.is_ascii_digit()))
    };
    let valid = text.len() == 10
        && digits(0..4)
        && &text[4..5] == "-"
        && digits(5..7)
        && &text[7..8] == "-"
        && digits(8..10)
        && parse_timestamp(&format!("{text}T00:00:00Z")).is_some();
    if !valid {
        bail!("not a date: {text} (YYYY-MM-DD, or none)");
    }
    Ok(Some(text.to_string()))
}

/// Every saved view the user can open, in Linear's order.
pub async fn views(linear: &impl Linear) -> Result<Vec<CustomView>> {
    let mut store = Store::default();
    let request = usecase::view::reload_views(&mut store);
    let Message::CustomViews(views) = linear.run(request).await? else {
        bail!("unexpected answer to a views request");
    };
    usecase::view::take_views(&mut store, views);
    Ok(store.custom_views)
}

/// A saved view by name: one listing issues, or one listing projects.
pub async fn view(linear: &impl Linear, wanted: &str, issues: bool) -> Result<CustomView> {
    let views: Vec<CustomView> = views(linear)
        .await?
        .into_iter()
        .filter(|v| v.lists_issues() == issues)
        .collect();
    let what = if issues { "issue view" } else { "project view" };
    Ok(pick(
        what,
        wanted,
        &views,
        |v| vec![v.name.clone()],
        |v| v.name.clone(),
    )?
    .clone())
}

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

    #[test]
    fn dates_are_calendar_days_or_none() {
        assert_eq!(date("2026-12-01").unwrap().as_deref(), Some("2026-12-01"));
        assert_eq!(date("none").unwrap(), None);
        assert!(date("2026-13-01").is_err());
        assert!(date("12/01/2026").is_err());
    }

    fn labels() -> Vec<Label> {
        serde_json::from_value(serde_json::json!([
            { "id": "l1", "name": "Bug" },
            { "id": "l2", "name": "bug" },
            { "id": "l3", "name": "Feature" },
        ]))
        .unwrap()
    }

    fn by_name(l: &Label) -> Vec<String> {
        vec![l.name.clone()]
    }

    #[test]
    fn a_name_matches_in_any_case() {
        let labels = labels();
        let found = pick("label", "feature", &labels, by_name, |l| l.name.clone()).unwrap();
        assert_eq!(found.id, "l3");
    }

    #[test]
    fn an_unknown_name_lists_the_choices() {
        let labels = labels();
        let err = pick("label", "Chore", &labels, by_name, |l| l.name.clone()).unwrap_err();
        assert_eq!(
            err.to_string(),
            "no label Chore (choices: Bug, bug, Feature)"
        );
    }

    #[test]
    fn an_ambiguous_name_lists_the_candidates() {
        let labels = labels();
        let err = pick("label", "BUG", &labels, by_name, |l| l.name.clone()).unwrap_err();
        assert_eq!(
            err.to_string(),
            "label BUG is ambiguous: it could be Bug, bug"
        );
    }

    #[test]
    fn priorities_and_estimates_parse_or_say_what_is_valid() {
        assert_eq!(priority("LOW").unwrap(), Priority::Low);
        assert!(
            priority("huge")
                .unwrap_err()
                .to_string()
                .contains("urgent, high")
        );
        assert_eq!(estimate("3").unwrap(), Some(3));
        assert_eq!(estimate("none").unwrap(), None);
        assert!(estimate("two").is_err());
    }

    #[test]
    fn the_current_cycle_is_the_one_under_way() {
        let cycles: Vec<Cycle> = serde_json::from_value(serde_json::json!([
            { "id": "c1", "number": 1, "startsAt": "2026-09-01T00:00:00.000Z", "endsAt": "2026-09-15T00:00:00.000Z" },
            { "id": "c2", "number": 2, "startsAt": "2026-09-15T00:00:00.000Z", "endsAt": "2026-09-29T00:00:00.000Z" },
        ]))
        .unwrap();
        let now = parse_timestamp("2026-09-26T12:00:00Z").unwrap();
        assert_eq!(current_cycle(&cycles, now).unwrap().id, "c2");
        let later = parse_timestamp("2026-10-26T12:00:00Z").unwrap();
        assert!(current_cycle(&cycles, later).is_none());
        let cycle = pick("cycle", "cycle 1", &cycles, cycle_names, Cycle::label).unwrap();
        assert_eq!(cycle.id, "c1");
    }
}