zellij-utils 0.44.2

A utility library for Zellij client and server
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
use crate::data::{Direction, InputMode, Resize, UnblockCondition};
use crate::setup::Setup;
use crate::{
    consts::{ZELLIJ_CONFIG_DIR_ENV, ZELLIJ_CONFIG_FILE_ENV},
    input::{layout::PluginUserConfiguration, options::Options},
};
use clap::{ArgEnum, Args, Parser, Subcommand};
use serde::{Deserialize, Serialize};
use std::net::IpAddr;
use std::path::PathBuf;
use url::Url;

fn validate_session(name: &str) -> Result<String, String> {
    #[cfg(unix)]
    {
        use crate::consts::ZELLIJ_SOCK_MAX_LENGTH;

        let mut socket_path = crate::consts::ZELLIJ_SOCK_DIR.clone();
        socket_path.push(name);

        if socket_path.as_os_str().len() >= ZELLIJ_SOCK_MAX_LENGTH {
            // socket path must be less than 108 bytes
            let available_length = ZELLIJ_SOCK_MAX_LENGTH
                .saturating_sub(socket_path.as_os_str().len())
                .saturating_sub(1);

            return Err(format!(
                "session name must be less than {} characters",
                available_length
            ));
        };
    };

    Ok(name.to_owned())
}

#[derive(Parser, Default, Debug, Clone, Serialize, Deserialize)]
#[clap(version, name = "zellij")]
pub struct CliArgs {
    /// Maximum panes on screen, caution: opening more panes will close old ones
    #[clap(long, value_parser)]
    pub max_panes: Option<usize>,

    /// Change where zellij looks for plugins
    #[clap(long, value_parser, overrides_with = "data_dir")]
    pub data_dir: Option<PathBuf>,

    /// Run server listening at the specified socket path
    #[clap(long, value_parser, hide = true, overrides_with = "server")]
    pub server: Option<PathBuf>,

    /// Specify name of a new session
    #[clap(long, short, overrides_with = "session", value_parser = validate_session)]
    pub session: Option<String>,

    /// Name of a predefined layout inside the layout directory or the path to a layout file
    /// if inside a session (or using the --session flag) will be added to the session as a new tab
    /// or tabs, otherwise will start a new session
    #[clap(short, long, value_parser, overrides_with = "layout")]
    pub layout: Option<PathBuf>,

    /// Raw KDL layout string to use directly (instead of a file path)
    /// if inside a session (or using the --session flag) will be added to the session as a new tab
    /// or tabs, otherwise will start a new session
    #[clap(long, value_parser, conflicts_with_all = &["layout", "new-session-with-layout"])]
    pub layout_string: Option<String>,

    /// Name of a predefined layout inside the layout directory or the path to a layout file
    /// Will always start a new session, even if inside an existing session
    #[clap(short, long, value_parser, overrides_with = "new_session_with_layout")]
    pub new_session_with_layout: Option<PathBuf>,

    /// Change where zellij looks for the configuration file
    #[clap(short, long, overrides_with = "config", env = ZELLIJ_CONFIG_FILE_ENV, value_parser)]
    pub config: Option<PathBuf>,

    /// Change where zellij looks for the configuration directory
    #[clap(long, overrides_with = "config_dir", env = ZELLIJ_CONFIG_DIR_ENV, value_parser)]
    pub config_dir: Option<PathBuf>,

    #[clap(subcommand)]
    pub command: Option<Command>,

    /// Specify emitting additional debug information
    #[clap(short, long, value_parser)]
    pub debug: bool,
}

impl CliArgs {
    pub fn is_setup_clean(&self) -> bool {
        if let Some(Command::Setup(ref setup)) = &self.command {
            if setup.clean {
                return true;
            }
        }
        false
    }
    pub fn options(&self) -> Option<Options> {
        if let Some(Command::Options(options)) = &self.command {
            return Some(options.clone());
        }
        None
    }
}

#[derive(Debug, Subcommand, Clone, Serialize, Deserialize)]
pub enum Command {
    /// Change the behaviour of zellij
    #[clap(name = "options", value_parser)]
    Options(Options),

    /// Setup zellij and check its configuration
    #[clap(name = "setup", value_parser)]
    Setup(Setup),

    /// Run a web server to serve terminal sessions
    #[clap(name = "web", value_parser)]
    Web(WebCli),

    /// Send actions to a specific session
    #[clap(visible_alias = "ac")]
    #[clap(subcommand)]
    Action(Box<CliAction>),

    /// Explore existing zellij sessions
    #[clap(flatten)]
    Sessions(Sessions),

    /// Subscribe to pane render updates (viewport and scrollback)
    #[clap(override_usage(
        "zellij [--session <OTHER SESSION NAME>] subscribe [OPTIONS] --pane-id..."
    ))]
    Subscribe(SubscribeCli),
}

#[derive(Debug, Parser, Clone, Serialize, Deserialize)]
pub struct SubscribeCli {
    /// Pane ID(s) to subscribe to (e.g. terminal_1, plugin_2, or bare number like 1)
    #[clap(
        short,
        long,
        required = true,
        multiple_values = true,
        multiple_occurrences = true
    )]
    pub pane_id: Vec<String>,

    /// Include scrollback lines in initial delivery.
    /// Bare --scrollback = all scrollback, --scrollback N = last N lines.
    #[clap(
        short,
        long,
        min_values = 0,
        max_values = 1,
        default_missing_value = "0"
    )]
    pub scrollback: Option<usize>,

    /// Output format
    #[clap(short, long, default_value = "raw", arg_enum)]
    pub format: SubscribeFormat,

    /// Preserve ANSI styling in the output
    #[clap(long, value_parser, default_value("false"), takes_value(false))]
    pub ansi: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize, ArgEnum)]
