twc-rs 4.0.4

Fast single-binary CLI and interactive TUI dashboard for Timeweb Cloud
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
// SPDX-FileCopyrightText: 2026 RAprogramm <andrey.rozanov.vl@gmail.com>
// SPDX-License-Identifier: MIT

use std::fmt;

use chrono::{DateTime, FixedOffset, Local, NaiveDate, TimeZone};
use rust_i18n::t;
use tabled::Tabled;
use timeweb_rs::{
    apis::{apps_api, configuration::Configuration},
    models
};

use crate::{error::TwcError, output::OutputFormat};

/// Formats a float identifier for display.
fn fmt_id<T: std::fmt::Display>(v: T) -> String {
    v.to_string()
}

/// Compact row for the app list table.
#[derive(Tabled)]
struct AppRow {
    #[tabled(rename = "ID")]
    id:       String,
    #[tabled(rename = "Name")]
    name:     String,
    #[tabled(rename = "Status")]
    status:   String,
    #[tabled(rename = "IP")]
    ip:       String,
    #[tabled(rename = "Location")]
    location: String
}

impl fmt::Display for AppRow {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{} {} {} {} {}",
            self.id, self.name, self.status, self.ip, self.location
        )
    }
}

/// Lists all apps.
///
/// # Overview
///
/// Fetches all apps from the Timeweb Cloud API and displays
/// them in the requested output format.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn list(config: &Configuration, format: OutputFormat) -> Result<(), TwcError> {
    let resp = apps_api::get_apps(config).await?;

    let rows: Vec<AppRow> = resp
        .apps
        .iter()
        .map(|a| AppRow {
            id:       fmt_id(a.id),
            name:     a.name.clone(),
            status:   format!("{:?}", a.status),
            ip:       a.ip.clone().unwrap_or_default(),
            location: a.location.clone().unwrap_or_default()
        })
        .collect();

    match format {
        OutputFormat::Table => {
            if rows.is_empty() {
                println!("{}", t!("cli.no_apps"));
            } else {
                let table = crate::output::render_table(&rows);
                println!("{table}");
            }
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            let out = crate::output::serialized(format, &resp.apps)
                .transpose()?
                .unwrap_or_default();
            println!("{out}");
        }
        OutputFormat::Quiet => {
            for a in &resp.apps {
                println!("{}\t{}", fmt_id(a.id), a.name);
            }
        }
    }
    Ok(())
}

/// Resolves an app selector — a numeric ID or an exact app name — to an ID.
///
/// Numeric selectors pass through without a network round-trip; anything else
/// is matched against the names returned by the apps list endpoint.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network failures, when no app carries the
/// name, or when several apps share it.
pub async fn resolve_app(config: &Configuration, selector: &str) -> Result<String, TwcError> {
    if is_numeric_selector(selector) {
        return Ok(selector.to_owned());
    }
    let resp = apps_api::get_apps(config).await?;
    let apps: Vec<(String, String)> = resp
        .apps
        .iter()
        .map(|a| (fmt_id(a.id), a.name.clone()))
        .collect();
    match_app_selector(&apps, selector)
}

/// Returns `true` when the selector consists solely of ASCII digits.
fn is_numeric_selector(selector: &str) -> bool {
    !selector.is_empty() && selector.bytes().all(|b| b.is_ascii_digit())
}

/// Picks the single app whose name equals `selector` from `(id, name)` pairs.
///
/// # Errors
///
/// Returns [`TwcError::Api`] when the name matches zero or several apps.
fn match_app_selector(apps: &[(String, String)], selector: &str) -> Result<String, TwcError> {
    let mut ids = apps
        .iter()
        .filter(|(_, name)| name == selector)
        .map(|(id, _)| id.clone());
    let Some(first) = ids.next() else {
        return Err(TwcError::Api(
            t!("cli.app_not_found", name => selector).into_owned()
        ));
    };
    if ids.next().is_some() {
        return Err(TwcError::Api(
            t!("cli.app_ambiguous", name => selector).into_owned()
        ));
    }
    Ok(first)
}