pub enum SubscribeFormat {
    Raw,
    Json,
}

#[derive(Debug, Clone, Args, Serialize, Deserialize)]
pub struct WebCli {
    /// Start the server (default unless other arguments are specified)
    #[clap(long, value_parser, display_order = 1)]
    pub start: bool,

    /// Stop the server
    #[clap(long, value_parser, exclusive(true), display_order = 2)]
    pub stop: bool,

    /// Get the server status
    #[clap(long, value_parser, conflicts_with("start"), display_order = 3)]
    pub status: bool,

    /// Timeout in seconds for the status check (default: 30)
    #[clap(long, value_parser, requires = "status", display_order = 4)]
    pub timeout: Option<u64>,

    /// Run the server in the background
    #[clap(
        short,
        long,
        value_parser,
        conflicts_with_all(&["stop", "status", "create-token", "revoke-token", "revoke-all-tokens"]),
        display_order = 5
    )]
    pub daemonize: bool,
    /// Timeout in seconds waiting for the server to start (default: 10).
    /// Only used on Windows where the daemonized server is polled via TCP.
    /// On Unix, startup signaling uses pipes and this option is ignored.
    #[clap(long, value_parser, display_order = 6)]
    pub server_startup_timeout: Option<u64>,
    /// Create a login token for the web interface, will only be displayed once and cannot later be
    /// retrieved. Returns the token name and the token.
    #[clap(long, value_parser, exclusive(true), display_order = 7)]
    pub create_token: bool,
    /// Optional name for the token
    #[clap(long, value_parser, value_name = "TOKEN_NAME", display_order = 8)]
    pub token_name: Option<String>,
    /// Create a read-only login token (can only attach to existing sessions as watcher)
    #[clap(long, value_parser, exclusive(true), display_order = 9)]
    pub create_read_only_token: bool,
    /// Revoke a login token by its name
    #[clap(
        long,
        value_parser,
        exclusive(true),
        value_name = "TOKEN NAME",
        display_order = 10
    )]
    pub revoke_token: Option<String>,
    /// Revoke all login tokens
    #[clap(long, value_parser, exclusive(true), display_order = 11)]
    pub revoke_all_tokens: bool,
    /// List token names and their creation dates (cannot show actual tokens)
    #[clap(long, value_parser, exclusive(true), display_order = 12)]
    pub list_tokens: bool,
    /// The ip address to listen on locally for connections (defaults to 127.0.0.1)
    #[clap(
        long,
        value_parser,
        conflicts_with_all(&["stop", "create-token", "revoke-token", "revoke-all-tokens"]),
        display_order = 13
    )]
    pub ip: Option<IpAddr>,
    /// The port to listen on locally for connections (defaults to 8082)
    #[clap(
        long,
        value_parser,
        conflicts_with_all(&["stop", "create-token", "revoke-token", "revoke-all-tokens"]),
        display_order = 14
    )]
    pub port: Option<u16>,
    /// The path to the SSL certificate (required if not listening on 127.0.0.1)
    #[clap(
        long,
        value_parser,
        conflicts_with_all(&["stop", "status", "create-token", "revoke-token", "revoke-all-tokens"]),
        display_order = 15
    )]
    pub cert: Option<PathBuf>,
    /// The path to the SSL key (required if not listening on 127.0.0.1)
    #[clap(
        long,
        value_parser,
        conflicts_with_all(&["stop", "status", "create-token", "revoke-token", "revoke-all-tokens"]),
        display_order = 16
    )]
    pub key: Option<PathBuf>,
}

impl WebCli {
    pub fn get_start(&self) -> bool {
        self.start
            || !(self.stop
                || self.status
                || self.create_token
                || self.create_read_only_token
                || self.revoke_token.is_some()
                || self.revoke_all_tokens
                || self.list_tokens)
    }
}

#[derive(Debug, Subcommand, Clone, Serialize, Deserialize)]
pub enum SessionCommand {
    /// Change the behaviour of zellij
    #[clap(name = "options")]
    Options(Options),
}

#[derive(Debug, Subcommand, Clone, Serialize, Deserialize)]
pub enum Sessions {
    /// List active sessions
    #[clap(visible_alias = "ls")]
    ListSessions {
        /// Do not add colors and formatting to the list (useful for parsing)
        #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
        no_formatting: bool,

        /// Print just the session name
        #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
        short: bool,

        /// List the sessions in reverse order (default is ascending order)
        #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
        reverse: bool,
    },
    /// List existing plugin aliases
    #[clap(visible_alias = "la")]
    ListAliases,
    /// Attach to a session
    #[clap(visible_alias = "a")]
    Attach {
        /// Name of the session to attach to.
        #[clap(value_parser)]
        session_name: Option<String>,

        /// Create a session if one does not exist.
        #[clap(short, long, value_parser)]
        create: bool,

        /// Create a detached session in the background if one does not exist
        #[clap(short('b'), long, value_parser)]
        create_background: bool,

        /// Number of the session index in the active sessions ordered creation date.
        #[clap(long, value_parser)]
        index: Option<usize>,

        /// Change the behaviour of zellij
        #[clap(subcommand, name = "options")]
        options: Option<Box<SessionCommand>>,

        /// If resurrecting a dead session, immediately run all its commands on startup
        #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
        force_run_commands: bool,

        /// Authentication token for remote sessions
        #[clap(short('t'), long, value_parser)]
        token: Option<String>,

        /// Save session for automatic re-authentication (4 weeks)
        #[clap(short('r'), long, value_parser)]
        remember: bool,

        /// Delete saved session before connecting
        #[clap(long, value_parser)]
        forget: bool,

        /// Path to a custom CA certificate (PEM format) for verifying the remote server
        #[clap(long, value_name = "FILE", value_parser)]
        ca_cert: Option<PathBuf>,

        /// Skip TLS certificate validation (DANGEROUS — development only)
        #[clap(long, value_parser)]
        insecure: bool,
    },

    /// Watch a session (read-only)
    #[clap(visible_alias = "w")]
    Watch {
        /// Name of the session to watch
        #[clap(value_parser)]
        session_name: Option<String>,
    },

    /// Kill a specific session
    #[clap(visible_alias = "k")]
    KillSession {
        /// Name of target session
        #[clap(value_parser)]
        target_session: Option<String>,
    },

    /// Delete a specific session
    #[clap(visible_alias = "d")]
    DeleteSession {
        /// Name of target session
        #[clap(value_parser)]
        target_session: Option<String>,
        /// Kill the session if it's running before deleting it
        #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
        force: bool,
    },

    /// Kill all sessions
    #[clap(visible_alias = "ka")]
    KillAllSessions {
        /// Automatic yes to prompts
        #[clap(short, long, value_parser)]
        yes: bool,
    },

    /// Delete all sessions
    #[clap(visible_alias = "da")]
    DeleteAllSessions {
        /// Automatic yes to prompts
        #[clap(short, long, value_parser)]
        yes: bool,
        /// Kill the sessions if they're running before deleting them
        #[clap(short, long, value_parser, takes_value(false), default_value("false"))]
        force: bool,
    },

    /// Run a command in a new pane
    /// Returns: Created pane ID (format: terminal_<id>)
    #[clap(visible_alias = "r")]
    Run {
        /// Command to run
        #[clap(last(true), required(true))]
        command: Vec<String>,

        /// Direction to open the new pane in
        #[clap(short, long, value_parser, conflicts_with("floating"))]
        direction: Option<Direction>,

        /// Change the working directory of the new pane
        #[clap(long, value_parser)]
        cwd: Option<PathBuf>,

        /// Open the new pane in floating mode
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        floating: bool,

        /// Open the new pane in place of the current pane, temporarily suspending it
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("floating"),
            conflicts_with("direction")
        )]
        in_place: bool,

        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,

        /// Name of the new pane
        #[clap(short, long, value_parser)]
        name: Option<String>,

        /// Close the pane immediately when its command exits
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        close_on_exit: bool,

        /// Start the command suspended, only running after you first presses ENTER
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        start_suspended: bool,

        /// The x coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        x: Option<String>,
        /// The y coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        y: Option<String>,
        /// The width if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        width: Option<String>,
        /// The height if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        height: Option<String>,
        /// Whether to pin a floating pane so that it is always on top
        #[clap(long, requires("floating"))]
        pinned: Option<bool>,
        #[clap(
            long,
            conflicts_with("floating"),
            conflicts_with("direction"),
            value_parser,
            default_value("false"),
            takes_value(false)
        )]
        stacked: bool,
        /// Block until the command has finished and its pane has been closed
        #[clap(long, value_parser, default_value("false"), takes_value(false))]
        blocking: bool,

        /// Block until the command exits successfully (exit status 0) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("blocking"),
            conflicts_with("block-until-exit-failure"),
            conflicts_with("block-until-exit")
        )]
        block_until_exit_success: bool,

        /// Block until the command exits with failure (non-zero exit status) OR its pane has been
        /// closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("blocking"),
            conflicts_with("block-until-exit-success"),
            conflicts_with("block-until-exit")
        )]
        block_until_exit_failure: bool,

        /// Block until the command exits (regardless of exit status) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("blocking"),
            conflicts_with("block-until-exit-success"),
            conflicts_with("block-until-exit-failure")
        )]
        block_until_exit: bool,
        /// if set, will open the pane near the current one rather than following the user's focus
        #[clap(long)]
        near_current_pane: bool,
        /// start this pane without a border (warning: will make it impossible to move with the
        /// mouse)
        #[clap(short, long, value_parser)]
        borderless: Option<bool>,
        /// Target a specific tab by ID
        #[clap(
            long,
            value_parser,
            conflicts_with("near-current-pane"),
            conflicts_with("in-place")
        )]
        tab_id: Option<usize>,
    },
    /// Load a plugin
    /// Returns: Created pane ID (format: plugin_<id>)
    #[clap(visible_alias = "p")]
    Plugin {
        /// Plugin URL, can either start with http(s), file: or zellij:
        #[clap(last(true), required(true))]
        url: String,

        /// Plugin configuration
        #[clap(short, long, value_parser)]
        configuration: Option<PluginUserConfiguration>,

        /// Open the new pane in floating mode
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        floating: bool,

        /// Open the new pane in place of the current pane, temporarily suspending it
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("floating")
        )]
        in_place: bool,

        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,

        /// Skip the memory and HD cache and force recompile of the plugin (good for development)
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        skip_plugin_cache: bool,
        /// The x coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        x: Option<String>,
        /// The y coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        y: Option<String>,
        /// The width if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        width: Option<String>,
        /// The height if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        height: Option<String>,
        /// Whether to pin a floating pane so that it is always on top
        #[clap(long, requires("floating"))]
        pinned: Option<bool>,
        /// start this pane without a border (warning: will make it impossible to move with the
        /// mouse)
        #[clap(short, long, value_parser)]
        borderless: Option<bool>,
        /// Target a specific tab by ID
        #[clap(long, value_parser, conflicts_with("in-place"))]
        tab_id: Option<usize>,
    },
    /// Edit file with default $EDITOR / $VISUAL
    /// Returns: Created pane ID (format: terminal_<id>)
    #[clap(visible_alias = "e")]
    Edit {
        file: PathBuf,

        /// Open the file in the specified line number
        #[clap(short, long, value_parser)]
        line_number: Option<usize>,

        /// Direction to open the new pane in
        #[clap(short, long, value_parser, conflicts_with("floating"))]
        direction: Option<Direction>,

        /// Open the new pane in place of the current pane, temporarily suspending it
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("floating"),
            conflicts_with("direction")
        )]
        in_place: bool,

        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,

        /// Open the new pane in floating mode
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        floating: bool,

        /// Change the working directory of the editor
        #[clap(long, value_parser)]
        cwd: Option<PathBuf>,
        /// The x coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        x: Option<String>,
        /// The y coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        y: Option<String>,
        /// The width if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        width: Option<String>,
        /// The height if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        height: Option<String>,
        /// Whether to pin a floating pane so that it is always on top
        #[clap(long, requires("floating"))]
        pinned: Option<bool>,
        /// if set, will open the pane near the current one rather than following the user's focus
        #[clap(long)]
        near_current_pane: bool,
        /// start this pane without a border (warning: will make it impossible to move with the
        /// mouse)
        #[clap(short, long, value_parser)]
        borderless: Option<bool>,
        /// Target a specific tab by ID
        #[clap(
            long,
            value_parser,
            conflicts_with("near-current-pane"),
            conflicts_with("in-place")
        )]
        tab_id: Option<usize>,
    },
    ConvertConfig {
        old_config_file: PathBuf,
    },
    ConvertLayout {
        old_layout_file: PathBuf,
    },
    ConvertTheme {
        old_theme_file: PathBuf,
    },
    /// Send data to one or more plugins, launch them if they are not running.
    #[clap(override_usage(