/// Shows detailed info for a single app.
///
/// # Overview
///
/// Fetches an app by its identifier and prints its key fields.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn info(config: &Configuration, id: &str, format: OutputFormat) -> Result<(), TwcError> {
    let resp = apps_api::get_app(config, id).await?;
    let app = &resp.app;

    match format {
        OutputFormat::Table => {
            println!("ID:         {}", fmt_id(app.id));
            println!("Name:       {}", app.name);
            println!("Type:       {:?}", app.r#type);
            println!("Status:     {:?}", app.status);
            println!("IP:         {}", app.ip.clone().unwrap_or_default());
            println!("Location:   {}", app.location.clone().unwrap_or_default());
            println!(
                "Preset ID:  {}",
                app.preset_id.map(fmt_id).unwrap_or_default()
            );
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            if let Some(out) = crate::output::serialized(format, &resp.app) {
                println!("{}", out?);
            }
        }
        OutputFormat::Quiet => {
            println!("{}\t{}", fmt_id(app.id), app.name);
        }
    }
    Ok(())
}

/// Deletes an app by its identifier.
///
/// # Overview
///
/// Sends a delete request for the specified app and prints a confirmation.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn delete(config: &Configuration, id: &str) -> Result<(), TwcError> {
    apps_api::delete_app(config, id).await?;
    println!("{}", t!("cli.app_deleted", id => id));
    Ok(())
}

/// Compact row for the app presets table.
#[derive(Tabled)]
struct PresetRow {
    #[tabled(rename = "ID")]
    id:    String,
    #[tabled(rename = "Kind")]
    kind:  String,
    #[tabled(rename = "CPU")]
    cpu:   String,
    #[tabled(rename = "RAM")]
    ram:   String,
    #[tabled(rename = "Disk")]
    disk:  String,
    #[tabled(rename = "Price")]
    price: String
}

/// Lists available app presets (backend and frontend tariffs).
///
/// # Overview
///
/// Fetches all app presets and renders them in the requested output format.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn list_presets(config: &Configuration, format: OutputFormat) -> Result<(), TwcError> {
    let resp = apps_api::get_apps_presets(config, "").await?;

    let mut rows: Vec<PresetRow> = Vec::new();
    if let Some(backend) = &resp.backend_presets {
        for p in backend {
            rows.push(PresetRow {
                id:    fmt_id(p.id),
                kind:  "backend".to_owned(),
                cpu:   fmt_id(p.cpu),
                ram:   fmt_id(p.ram),
                disk:  fmt_id(p.disk),
                price: fmt_id(p.price)
            });
        }
    }
    if let Some(frontend) = &resp.frontend_presets {
        for p in frontend {
            rows.push(PresetRow {
                id:    fmt_id(p.id),
                kind:  "frontend".to_owned(),
                cpu:   "-".to_owned(),
                ram:   "-".to_owned(),
                disk:  fmt_id(p.disk),
                price: fmt_id(p.price)
            });
        }
    }

    match format {
        OutputFormat::Table => {
            if rows.is_empty() {
                println!("{}", t!("cli.no_app_presets"));
            } else {
                let table = crate::output::render_table(&rows);
                println!("{table}");
            }
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            if let Some(out) = crate::output::serialized(format, &resp) {
                println!("{}", out?);
            }
        }
        OutputFormat::Quiet => {
            for r in &rows {
                println!("{}\t{}", r.id, r.kind);
            }
        }
    }
    Ok(())
}

/// Compact row for the VCS providers table.
#[derive(Tabled)]
struct ProviderRow {
    #[tabled(rename = "Provider ID")]
    provider_id: String,
    #[tabled(rename = "Login")]
    login:       String,
    #[tabled(rename = "Type")]
    kind:        String
}

/// Lists configured VCS providers.
///
/// # Overview
///
/// Fetches VCS providers linked to the account and renders them.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn list_vcs_providers(
    config: &Configuration,
    format: OutputFormat
) -> Result<(), TwcError> {
    let resp = apps_api::get_providers(config).await?;

    let rows: Vec<ProviderRow> = resp
        .providers
        .iter()
        .map(|p| ProviderRow {
            provider_id: fmt_id(p.provider_id),
            login:       p.login.clone(),
            kind:        format!("{:?}", p.provider_type)
        })
        .collect();

    match format {
        OutputFormat::Table => {
            if rows.is_empty() {
                println!("{}", t!("cli.no_vcs_providers"));
            } else {
                let table = crate::output::render_table(&rows);
                println!("{table}");
            }
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            if let Some(out) = crate::output::serialized(format, &resp.providers) {
                println!("{}", out?);
            }
        }
        OutputFormat::Quiet => {
            for p in &resp.providers {
                println!("{}\t{}", fmt_id(p.provider_id), p.login);
            }
        }
    }
    Ok(())
}