r#"
zellij pipe [OPTIONS] [--] <PAYLOAD>

* Send data to a specific plugin:

zellij pipe --plugin file:/path/to/my/plugin.wasm --name my_pipe_name -- my_arbitrary_data

* To all running plugins (that are listening):

zellij pipe --name my_pipe_name -- my_arbitrary_data

* Pipe data into this command's STDIN and get output from the plugin on this command's STDOUT

tail -f /tmp/my-live-logfile | zellij pipe --name logs --plugin https://example.com/my-plugin.wasm | wc -l
"#))]
    Pipe {
        /// The name of the pipe
        #[clap(short, long, value_parser, display_order(1))]
        name: Option<String>,
        /// The data to send down this pipe (if blank, will listen to STDIN)
        payload: Option<String>,

        #[clap(short, long, value_parser, display_order(2))]
        /// The args of the pipe
        args: Option<PluginUserConfiguration>, // TODO: we might want to not re-use
        // PluginUserConfiguration
        /// The plugin url (eg. file:/tmp/my-plugin.wasm) to direct this pipe to, if not specified,
        /// will be sent to all plugins, if specified and is not running, the plugin will be launched
        #[clap(short, long, value_parser, display_order(3))]
        plugin: Option<String>,
        /// The plugin configuration (note: the same plugin with different configuration is
        /// considered a different plugin for the purposes of determining the pipe destination)
        #[clap(short('c'), long, value_parser, display_order(4))]
        plugin_configuration: Option<PluginUserConfiguration>,
    },
}