/// Compact row for the repositories table.
#[derive(Tabled)]
struct RepoRow {
    #[tabled(rename = "ID")]
    id:        String,
    #[tabled(rename = "Name")]
    name:      String,
    #[tabled(rename = "Full Name")]
    full_name: String,
    #[tabled(rename = "URL")]
    url:       String,
    #[tabled(rename = "Private")]
    private:   String
}

/// Lists repositories available from a connected VCS provider.
///
/// # Overview
///
/// Fetches repositories of the given VCS provider and renders them.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn list_repositories(
    config: &Configuration,
    provider_id: &str,
    format: OutputFormat
) -> Result<(), TwcError> {
    let resp = apps_api::get_repositories(config, provider_id).await?;

    let rows: Vec<RepoRow> = resp
        .repositories
        .iter()
        .map(|r| RepoRow {
            id:        fmt_id(r.id),
            name:      r.name.clone(),
            full_name: r.full_name.clone(),
            url:       r.url.clone(),
            private:   r.is_private.to_string()
        })
        .collect();

    match format {
        OutputFormat::Table => {
            if rows.is_empty() {
                println!("{}", t!("cli.no_repositories"));
            } else {
                let table = crate::output::render_table(&rows);
                println!("{table}");
            }
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            if let Some(out) = crate::output::serialized(format, &resp.repositories) {
                println!("{}", out?);
            }
        }
        OutputFormat::Quiet => {
            for r in &resp.repositories {
                println!("{}\t{}", fmt_id(r.id), r.name);
            }
        }
    }
    Ok(())
}

/// Canonical wire value for the app type.
///
/// # Errors
///
/// Returns [`TwcError::Api`] when the value is not `backend` or `frontend`.
fn parse_app_type(value: &str) -> Result<&'static str, TwcError> {
    match value.to_lowercase().as_str() {
        "backend" => Ok("backend"),
        "frontend" => Ok("frontend"),
        _ => Err(TwcError::Api(
            t!("cli.app_invalid_type", value => value).into_owned()
        ))
    }
}

/// Canonical wire value for a supported framework.
///
/// Matches the user-supplied name case-insensitively against the framework
/// identifiers accepted by the Timeweb Cloud API.
///
/// # Errors
///
/// Returns [`TwcError::Api`] when the framework is not recognised.
fn parse_framework(value: &str) -> Result<&'static str, TwcError> {
    const NAMES: [&str; 26] = [
        "django",
        "express",
        "phoenix",
        "Spring",
        "laravel",
        "beego",
        "fastapi",
        "ASP.NET Core",
        "hapi",
        "celery",
        "flask",
        "gin",
        "docker",
        "fastify",
        "nest",
        "symfony",
        "yii",
        "angular",
        "ember",
        "next.js",
        "nuxt",
        "preact",
        "react",
        "svelte",
        "vue",
        "another"
    ];

    let lower = value.to_lowercase();
    NAMES
        .into_iter()
        .find(|name| name.to_lowercase() == lower)
        .ok_or_else(|| TwcError::Api(t!("cli.app_invalid_framework", value => value).into_owned()))
}

/// Creates a new cloud app from a connected VCS repository.
///
/// # Overview
///
/// Builds a [`models::CreateApp`] request for either a `backend` app (built and
/// run from a git repository) or a `frontend` app (static/SSR site), submits it
/// to the Timeweb Cloud API, and prints the new app id and name.
///
/// A backend app requires `run_cmd`; a frontend app requires `index_dir`.
/// The request body is assembled as JSON and deserialised into the SDK model so
/// that provider/repository identifiers and the framework are validated by the
/// SDK's own `serde` definitions without any panic-prone conversions.
///
/// # Errors
///
/// Returns [`TwcError::Api`] when the app type or framework is invalid, when a
/// required field for the chosen type is missing, when the identifiers cannot
/// be parsed, or on any network or API failure.
pub async fn create(
    config: &Configuration,
    name: &str,
    comment: Option<&str>,
    provider_id: &str,
    repository_id: &str,
    preset_id: i64,
    app_type: &str,
    framework: &str,
    branch: &str,
    commit_sha: Option<&str>,
    build_cmd: Option<&str>,
    run_cmd: Option<&str>,
    index_dir: Option<&str>,
    is_auto_deploy: bool,
    project_id: Option<i64>,
    format: OutputFormat
) -> Result<(), TwcError> {
    let type_value = parse_app_type(app_type)?;
    let framework_value = parse_framework(framework)?;

    if type_value == "backend" && run_cmd.is_none() {
        return Err(TwcError::Api(
            t!("cli.app_backend_needs_run_cmd").into_owned()
        ));
    }
    if type_value == "frontend" && index_dir.is_none() {
        return Err(TwcError::Api(
            t!("cli.app_frontend_needs_index_dir").into_owned()
        ));
    }

    let mut body = serde_json::json!({
        "provider_id": provider_id,
        "type": type_value,
        "repository_id": repository_id,
        "build_cmd": build_cmd.unwrap_or_default(),
        "branch_name": branch,
        "is_auto_deploy": is_auto_deploy,
        "commit_sha": commit_sha.unwrap_or_default(),
        "name": name,
        "comment": comment.unwrap_or_default(),
        "preset_id": preset_id,
        "framework": framework_value
    });

    if let Some(map) = body.as_object_mut() {
        if let Some(cmd) = run_cmd {
            map.insert(
                "run_cmd".to_owned(),
                serde_json::Value::String(cmd.to_owned())
            );
        }
        if let Some(dir) = index_dir {
            map.insert(
                "index_dir".to_owned(),
                serde_json::Value::String(dir.to_owned())
            );
        }
        if let Some(project) = project_id {
            map.insert("project_id".to_owned(), serde_json::Value::from(project));
        }
    }

    let request: models::CreateApp = serde_json::from_value(body).map_err(|e| {
        TwcError::Api(t!("cli.app_invalid_input", value => e.to_string()).into_owned())
    })?;

    let resp = apps_api::create_app(config, request).await?;
    let app = &resp.app;

    match format {
        OutputFormat::Table => {
            println!(
                "{}",
                t!("cli.app_created", id => fmt_id(app.id), name => app.name.clone())
            );
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            if let Some(out) = crate::output::serialized(format, &resp.app) {
                println!("{}", out?);
            }
        }
        OutputFormat::Quiet => {
            println!("{}\t{}", fmt_id(app.id), app.name);
        }
    }
    Ok(())
}

/// Lower bound for log filtering resolved from `--since` / `--today`.
///
/// Accepts `YYYY-MM-DD` (interpreted as local midnight) or a full RFC 3339
/// timestamp. `--today` resolves to the current local midnight.
///
/// # Errors
///
/// Returns [`TwcError::Api`] when the value is neither a date nor an RFC 3339
/// timestamp.
fn resolve_since(since: Option<&str>, today: bool) -> Result<Option<DateTime<Local>>, TwcError> {
    if today {
        let midnight = Local::now()
            .date_naive()
            .and_hms_opt(0, 0, 0)
            .and_then(|naive| Local.from_local_datetime(&naive).single());
        return Ok(midnight);
    }
    let Some(raw) = since else {
        return Ok(None);
    };
    if let Ok(ts) = DateTime::parse_from_rfc3339(raw) {
        return Ok(Some(ts.with_timezone(&Local)));
    }
    if let Ok(date) = NaiveDate::parse_from_str(raw, "%Y-%m-%d") {
        let midnight = date
            .and_hms_opt(0, 0, 0)
            .and_then(|naive| Local.from_local_datetime(&naive).single());
        return Ok(midnight);
    }
    Err(TwcError::Api(
        t!("cli.app_invalid_since", value => raw).into_owned()
    ))
}