#[derive(Debug, Subcommand, Clone, Serialize, Deserialize)]
pub enum CliAction {
    /// Write bytes to the terminal.
    Write {
        bytes: Vec<u8>,
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Write characters to the terminal.
    WriteChars {
        chars: String,
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Paste text to the terminal (using bracketed paste mode).
    Paste {
        chars: String,
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Send one or more keys to the terminal (e.g., "Ctrl a", "F1", "Alt Shift b")
    SendKeys {
        /// Keys to send as space-separated strings
        #[clap(value_parser, required = true)]
        keys: Vec<String>,

        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// [increase|decrease] the focused panes area at the [left|down|up|right] border.
    Resize {
        resize: Resize,
        direction: Option<Direction>,
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Change focus to the next pane
    FocusNextPane,
    /// Change focus to the previous pane
    FocusPreviousPane,
    /// Focus a specific pane by its ID
    FocusPaneId {
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3
        pane_id: String,
    },
    /// Move the focused pane in the specified direction. [right|left|up|down]
    MoveFocus {
        direction: Direction,
    },
    /// Move focus to the pane or tab (if on screen edge) in the specified direction
    /// [right|left|up|down]
    MoveFocusOrTab {
        direction: Direction,
    },
    /// Change the location of the focused pane in the specified direction or rotate forwrads
    /// [right|left|up|down]
    MovePane {
        direction: Option<Direction>,
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Rotate the location of the previous pane backwards
    MovePaneBackwards {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Clear all buffers for a focused pane
    Clear {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Dumps the viewport and optionally scrollback of a pane to a file or STDOUT
    DumpScreen {
        /// File path to dump the pane content to. If omitted, prints to STDOUT.
        #[clap(long, value_parser)]
        path: Option<PathBuf>,

        /// Dump the pane with full scrollback
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        full: bool,

        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3). If not specified, dumps the focused pane.
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,

        /// Preserve ANSI styling in the dump output
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        ansi: bool,
    },
    /// Dump current layout to stdout
    DumpLayout,
    /// Save the current session state to disk immediately
    SaveSession,
    /// Open the pane scrollback in your default editor
    EditScrollback {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,

        /// Preserve ANSI styling in the scrollback dump
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        ansi: bool,
    },
    /// Scroll up in the focused pane
    ScrollUp {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll down in focus pane.
    ScrollDown {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll down to bottom in focus pane.
    ScrollToBottom {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll up to top in focus pane.
    ScrollToTop {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll up one page in focus pane.
    PageScrollUp {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll down one page in focus pane.
    PageScrollDown {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll up half page in focus pane.
    HalfPageScrollUp {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Scroll down half page in focus pane.
    HalfPageScrollDown {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Toggle between fullscreen focus pane and normal layout.
    ToggleFullscreen {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Toggle frames around panes in the UI
    TogglePaneFrames,
    /// Toggle between sending text commands to all panes on the current tab and normal mode.
    ToggleActiveSyncTab {
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Open a new pane in the specified direction [right|down]
    /// If no direction is specified, will try to use the biggest available space.
    /// Returns: Created pane ID (format: terminal_<id> or plugin_<id>)
    NewPane {
        /// Direction to open the new pane in
        #[clap(short, long, value_parser, conflicts_with("floating"))]
        direction: Option<Direction>,

        #[clap(last(true))]
        command: Vec<String>,

        #[clap(short, long, conflicts_with("command"), conflicts_with("direction"))]
        plugin: Option<String>,

        /// Change the working directory of the new pane
        #[clap(long, value_parser)]
        cwd: Option<PathBuf>,

        /// Open the new pane in floating mode
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        floating: bool,

        /// Open the new pane in place of the current pane, temporarily suspending it
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("floating"),
            conflicts_with("direction")
        )]
        in_place: bool,

        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,

        /// Name of the new pane
        #[clap(short, long, value_parser)]
        name: Option<String>,

        /// Close the pane immediately when its command exits
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("command")
        )]
        close_on_exit: bool,
        /// Start the command suspended, only running it after the you first press ENTER
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("command")
        )]
        start_suspended: bool,
        #[clap(long, value_parser)]
        configuration: Option<PluginUserConfiguration>,
        #[clap(long, value_parser)]
        skip_plugin_cache: bool,
        /// The x coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        x: Option<String>,
        /// The y coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        y: Option<String>,
        /// The width if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        width: Option<String>,
        /// The height if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        height: Option<String>,
        /// Whether to pin a floating pane so that it is always on top
        #[clap(long, requires("floating"))]
        pinned: Option<bool>,
        #[clap(
            long,
            conflicts_with("floating"),
            conflicts_with("direction"),
            value_parser,
            default_value("false"),
            takes_value(false)
        )]
        stacked: bool,
        /// Block until the command has finished and its pane has been closed
        #[clap(short, long)]
        blocking: bool,

        /// Block until the command exits successfully (exit status 0) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("blocking"),
            conflicts_with("block-until-exit-failure"),
            conflicts_with("block-until-exit")
        )]
        block_until_exit_success: bool,

        /// Block until the command exits with failure (non-zero exit status) OR its pane has been
        /// closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("blocking"),
            conflicts_with("block-until-exit-success"),
            conflicts_with("block-until-exit")
        )]
        block_until_exit_failure: bool,

        /// Block until the command exits (regardless of exit status) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("blocking"),
            conflicts_with("block-until-exit-success"),
            conflicts_with("block-until-exit-failure")
        )]
        block_until_exit: bool,

        #[clap(skip)]
        unblock_condition: Option<UnblockCondition>,

        /// if set, will open the pane near the current one rather than following the user's focus
        #[clap(long)]
        near_current_pane: bool,
        /// start this pane without a border (warning: will make it impossible to move with the
        /// mouse)
        #[clap(long, value_parser)]
        borderless: Option<bool>,
        /// Target a specific tab by ID
        #[clap(
            long,
            value_parser,
            conflicts_with("near-current-pane"),
            conflicts_with("in-place")
        )]
        tab_id: Option<usize>,
    },
    /// Open the specified file in a new zellij pane with your default EDITOR
    /// Returns: Created pane ID (format: terminal_<id>)
    Edit {
        file: PathBuf,

        /// Direction to open the new pane in
        #[clap(short, long, value_parser, conflicts_with("floating"))]
        direction: Option<Direction>,

        /// Open the file in the specified line number
        #[clap(short, long, value_parser)]
        line_number: Option<usize>,

        /// Open the new pane in floating mode
        #[clap(short, long, value_parser, default_value("false"), takes_value(false))]
        floating: bool,

        /// Open the new pane in place of the current pane, temporarily suspending it
        #[clap(
            short,
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            conflicts_with("floating"),
            conflicts_with("direction")
        )]
        in_place: bool,

        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,

        /// Change the working directory of the editor
        #[clap(long, value_parser)]
        cwd: Option<PathBuf>,
        /// The x coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        x: Option<String>,
        /// The y coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long, requires("floating"))]
        y: Option<String>,
        /// The width if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        width: Option<String>,
        /// The height if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long, requires("floating"))]
        height: Option<String>,
        /// Whether to pin a floating pane so that it is always on top
        #[clap(long, requires("floating"))]
        pinned: Option<bool>,
        /// if set, will open the pane near the current one rather than following the user's focus
        #[clap(long)]
        near_current_pane: bool,
        /// start this pane without a border (warning: will make it impossible to move with the
        /// mouse)
        #[clap(short, long, value_parser)]
        borderless: Option<bool>,
        /// Target a specific tab by ID
        #[clap(
            long,
            value_parser,
            conflicts_with("near-current-pane"),
            conflicts_with("in-place")
        )]
        tab_id: Option<usize>,
    },
    /// Switch input mode of all connected clients [locked|pane|tab|resize|move|search|session]
    SwitchMode {
        input_mode: InputMode,
    },
    /// Embed focused pane if floating or float focused pane if embedded
    TogglePaneEmbedOrFloating {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Toggle the visibility of all floating panes in the current Tab, open one if none exist
    ToggleFloatingPanes {
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Show all floating panes in the specified tab (or active tab if tab_id is not provided).
    ///
    /// Returns exit code 0 if state was changed, 2 if already visible, 1 if tab not found.
    ShowFloatingPanes {
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Hide all floating panes in the specified tab (or active tab if tab_id is not provided).
    ///
    /// Returns exit code 0 if state was changed, 2 if already hidden, 1 if tab not found.
    HideFloatingPanes {
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Check if floating panes are visible in the specified tab (or active tab).
    ///
    /// Prints "true" to stdout and exits 0 if visible.
    /// Prints "false" to stdout and exits 1 if not visible.
    AreFloatingPanesVisible {
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Close the focused pane.
    ClosePane {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Renames the focused pane
    RenamePane {
        name: String,
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Remove a previously set pane name
    UndoRenamePane {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Go to the next tab.
    GoToNextTab,
    /// Go to the previous tab.
    GoToPreviousTab,
    /// Close the current tab.
    CloseTab {
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Go to tab with index [index]
    GoToTab {
        index: u32,
    },
    /// Go to tab with name [name]
    ///
    /// Returns: When --create is used and tab is created, outputs the tab ID as a single number
    GoToTabName {
        name: String,
        /// Create a tab if one does not exist.
        #[clap(short, long, value_parser)]
        create: bool,
    },
    /// Renames the focused pane
    RenameTab {
        name: String,
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Remove a previously set tab name
    UndoRenameTab {
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Go to tab with stable ID
    GoToTabById {
        id: u64,
    },
    /// Close tab with stable ID
    CloseTabById {
        id: u64,
    },
    /// Rename tab by stable ID
    RenameTabById {
        id: u64,
        name: String,
    },
    /// Create a new tab, optionally with a specified tab layout and name
    ///
    /// Returns: The created tab's ID as a single number on stdout
    NewTab {
        /// Layout to use for the new tab
        #[clap(short, long, value_parser, conflicts_with = "layout-string")]
        layout: Option<PathBuf>,

        /// Raw KDL layout string to use directly (instead of a layout file path)
        #[clap(long, value_parser, conflicts_with = "layout")]
        layout_string: Option<String>,

        /// Default folder to look for layouts
        #[clap(long, value_parser, requires("layout"))]
        layout_dir: Option<PathBuf>,

        /// Name of the new tab
        #[clap(short, long, value_parser)]
        name: Option<String>,

        /// Change the working directory of the new tab
        #[clap(short, long, value_parser)]
        cwd: Option<PathBuf>,

        /// Optional initial command to run in the new tab
        #[clap(
            value_parser,
            conflicts_with("initial-plugin"),
            multiple_values(true),
            takes_value(true),
            last(true)
        )]
        initial_command: Vec<String>,

        /// Initial plugin to load in the new tab
        #[clap(long, value_parser, conflicts_with("initial-command"))]
        initial_plugin: Option<String>,

        /// Close the pane immediately when its command exits
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("initial-command")
        )]
        close_on_exit: bool,

        /// Start the command suspended, only running it after you first press ENTER
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("initial-command")
        )]
        start_suspended: bool,

        /// Block until the command exits successfully (exit status 0) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("initial-command"),
            conflicts_with("block-until-exit-failure"),
            conflicts_with("block-until-exit")
        )]
        block_until_exit_success: bool,

        /// Block until the command exits with failure (non-zero exit status) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("initial-command"),
            conflicts_with("block-until-exit-success"),
            conflicts_with("block-until-exit")
        )]
        block_until_exit_failure: bool,

        /// Block until the command exits (regardless of exit status) OR its pane has been closed
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("initial-command"),
            conflicts_with("block-until-exit-success"),
            conflicts_with("block-until-exit-failure")
        )]
        block_until_exit: bool,
    },
    /// Move the focused tab in the specified direction. [right|left]
    MoveTab {
        direction: Direction,
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    PreviousSwapLayout {
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    NextSwapLayout {
        /// Target a specific tab by ID
        #[clap(short, long, value_parser)]
        tab_id: Option<usize>,
    },
    /// Override the layout of the active tab
    OverrideLayout {
        /// Path to the layout file
        #[clap(
            value_parser,
            required_unless_present = "layout-string",
            conflicts_with = "layout-string"
        )]
        layout: Option<PathBuf>,

        /// Raw KDL layout string to use directly (instead of a layout file path)
        #[clap(long, value_parser, conflicts_with = "layout")]
        layout_string: Option<String>,

        /// Default folder to look for layouts
        #[clap(long, value_parser)]
        layout_dir: Option<PathBuf>,

        /// Retain existing terminal panes that do not fit in the layout (default: false)
        #[clap(long, value_parser, takes_value(false), default_value("false"))]
        retain_existing_terminal_panes: bool,

        /// Retain existing plugin panes that do not fit with the layout default: false)
        #[clap(long, value_parser, takes_value(false), default_value("false"))]
        retain_existing_plugin_panes: bool,

        /// Only apply the layout to the active tab (uses just the first layout tab if it has
        /// multiple)
        #[clap(long, value_parser, takes_value(false), default_value("false"))]
        apply_only_to_active_tab: bool,
    },
    /// Query all tab names
    QueryTabNames,
    StartOrReloadPlugin {
        url: String,
        #[clap(short, long, value_parser)]
        configuration: Option<PluginUserConfiguration>,
    },
    /// Returns: Plugin pane ID (format: plugin_<id>) when creating or focusing plugin
    LaunchOrFocusPlugin {
        #[clap(short, long, value_parser)]
        floating: bool,
        #[clap(short, long, value_parser)]
        in_place: bool,
        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,
        #[clap(short, long, value_parser)]
        move_to_focused_tab: bool,
        url: String,
        #[clap(short, long, value_parser)]
        configuration: Option<PluginUserConfiguration>,
        #[clap(short, long, value_parser)]
        skip_plugin_cache: bool,
        /// Target a specific tab by ID
        #[clap(long, value_parser, conflicts_with("in-place"))]
        tab_id: Option<usize>,
    },
    /// Returns: Plugin pane ID (format: plugin_<id>)
    LaunchPlugin {
        #[clap(short, long, value_parser)]
        floating: bool,
        #[clap(short, long, value_parser)]
        in_place: bool,
        /// Close the replaced pane instead of suspending it (only effective with --in-place)
        #[clap(
            long,
            value_parser,
            default_value("false"),
            takes_value(false),
            requires("in-place")
        )]
        close_replaced_pane: bool,
        url: Url,
        #[clap(short, long, value_parser)]
        configuration: Option<PluginUserConfiguration>,
        #[clap(short, long, value_parser)]
        skip_plugin_cache: bool,
        /// Target a specific tab by ID
        #[clap(long, value_parser, conflicts_with("in-place"))]
        tab_id: Option<usize>,
    },
    RenameSession {
        name: String,
    },
    /// Send data to one or more plugins, launch them if they are not running.
    #[clap(override_usage(