/// Removes ANSI CSI escape sequences from a log line.
///
/// App runtime logs arrive colorized (`ESC[2m2026-07-05T07:13:30Z…`), so the
/// leading timestamp is wrapped in styling sequences that must be dropped
/// before parsing.
fn strip_ansi(line: &str) -> String {
    let mut out = String::with_capacity(line.len());
    let mut chars = line.chars().peekable();
    while let Some(c) = chars.next() {
        if c != '\u{1b}' {
            out.push(c);
            continue;
        }
        if chars.peek() == Some(&'[') {
            chars.next();
            for esc in chars.by_ref() {
                if esc.is_ascii_alphabetic() {
                    break;
                }
            }
        }
    }
    out
}

/// Timestamp parsed from a log line, if present.
///
/// Plain runtime lines start with an RFC 3339 timestamp (possibly wrapped in
/// ANSI styling). Structured JSON lines (`{"timestamp":"…"}`) carry it inside
/// the payload instead, so the first embedded RFC 3339 substring is used as a
/// fallback. Continuation lines (stack traces, wrapped output) have no
/// timestamp at all and return `None`.
fn line_timestamp(line: &str) -> Option<DateTime<FixedOffset>> {
    let clean = strip_ansi(line);
    clean
        .split_whitespace()
        .next()
        .and_then(|head| DateTime::parse_from_rfc3339(head).ok())
        .or_else(|| embedded_rfc3339(&clean))
}

/// Finds and parses the first RFC 3339 timestamp embedded anywhere in `line`.
///
/// Locates a `YYYY-MM-DDT` shaped anchor, extends it across the characters an
/// RFC 3339 timestamp may contain and hands the slice to the strict parser,
/// so arbitrary digits in the line cannot produce false positives.
fn embedded_rfc3339(line: &str) -> Option<DateTime<FixedOffset>> {
    let bytes = line.as_bytes();
    for (start, window) in bytes.windows(11).enumerate() {
        let anchored = window[4] == b'-'
            && window[7] == b'-'
            && window[10] == b'T'
            && window[..4].iter().all(u8::is_ascii_digit)
            && window[5..7].iter().all(u8::is_ascii_digit)
            && window[8..10].iter().all(u8::is_ascii_digit);
        if !anchored {
            continue;
        }
        let tail = &line[start..];
        let end = tail
            .find(|c: char| {
                !(c.is_ascii_digit() || matches!(c, '-' | ':' | '.' | 'T' | 'Z' | '+'))
            })
            .unwrap_or(tail.len());
        if let Ok(ts) = DateTime::parse_from_rfc3339(&tail[..end]) {
            return Some(ts);
        }
    }
    None
}

/// Timestamp of the most recent stamped line, scanning from the end.
fn last_line_timestamp(lines: &[String]) -> Option<DateTime<FixedOffset>> {
    lines.iter().rev().find_map(|line| line_timestamp(line))
}

/// Applies `since` and `tail` filters to raw log lines.
///
/// A line without its own timestamp inherits the timestamp of the closest
/// preceding stamped line, so multi-line entries stay intact. Lines seen
/// before any stamped line are kept only when no lower bound is set.
fn filter_log_lines(
    lines: &[String],
    since: Option<DateTime<Local>>,
    tail: Option<usize>
) -> Vec<String> {
    let mut kept: Vec<String> = Vec::with_capacity(lines.len());
    let mut current: Option<DateTime<Local>> = None;
    for line in lines {
        if let Some(ts) = line_timestamp(line) {
            current = Some(ts.with_timezone(&Local));
        }
        let keep = since.is_none_or(|bound| current.is_some_and(|ts| ts >= bound));
        if keep {
            kept.push(line.clone());
        }
    }
    if let Some(n) = tail {
        let skip = kept.len().saturating_sub(n);
        kept.drain(..skip);
    }
    kept
}

/// Prints filtered log lines in the requested output format.
///
/// # Errors
///
/// Returns [`TwcError::Api`] when serialization fails.
fn print_log_lines(
    lines: &[String],
    empty_key: &str,
    format: OutputFormat
) -> Result<(), TwcError> {
    match format {
        OutputFormat::Table => {
            if lines.is_empty() {
                println!("{}", t!(empty_key));
            } else {
                for line in lines {
                    println!("{line}");
                }
            }
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            let out = crate::output::serialized(format, &lines)
                .transpose()?
                .unwrap_or_default();
            println!("{out}");
        }
        OutputFormat::Quiet => {
            for line in lines {
                println!("{line}");
            }
        }
    }
    Ok(())
}