r#"
zellij action pipe [OPTIONS] [--] <PAYLOAD>

* Send data to a specific plugin:

zellij action pipe --plugin file:/path/to/my/plugin.wasm --name my_pipe_name -- my_arbitrary_data

* To all running plugins (that are listening):

zellij action pipe --name my_pipe_name -- my_arbitrary_data

* Pipe data into this command's STDIN and get output from the plugin on this command's STDOUT

tail -f /tmp/my-live-logfile | zellij action pipe --name logs --plugin https://example.com/my-plugin.wasm | wc -l
"#))]
    Pipe {
        /// The name of the pipe
        #[clap(short, long, value_parser, display_order(1))]
        name: Option<String>,
        /// The data to send down this pipe (if blank, will listen to STDIN)
        payload: Option<String>,

        #[clap(short, long, value_parser, display_order(2))]
        /// The args of the pipe
        args: Option<PluginUserConfiguration>, // TODO: we might want to not re-use
        // PluginUserConfiguration
        /// The plugin url (eg. file:/tmp/my-plugin.wasm) to direct this pipe to, if not specified,
        /// will be sent to all plugins, if specified and is not running, the plugin will be launched
        #[clap(short, long, value_parser, display_order(3))]
        plugin: Option<String>,
        /// The plugin configuration (note: the same plugin with different configuration is
        /// considered a different plugin for the purposes of determining the pipe destination)
        #[clap(short('c'), long, value_parser, display_order(4))]
        plugin_configuration: Option<PluginUserConfiguration>,
        /// Launch a new plugin even if one is already running
        #[clap(
            short('l'),
            long,
            value_parser,
            takes_value(false),
            default_value("false"),
            display_order(5)
        )]
        force_launch_plugin: bool,
        /// If launching a new plugin, skip cache and force-compile the plugin
        #[clap(
            short('s'),
            long,
            value_parser,
            takes_value(false),
            default_value("false"),
            display_order(6)
        )]
        skip_plugin_cache: bool,
        /// If launching a plugin, should it be floating or not, defaults to floating
        #[clap(short('f'), long, value_parser, display_order(7))]
        floating_plugin: Option<bool>,
        /// If launching a plugin, launch it in-place (on top of the current pane)
        #[clap(
            short('i'),
            long,
            value_parser,
            conflicts_with("floating-plugin"),
            display_order(8)
        )]
        in_place_plugin: Option<bool>,
        /// If launching a plugin, specify its working directory
        #[clap(short('w'), long, value_parser, display_order(9))]
        plugin_cwd: Option<PathBuf>,
        /// If launching a plugin, specify its pane title
        #[clap(short('t'), long, value_parser, display_order(10))]
        plugin_title: Option<String>,
    },
    ListClients,
    /// List all panes in the current session
    ///
    /// Returns: Formatted list of panes (table or JSON) to stdout
    ListPanes {
        /// Include tab information (name, position, ID)
        #[clap(short, long, value_parser)]
        tab: bool,

        /// Include running command information
        #[clap(short, long, value_parser)]
        command: bool,

        /// Include pane state (focused, floating, exited, etc.)
        #[clap(short, long, value_parser)]
        state: bool,

        /// Include geometry (position, size)
        #[clap(short, long, value_parser)]
        geometry: bool,

        /// Include all available fields
        #[clap(short, long, value_parser)]
        all: bool,

        /// Output as JSON
        #[clap(short, long, value_parser)]
        json: bool,
    },
    /// List all tabs with their information
    ///
    /// Returns: Tab information in table or JSON format
    ListTabs {
        /// Include state information (active, fullscreen, sync, floating visibility)
        #[clap(short, long, value_parser)]
        state: bool,

        /// Include dimension information (viewport, display area)
        #[clap(short, long, value_parser)]
        dimensions: bool,

        /// Include pane counts
        #[clap(short, long, value_parser)]
        panes: bool,

        /// Include layout information (swap layout name and dirty state)
        #[clap(short, long, value_parser)]
        layout: bool,

        /// Include all available fields
        #[clap(short, long, value_parser)]
        all: bool,

        /// Output as JSON
        #[clap(short, long, value_parser)]
        json: bool,
    },
    /// Get information about the currently active tab
    ///
    /// Returns: Tab name and ID by default, or full info in JSON
    CurrentTabInfo {
        /// Output as JSON with full TabInfo
        #[clap(short, long, value_parser)]
        json: bool,
    },
    TogglePanePinned {
        /// Target a specific pane by ID (eg. terminal_1, plugin_2, or 3)
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
    },
    /// Stack pane ids
    /// Ids are a space separated list of pane ids.
    /// They should either be in the form of `terminal_<int>` (eg. terminal_1), `plugin_<int>` (eg.
    /// plugin_1) or bare integers in which case they'll be considered terminals (eg. 1 is
    /// the equivalent of terminal_1)
    ///
    /// Example: zellij action stack-panes -- terminal_1 plugin_2 3
    StackPanes {
        #[clap(last(true), required(true))]
        pane_ids: Vec<String>,
    },
    ChangeFloatingPaneCoordinates {
        /// The pane_id of the floating pane, eg.  terminal_1, plugin_2 or 3 (equivalent to
        /// terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: String,
        /// The x coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long)]
        x: Option<String>,
        /// The y coordinates if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(short, long)]
        y: Option<String>,
        /// The width if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long)]
        width: Option<String>,
        /// The height if the pane is floating as a bare integer (eg. 1) or percent (eg. 10%)
        #[clap(long)]
        height: Option<String>,
        /// Whether to pin a floating pane so that it is always on top
        #[clap(long)]
        pinned: Option<bool>,
        /// change this pane to be with/without a border (warning: will make it impossible to move with the
        /// mouse if without a border)
        #[clap(short, long, value_parser)]
        borderless: Option<bool>,
    },
    TogglePaneBorderless {
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: String,
    },
    SetPaneBorderless {
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3)
        #[clap(short, long, value_parser)]
        pane_id: String,
        /// Whether the pane should be borderless (flag present) or bordered (flag absent)
        #[clap(short, long, value_parser)]
        borderless: bool,
    },
    /// Detach from the current session
    Detach,
    /// Switch the theme to dark (uses configured `theme_dark`).
    SetDarkTheme,
    /// Switch the theme to light (uses configured `theme_light`).
    SetLightTheme,
    /// Toggle between dark and light themes (used configured `theme_dark` and `theme_light`)
    ToggleTheme,
    /// Switch to a different session
    SwitchSession {
        /// Name of the session to switch to
        name: String,
        /// Optional tab position to focus
        #[clap(long)]
        tab_position: Option<usize>,
        /// Optional pane ID to focus (eg. "terminal_1" for terminal pane with id 1, or "plugin_2" for plugin pane with id 2)
        #[clap(long)]
        pane_id: Option<String>,
        /// Layout to apply when switching to the session (relative paths start at layout-dir)
        #[clap(short, long, value_parser, conflicts_with = "layout-string")]
        layout: Option<PathBuf>,
        /// Raw KDL layout string to use directly
        #[clap(long, value_parser, conflicts_with = "layout")]
        layout_string: Option<String>,
        /// Default folder to look for layouts
        #[clap(long, value_parser, requires("layout"))]
        layout_dir: Option<PathBuf>,
        /// Change the working directory when switching
        #[clap(short, long, value_parser)]
        cwd: Option<PathBuf>,
    },
    /// Set the default foreground/background color of a pane
    SetPaneColor {
        /// The pane_id of the pane, eg. terminal_1, plugin_2 or 3 (equivalent to terminal_3).
        /// Defaults to $ZELLIJ_PANE_ID if not provided.
        #[clap(short, long, value_parser)]
        pane_id: Option<String>,
        /// Foreground color (e.g. "#00e000", "rgb:00/e0/00")
        #[clap(long, value_parser)]
        fg: Option<String>,
        /// Background color (e.g. "#001a3a", "rgb:00/1a/3a")
        #[clap(long, value_parser)]
        bg: Option<String>,
        /// Reset pane colors to terminal defaults
        #[clap(long, value_parser, conflicts_with_all(&["fg", "bg"]))]
        reset: bool,
    },
}

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

    fn parse_subscribe(args: &[&str]) -> SubscribeCli {
        let mut full_args = vec!["zellij"];
        full_args.extend_from_slice(args);
        let cli = CliArgs::try_parse_from(full_args).unwrap();
        match cli.command {
            Some(Command::Subscribe(s)) => s,
            other => panic!("Expected Subscribe, got {:?}", other),
        }
    }

    #[test]
    fn subscribe_scrollback_bare_flag() {
        let s = parse_subscribe(&["subscribe", "--pane-id", "terminal_1", "--scrollback"]);
        assert_eq!(s.scrollback, Some(0));
    }

    #[test]
    fn subscribe_scrollback_with_value() {
        let s = parse_subscribe(&[
            "subscribe",
            "--pane-id",
            "terminal_1",
            "--scrollback",
            "100",
        ]);
        assert_eq!(s.scrollback, Some(100));
    }

    #[test]
    fn subscribe_scrollback_absent() {
        let s = parse_subscribe(&["subscribe", "--pane-id", "terminal_1"]);
        assert_eq!(s.scrollback, None);
    }

    #[test]
    fn subscribe_format_json() {
        let s = parse_subscribe(&["subscribe", "--pane-id", "terminal_1", "--format", "json"]);
        assert!(matches!(s.format, SubscribeFormat::Json));
    }

    #[test]
    fn subscribe_format_default_raw() {
        let s = parse_subscribe(&["subscribe", "--pane-id", "terminal_1"]);
        assert!(matches!(s.format, SubscribeFormat::Raw));
    }

    #[test]
    fn subscribe_multiple_pane_ids() {
        let s = parse_subscribe(&[
            "subscribe",
            "--pane-id",
            "terminal_1",
            "--pane-id",
            "plugin_2",
        ]);
        assert_eq!(
            s.pane_id,
            vec!["terminal_1".to_string(), "plugin_2".to_string()]
        );
    }

    #[test]
    fn subscribe_requires_pane_id() {
        let result = CliArgs::try_parse_from(["zellij", "subscribe"]);
        assert!(result.is_err());
    }
}