/// Shows runtime logs of an app with optional date and tail filters.
///
/// # Overview
///
/// Fetches all runtime log lines of the app (the API has no server-side
/// pagination for app logs) and applies `--since`/`--today` and `--tail`
/// filters on the client before printing.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures, or when the
/// `--since` value cannot be parsed.
pub async fn logs(
    config: &Configuration,
    id: &str,
    tail: Option<usize>,
    since: Option<&str>,
    today: bool,
    format: OutputFormat
) -> Result<(), TwcError> {
    let bound = resolve_since(since, today)?;
    let resp = apps_api::get_app_logs(config, id).await?;
    let lines = filter_log_lines(&resp.app_logs, bound, tail);
    if lines.is_empty()
        && matches!(format, OutputFormat::Table)
        && let Some(bound) = bound
        && let Some(last) = last_line_timestamp(&resp.app_logs)
    {
        println!(
            "{}",
            t!(
                "cli.no_app_logs_since",
                since => bound.format("%Y-%m-%d %H:%M:%S").to_string(),
                last => last.with_timezone(&Local).format("%Y-%m-%d %H:%M:%S").to_string()
            )
        );
        return Ok(());
    }
    print_log_lines(&lines, "cli.no_app_logs", format)
}

/// Compact row for the deploys table.
#[derive(Tabled)]
struct DeployRow {
    #[tabled(rename = "ID")]
    id:         String,
    #[tabled(rename = "Status")]
    status:     String,
    #[tabled(rename = "Commit")]
    commit:     String,
    #[tabled(rename = "Message")]
    message:    String,
    #[tabled(rename = "Started")]
    started_at: String,
    #[tabled(rename = "Ended")]
    ended_at:   String
}

/// Lists deploys of an app, newest first.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures.
pub async fn list_deploys(
    config: &Configuration,
    id: &str,
    format: OutputFormat
) -> Result<(), TwcError> {
    let resp = apps_api::get_app_deploys(config, id, None, None).await?;
    let mut deploys = resp.deploys.unwrap_or_default();
    deploys.sort_by(|a, b| b.started_at.cmp(&a.started_at));

    let rows: Vec<DeployRow> = deploys
        .iter()
        .map(|d| DeployRow {
            id:         d.id.to_string(),
            status:     format!("{:?}", d.status),
            commit:     d.commit_sha.chars().take(10).collect(),
            message:    d.commit_msg.lines().next().unwrap_or_default().to_owned(),
            started_at: d.started_at.clone(),
            ended_at:   d.ended_at.clone().unwrap_or_default()
        })
        .collect();

    match format {
        OutputFormat::Table => {
            if rows.is_empty() {
                println!("{}", t!("cli.no_deploys"));
            } else {
                println!("{}", crate::output::render_table(&rows));
            }
        }
        OutputFormat::Json | OutputFormat::Yaml => {
            let out = crate::output::serialized(format, &deploys)
                .transpose()?
                .unwrap_or_default();
            println!("{out}");
        }
        OutputFormat::Quiet => {
            for d in &deploys {
                println!("{}\t{:?}", d.id, d.status);
            }
        }
    }
    Ok(())
}

/// Shows build/deploy logs of a deploy, defaulting to the most recent one.
///
/// # Errors
///
/// Returns [`TwcError::Api`] on network or API failures, or when the app has
/// no deploys to default to.
pub async fn deploy_logs(
    config: &Configuration,
    id: &str,
    deploy_id: Option<&str>,
    debug: bool,
    format: OutputFormat
) -> Result<(), TwcError> {
    let target = if let Some(explicit) = deploy_id {
        explicit.to_owned()
    } else {
        let resp = apps_api::get_app_deploys(config, id, None, None).await?;
        resp.deploys
            .unwrap_or_default()
            .iter()
            .max_by(|a, b| a.started_at.cmp(&b.started_at))
            .map(|d| d.id.to_string())
            .ok_or_else(|| TwcError::Api(t!("cli.no_deploys").into_owned()))?
    };
    let resp = apps_api::get_deploy_logs(config, id, &target, Some(debug)).await?;
    print_log_lines(&resp.deploy_logs, "cli.no_deploy_logs", format)
}

#[cfg(test)]
mod tests;