sway-groups-cli 0.2.0

Command-line tool for managing sway workspaces in named groups.
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
//! CLI commands for swayg.

use clap::{Parser, Subcommand};
use std::path::PathBuf;
use sway_groups_core::services::{GroupService, NavigationService, WaybarSyncService, WorkspaceService};
use sway_groups_core::sway::SwayIpcClient;

#[derive(Parser)]
#[command(name = "swayg")]
#[command(author, version, about = "Sway workspace groups management CLI")]
pub struct Cli {
    /// Enable verbose logging (info/debug to stderr).
    #[arg(short, long)]
    pub verbose: bool,

    /// Path to the database file. Overrides the default location.
    #[arg(short, long, env = "SWAYG_DB")]
    pub db: Option<PathBuf>,

    /// Path to the config file. Overrides the default location.
    #[arg(short, long, env = "SWAYG_CONFIG")]
    pub config: Option<PathBuf>,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Manage groups (named workspace collections).
    Group {
        #[command(subcommand)]
        action: GroupAction,
    },
    /// Manage workspaces and their group membership.
    Workspace {
        #[command(subcommand)]
        action: WorkspaceAction,
    },
    /// Navigate between workspaces.
    Nav {
        #[command(subcommand)]
        action: NavAction,
    },
    /// Move the focused container between workspaces.
    Container {
        #[command(subcommand)]
        action: ContainerAction,
    },
    /// Synchronize state between sway, the DB, and the waybar bars.
    Sync {
        /// Sync workspaces, groups, and outputs from sway.
        #[arg(short, long)]
        all: bool,

        /// Sync only workspaces from sway into the DB.
        #[arg(short, long)]
        workspaces: bool,

        /// Sync only groups.
        #[arg(short, long)]
        groups: bool,

        /// Sync only outputs from sway.
        #[arg(short, long)]
        outputs: bool,

        /// Run repair pass (reconcile DB with sway, prune stale entries).
        #[arg(long)]
        repair: bool,

        /// Force a full waybar bar refresh; use with retries/delay to wait for
        /// waybar startup.
        #[arg(long)]
        init_bars: bool,

        /// Max retry attempts when the waybar socket isn't available yet.
        #[arg(long, default_value = "5")]
        init_bars_retries: u32,

        /// Delay in milliseconds between waybar-socket retry attempts.
        #[arg(long, default_value = "200")]
        init_bars_delay_ms: u64,
    },
    /// Initialize the database and sync current sway state.
    Init {
        /// Restart the swayg-daemon systemd user service after init.
        #[arg(long)]
        restart_daemon_service: bool,
    },
    /// Repair DB state by reconciling with sway (removes stale entries).
    Repair,
    /// Show current group/workspace status per output.
    Status,
    /// Configuration file operations.
    Config {
        #[command(subcommand)]
        action: ConfigAction,
    },
}

#[derive(Subcommand)]
enum ConfigAction {
    /// Write the default configuration to stdout or a file.
    Dump {
        /// Write to this file instead of stdout.
        #[arg(short, long)]
        output: Option<PathBuf>,
    },
}

#[derive(Subcommand)]
enum GroupAction {
    /// List all groups.
    List {
        /// Only list groups that have workspaces on this output.
        #[arg(short, long)]
        output: Option<String>,
    },
    /// Create a new group.
    Create {
        /// Name of the new group.
        name: String,
    },
    /// Delete a group.
    Delete {
        /// Group name to delete.
        name: String,
        /// Delete even if the group has workspaces; orphans move to default_group.
        #[arg(short, long)]
        force: bool,
    },
    /// Rename an existing group.
    Rename {
        /// Current group name.
        old_name: String,
        /// New group name.
        new_name: String,
    },
    /// Make a group the active group for an output.
    Select {
        /// Group to activate.
        group: String,
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Create the group if it does not exist yet.
        #[arg(short, long)]
        create: bool,
    },
    /// Print the active group of an output.
    Active {
        /// Output to query.
        output: String,
    },
    /// Switch to the next group (alphabetical).
    Next {
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Wrap around past the last group.
        #[arg(short, long)]
        wrap: bool,
    },
    /// Switch to the next group in the current output's group list.
    NextOnOutput {
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Wrap around past the last group.
        #[arg(short, long)]
        wrap: bool,
    },
    /// Switch to the previous group (alphabetical).
    Prev {
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Wrap around past the first group.
        #[arg(short, long)]
        wrap: bool,
    },
    /// Switch to the previous group in the current output's group list.
    PrevOnOutput {
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Wrap around past the first group.
        #[arg(short, long)]
        wrap: bool,
    },
    /// Delete all empty groups.
    Prune {
        /// Do not delete these groups even if they are empty.
        #[arg(long)]
        keep: Vec<String>,
    },
    /// Unhide all hidden workspaces in a group.
    UnhideAll {
        /// Group (default: active group on focused output).
        group: Option<String>,
    },
}

#[derive(Subcommand)]
enum WorkspaceAction {
    /// List workspaces.
    List {
        /// Filter by output.
        #[arg(short, long)]
        output: Option<String>,
        /// Filter by group.
        #[arg(short, long)]
        group: Option<String>,
        /// Only show workspaces visible in the active group.
        #[arg(long)]
        visible: bool,
        /// Plain output (space-separated names) instead of formatted list.
        #[arg(long)]
        plain: bool,
        /// Include group membership per workspace.
        #[arg(long)]
        groups: bool,
        /// One workspace per line.
        #[arg(long)]
        flatten: bool,
    },
    /// Add a workspace to a group.
    Add {
        /// Workspace name.
        workspace: String,
        /// Group (default: active group on focused output).
        #[arg(short, long)]
        group: Option<String>,
    },
    /// Move a workspace to a set of groups (removing from all others).
    Move {
        /// Workspace name.
        workspace: String,
        /// Comma-separated list of target groups.
        #[arg(short, long)]
        groups: String,
    },
    /// Remove a workspace from a group.
    Remove {
        /// Workspace name.
        workspace: String,
        /// Group (default: active group on focused output).
        #[arg(short, long)]
        group: Option<String>,
    },
    /// Rename a workspace. If the target exists, merges source into target.
    Rename {
        /// Current workspace name.
        old_name: String,
        /// New workspace name.
        new_name: String,
    },
    /// List groups a workspace belongs to.
    Groups {
        /// Workspace name.
        workspace: String,
    },
    /// Mark a workspace as global (visible in all groups).
    Global {
        /// Workspace name (default: focused workspace).
        workspace: Option<String>,
        /// Toggle current state instead of always setting true.
        #[arg(short, long)]
        toggle: bool,
    },
    /// Remove global status from a workspace.
    Unglobal {
        /// Workspace name (default: focused workspace).
        workspace: Option<String>,
    },
    /// Mark a workspace as hidden in a specific group.
    Hide {
        /// Workspace name (default: focused workspace).
        workspace: Option<String>,
        /// Group (default: active group on focused output).
        #[arg(short, long)]
        group: Option<String>,
        /// Toggle current hidden state instead of always setting true.
        #[arg(short, long)]
        toggle: bool,
    },
    /// Remove the hidden flag from a workspace in a group.
    Unhide {
        /// Workspace name (default: focused workspace).
        workspace: Option<String>,
        /// Group (default: active group on focused output).
        #[arg(short, long)]
        group: Option<String>,
    },
    /// Toggle or enable the global show_hidden_workspaces DB flag.
    ///
    /// When true, hidden workspaces are emitted to waybar with the CSS class
    /// "hidden" and included in navigation. Default: false.
    ShowHidden {
        /// Toggle current state; otherwise sets true.
        #[arg(short, long)]
        toggle: bool,
    },
}

#[derive(Subcommand)]
enum NavAction {
    /// Focus the next visible workspace.
    Next {
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Wrap around past the last workspace.
        #[arg(short, long)]
        wrap: bool,
        /// Include workspaces from all outputs.
        #[arg(long)]
        all_outputs: bool,
    },
    /// Focus the next visible workspace on the same output.
    NextOnOutput {
        /// Wrap around past the last workspace on this output.
        #[arg(short, long)]
        wrap: bool,
    },
    /// Focus the previous visible workspace.
    Prev {
        /// Target output (default: focused output).
        #[arg(short, long)]
        output: Option<String>,
        /// Wrap around past the first workspace.
        #[arg(short, long)]
        wrap: bool,
        /// Include workspaces from all outputs.
        #[arg(long)]
        all_outputs: bool,
    },
    /// Focus the previous visible workspace on the same output.
    PrevOnOutput {
        /// Wrap around past the first workspace on this output.
        #[arg(short, long)]
        wrap: bool,
    },
    /// Focus a specific workspace (works even for hidden workspaces).
    Go {
        /// Target workspace name.
        workspace: String,
        /// Move workspace to this output if it doesn't exist yet.
        #[arg(short, long)]
        output: Option<String>,
    },
    /// Focus the previously focused workspace.
    Back,
}

#[derive(Subcommand)]
enum ContainerAction {
    /// Move the focused container to a workspace.
    Move {
        /// Target workspace name.
        workspace: String,
        /// After moving, follow the container by switching to that workspace.
        #[arg(long)]
        switch_to_workspace: bool,
    },
}

pub async fn run(
    cli: Cli,
    group_service: &GroupService,
    workspace_service: &WorkspaceService,
    waybar_sync: &WaybarSyncService,
    nav_service: &NavigationService,
    ipc_client: &SwayIpcClient,
    db_path: PathBuf,
) -> anyhow::Result<()> {
    match cli.command {
        Command::Group { action } => run_group(action, group_service, workspace_service, waybar_sync, ipc_client).await?,
        Command::Workspace { action } => run_workspace(action, workspace_service, group_service, nav_service, waybar_sync, ipc_client).await?,
        Command::Nav { action } => run_nav(action, nav_service, workspace_service, waybar_sync, ipc_client).await?,
        Command::Container { action } => run_container(action, nav_service, group_service, workspace_service, waybar_sync, ipc_client).await?,
        Command::Sync { all, workspaces, groups, outputs, repair, init_bars, init_bars_retries, init_bars_delay_ms } => {
            run_sync(all, workspaces, groups, outputs, repair, init_bars, init_bars_retries, init_bars_delay_ms, workspace_service, group_service, waybar_sync).await?;
        }
        Command::Init { restart_daemon_service } => {
            run_init(db_path, workspace_service, group_service, waybar_sync, restart_daemon_service).await?;
        }
        Command::Repair => {
            run_repair(workspace_service, group_service, waybar_sync, ipc_client).await?;
        }
        Command::Status => {
            run_status(group_service, workspace_service, waybar_sync, ipc_client).await?;
        }
        Command::Config { action } => {
            run_config(action)?;
        }
    }
    Ok(())
}

fn resolve_output(output: Option<&str>, ipc_client: &SwayIpcClient) -> anyhow::Result<String> {
    match output {
        Some(o) => Ok(o.to_string()),
        None => {
            let primary = ipc_client.get_primary_output()?;
            Ok(primary)
        }
    }
}

async fn resolve_group_output(
    explicit_output: Option<&str>,
    group: &str,
    group_service: &GroupService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<String> {
    if let Some(output) = explicit_output {
        return Ok(output.to_string());
    }
    if let Some(output) = group_service.find_last_visited_output(group).await? {
        return Ok(output);
    }
    Ok(ipc_client.get_primary_output()?)
}

async fn run_group(
    action: GroupAction,
    group_service: &GroupService,
    workspace_service: &WorkspaceService,
    waybar_sync: &WaybarSyncService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<()> {
    match action {
        GroupAction::List { output } => {
            let groups = group_service.list_groups(output.as_deref()).await?;
            if groups.is_empty() {
                println!("No groups found.");
            } else {
                for group in &groups {
                    println!("Group \"{}\":", group.name);
                    if group.workspaces.is_empty() {
                        println!("  (empty)");
                    } else {
                        for ws in &group.workspaces {
                            println!("  - {}", ws);
                        }
                    }
                }
            }
        }
        GroupAction::Create { name } => {
            group_service.create_group(&name).await?;
            waybar_sync.update_waybar_groups().await?;
            println!("Created group \"{}\"", name);
        }
        GroupAction::Delete { name, force } => {
            group_service.delete_group(&name, force).await?;
            waybar_sync.update_waybar_groups().await?;
            println!("Deleted group \"{}\"", name);
        }
        GroupAction::Rename { old_name, new_name } => {
            group_service.rename_group(&old_name, &new_name).await?;
            waybar_sync.update_waybar_groups().await?;
            println!("Renamed group \"{}\" to \"{}\"", old_name, new_name);
        }
        GroupAction::Select { output, group, create } => {
            if create {
                let groups = group_service.list_all_group_names().await?;
                if !groups.iter().any(|g| g == &group) {
                    group_service.create_group(&group).await?;
                    println!("Created group \"{}\"", group);
                }
            }
            let resolved_output = resolve_group_output(output.as_deref(), &group, group_service, ipc_client).await?;
            group_service.set_active_group(&resolved_output, &group).await?;
            waybar_sync.update_waybar().await?;
            waybar_sync.update_waybar_groups().await?;
            println!("Set active group for \"{}\" to \"{}\"", resolved_output, group);
        }
        GroupAction::Active { output } => {
            let active = group_service.get_active_group(&output).await.unwrap_or(None);
            println!("{}", active.unwrap_or_default());
        }
        GroupAction::Next { output, wrap } => {
            let current_output = resolve_output(output.as_deref(), ipc_client)?;
            if let Some(next_name) = group_service.next_group_name(&current_output, wrap).await? {
                let resolved_output = resolve_group_output(None, &next_name, group_service, ipc_client).await?;
                group_service.set_active_group(&resolved_output, &next_name).await?;
                waybar_sync.update_waybar().await?;
                waybar_sync.update_waybar_groups().await?;
                println!("Switched from active group to \"{}\"", next_name);
            }
        }
        GroupAction::NextOnOutput { output, wrap } => {
            let output = resolve_output(output.as_deref(), ipc_client)?;
            if let Some(next) = group_service.next_group_on_output(&output, wrap).await? {
                waybar_sync.update_waybar().await?;
                waybar_sync.update_waybar_groups().await?;
                println!("Switched from active group to \"{}\"", next);
            }
        }
        GroupAction::Prev { output, wrap } => {
            let current_output = resolve_output(output.as_deref(), ipc_client)?;
            if let Some(prev_name) = group_service.prev_group_name(&current_output, wrap).await? {
                let resolved_output = resolve_group_output(None, &prev_name, group_service, ipc_client).await?;
                group_service.set_active_group(&resolved_output, &prev_name).await?;
                waybar_sync.update_waybar().await?;
                waybar_sync.update_waybar_groups().await?;
                println!("Switched from active group to \"{}\"", prev_name);
            }
        }
        GroupAction::PrevOnOutput { output, wrap } => {
            let output = resolve_output(output.as_deref(), ipc_client)?;
            if let Some(prev) = group_service.prev_group_on_output(&output, wrap).await? {
                waybar_sync.update_waybar().await?;
                waybar_sync.update_waybar_groups().await?;
                println!("Switched from active group to \"{}\"", prev);
            }
        }
        GroupAction::Prune { keep } => {
            let removed = group_service.prune_groups(&keep).await?;
            if removed == 0 {
                println!("No empty groups to prune.");
            } else {
                waybar_sync.update_waybar_groups().await?;
                println!("Pruned {} empty group(s)", removed);
            }
        }
        GroupAction::UnhideAll { group } => {
            let group_name = match resolve_group(group.as_deref(), group_service, ipc_client).await? {
                Some(g) => g,
                None => return Ok(()),
            };
            let removed = workspace_service.unhide_all_in_group(&group_name).await?;
            waybar_sync.update_waybar().await?;
            if removed == 0 {
                println!("No hidden workspaces in group \"{}\"", group_name);
            } else {
                println!("Unhid {} workspace(s) in group \"{}\"", removed, group_name);
            }
        }
    }
    Ok(())
}

async fn run_workspace(
    action: WorkspaceAction,
    workspace_service: &WorkspaceService,
    group_service: &GroupService,
    nav_service: &NavigationService,
    waybar_sync: &WaybarSyncService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<()> {
    match action {
        WorkspaceAction::List { output, group, visible, plain, groups, flatten } => {
            if visible {
                let output_name = output.as_deref()
                    .map(|s| s.to_string())
                    .or_else(|| ipc_client.get_primary_output().ok())
                    .unwrap_or_default();
                let workspaces = workspace_service.list_visible_workspaces(&output_name).await?;
                if workspaces.is_empty() {
                    if !plain {
                        println!("No visible workspaces found.");
                    }
                } else {
                    for ws in &workspaces {
                        println!("{}", ws);
                    }
                }
            } else {
                let workspaces = workspace_service.list_workspaces(output.as_deref(), group.as_deref()).await?;
                if workspaces.is_empty() {
                    if !plain {
                        println!("No workspaces found.");
                    }
                } else {
                    let active_group_name = if group.is_none() {
                        let output_name = output.as_deref()
                            .map(|s| s.to_string())
                            .or_else(|| ipc_client.get_primary_output().ok());
                        match output_name {
                            Some(ref out) => group_service.get_active_group(out).await.ok(),
                            None => None,
                        }
                    } else {
                        None
                    };

                    if !plain {
                        let group_label = group.as_deref().unwrap_or("active");
                        let output_label = output.as_deref().unwrap_or("all");
                        println!("Workspaces in group \"{}\" on \"{}\":", group_label, output_label);
                    }
                    for ws in &workspaces {
                        if plain && groups && flatten {
                            let mut sorted_groups: Vec<&String> = ws.groups.iter().collect();
                            sorted_groups.sort_by(|a, b| {
                                if let Some(ref active) = active_group_name {
                                    if active.as_deref() == Some(a.as_str()) { return std::cmp::Ordering::Less; }
                                    if active.as_deref() == Some(b.as_str()) { return std::cmp::Ordering::Greater; }
                                }
                                a.cmp(b)
                            });
                            for g in &sorted_groups {
                                println!("{}│{}", ws.name, g);
                            }
                        } else if plain && groups {
                            let groups_str = ws.groups.join(",");
                            if groups_str.is_empty() {
                                println!("{}│", ws.name);
                            } else {
                                println!("{}│{}", ws.name, groups_str);
                            }
                        } else if plain {
                            println!("{}", ws.name);
                        } else {
                            let status = if ws.is_global {
                                "(global)"
                            } else if let Some(ref active) = active_group_name {
                                if ws.groups.iter().any(|g| Some(g.as_str()) == active.as_deref()) {
                                    "(visible)"
                                } else if !ws.groups.is_empty() {
                                    "(hidden)"
                                } else {
                                    "(visible)"
                                }
                            } else {
                                ""
                            };
                            println!("  {:20} {}", ws.name, status);
                        }
                    }
                }
            }
        }
        WorkspaceAction::Add { workspace, group } => {
            let target_group = match &group {
                Some(g) => g.clone(),
                None => {
                    let output_name = ipc_client.get_primary_output().ok();
                    match output_name {
                        Some(ref out) => {
                            match group_service.get_active_group(out).await.unwrap_or(None) {
                                Some(g) => g,
                                None => {
                                    eprintln!("No active group for output '{}'. Specify a group explicitly.", out);
                                    return Ok(());
                                }
                            }
                        }
                        None => {
                            eprintln!("Cannot determine output. Specify a group explicitly.");
                            return Ok(());
                        }
                    }
                }
            };
            workspace_service.add_to_group(&workspace, &target_group).await?;
            waybar_sync.update_waybar().await?;
            println!("Added workspace \"{}\" to group \"{}\"", workspace, target_group);
        }
        WorkspaceAction::Move { workspace, groups } => {
            let target_groups: Vec<&str> = groups.split(',').map(|g| g.trim()).filter(|g| !g.is_empty()).collect();
            if target_groups.is_empty() {
                anyhow::bail!("No groups specified for move. Use --groups <group1,group2,...>");
            }
            workspace_service.move_to_groups(&workspace, &target_groups).await?;
            waybar_sync.update_waybar().await?;
            println!("Moved workspace \"{}\" to group(s): {}", workspace, target_groups.join(", "));
        }
        WorkspaceAction::Remove { workspace, group } => {
            let source_group = match &group {
                Some(g) => g.clone(),
                None => {
                    let output_name = ipc_client.get_primary_output().ok();
                    match output_name {
                        Some(ref out) => {
                            match group_service.get_active_group(out).await.unwrap_or(None) {
                                Some(g) => g,
                                None => {
                                    eprintln!("No active group for output '{}'. Specify a group explicitly.", out);
                                    return Ok(());
                                }
                            }
                        }
                        None => {
                            eprintln!("Cannot determine output. Specify a group explicitly.");
                            return Ok(());
                        }
                    }
                }
            };
            workspace_service.remove_from_group(&workspace, &source_group).await?;
            waybar_sync.update_waybar().await?;
            println!("Removed workspace \"{}\" from group \"{}\"", workspace, source_group);
        }
        WorkspaceAction::Rename { old_name, new_name } => {
            let pending_id = workspace_service.register_pending_event(&new_name, "rename").await.ok();
            let result: anyhow::Result<()> = async {
                let merged = workspace_service.rename_workspace(&old_name, &new_name).await?;
                waybar_sync.update_waybar().await?;
                if merged {
                    println!("Merged workspace \"{}\" into \"{}\"", old_name, new_name);
                } else {
                    println!("Renamed workspace \"{}\" to \"{}\"", old_name, new_name);
                }
                Ok(())
            }.await;
            if let Some(id) = pending_id {
                workspace_service.remove_pending_event(id).await.ok();
            }
            result?;
        }
        WorkspaceAction::Global { workspace, toggle } => {
            let ws = match workspace {
                Some(w) => w,
                None => ipc_client.get_focused_workspace().map(|ws| ws.name).unwrap_or_default(),
            };
            let make_global = if toggle {
                !workspace_service.is_global(&ws).await.unwrap_or(false)
            } else {
                true
            };
            workspace_service.set_global(&ws, make_global).await?;
            waybar_sync.update_waybar().await?;
            if make_global {
                println!("Marked workspace \"{}\" as global", ws);
            } else {
                println!("Removed global status from workspace \"{}\"", ws);
            }
        }
        WorkspaceAction::Unglobal { workspace } => {
            let ws = match workspace {
                Some(w) => w,
                None => ipc_client.get_focused_workspace().map(|ws| ws.name).unwrap_or_default(),
            };
            workspace_service.set_global(&ws, false).await?;
            waybar_sync.update_waybar().await?;
            println!("Removed global status from workspace \"{}\"", ws);
        }
        WorkspaceAction::Groups { workspace } => {
            let groups = workspace_service.get_groups_for_workspace(&workspace).await?;
            if groups.is_empty() {
                println!("Workspace \"{}\" is not in any group.", workspace);
            } else {
                println!("Workspace \"{}\" is in groups: {}", workspace,
                    groups.iter().map(|g| format!("\"{}\"", g)).collect::<Vec<_>>().join(", "));
            }
        }
        WorkspaceAction::Hide { workspace, group, toggle } => {
            let ws = match workspace {
                Some(w) => w,
                None => ipc_client.get_focused_workspace().map(|w| w.name).unwrap_or_default(),
            };
            let group_name = match resolve_group(group.as_deref(), group_service, ipc_client).await? {
                Some(g) => g,
                None => return Ok(()),
            };
            let new_state = if toggle {
                !workspace_service.is_hidden(&ws, &group_name).await.unwrap_or(false)
            } else {
                true
            };
            workspace_service.set_hidden(&ws, &group_name, new_state).await?;
            if new_state && !workspace_service.get_show_hidden().await? {
                focus_away_from_hidden(&ws, ipc_client, nav_service).await?;
            }
            waybar_sync.update_waybar().await?;
            if new_state {
                println!("Hid workspace \"{}\" in group \"{}\"", ws, group_name);
            } else {
                println!("Unhid workspace \"{}\" in group \"{}\"", ws, group_name);
            }
        }
        WorkspaceAction::Unhide { workspace, group } => {
            let ws = match workspace {
                Some(w) => w,
                None => ipc_client.get_focused_workspace().map(|w| w.name).unwrap_or_default(),
            };
            let group_name = match resolve_group(group.as_deref(), group_service, ipc_client).await? {
                Some(g) => g,
                None => return Ok(()),
            };
            workspace_service.set_hidden(&ws, &group_name, false).await?;
            waybar_sync.update_waybar().await?;
            println!("Unhid workspace \"{}\" in group \"{}\"", ws, group_name);
        }
        WorkspaceAction::ShowHidden { toggle } => {
            let new_value = if toggle {
                !workspace_service.get_show_hidden().await?
            } else {
                true
            };
            workspace_service.set_show_hidden(new_value).await?;
            if !new_value {
                // show_hidden turned off — if focused workspace is hidden, move away
                if let Ok(focused) = ipc_client.get_focused_workspace() {
                    let output = ipc_client.get_primary_output().unwrap_or_default();
                    let group_name = group_service.get_active_group(&output).await.unwrap_or(None);
                    if let Some(ref gn) = group_name {
                        if workspace_service.is_hidden(&focused.name, gn).await.unwrap_or(false) {
                            focus_away_from_hidden(&focused.name, ipc_client, nav_service).await?;
                        }
                    }
                }
            }
            waybar_sync.update_waybar().await?;
            println!("show_hidden_workspaces = {}", new_value);
        }
    }
    Ok(())
}

/// Resolve a group argument: if `Some`, returns it as-is; otherwise resolves to
/// the active group of the focused output. Prints an error and returns `None`
/// if resolution fails.
async fn resolve_group(
    explicit: Option<&str>,
    group_service: &GroupService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<Option<String>> {
    if let Some(g) = explicit {
        return Ok(Some(g.to_string()));
    }
    let output = match ipc_client.get_primary_output().ok() {
        Some(o) => o,
        None => {
            eprintln!("Cannot determine output. Specify a group explicitly.");
            return Ok(None);
        }
    };
    match group_service.get_active_group(&output).await.unwrap_or(None) {
        Some(g) => Ok(Some(g)),
        None => {
            eprintln!("No active group for output '{}'. Specify a group explicitly.", output);
            Ok(None)
        }
    }
}

/// If the focused workspace matches `hidden_ws`, focus the first visible
/// workspace on the same output. The current workspace is already hidden in
/// the DB, so the visible list will not contain it.
async fn focus_away_from_hidden(
    hidden_ws: &str,
    ipc_client: &SwayIpcClient,
    nav_service: &NavigationService,
) -> anyhow::Result<()> {
    let focused = ipc_client.get_focused_workspace().map(|w| w.name).unwrap_or_default();
    if focused != hidden_ws {
        return Ok(());
    }
    let output = ipc_client.get_primary_output().unwrap_or_default();
    let visible = nav_service.get_visible_workspaces(&output).await?;
    if let Some(target) = visible.first() {
        nav_service.focus_workspace(target).await?;
    }
    Ok(())
}

async fn run_nav(
    action: NavAction,
    nav_service: &NavigationService,
    workspace_service: &WorkspaceService,
    waybar_sync: &WaybarSyncService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<()> {
    match action {
        NavAction::Next { output, wrap, all_outputs } => {
            let output = resolve_output(output.as_deref(), ipc_client)?;
            if all_outputs {
                if let Some(target) = nav_service.next_workspace_all_outputs(&output, wrap).await? {
                    waybar_sync.update_waybar().await?;
                    println!("Navigated to \"{}\"", target);
                }
            } else if let Some(target) = nav_service.next_workspace(&output, wrap).await? {
                waybar_sync.update_waybar().await?;
                println!("Navigated to \"{}\"", target);
            }
        }
        NavAction::NextOnOutput { wrap } => {
            if let Some(target) = nav_service.next_workspace_global(wrap).await? {
                waybar_sync.update_waybar().await?;
                println!("Navigated to \"{}\"", target);
            }
        }
        NavAction::Prev { output, wrap, all_outputs } => {
            let output = resolve_output(output.as_deref(), ipc_client)?;
            if all_outputs {
                if let Some(target) = nav_service.prev_workspace_all_outputs(&output, wrap).await? {
                    waybar_sync.update_waybar().await?;
                    println!("Navigated to \"{}\"", target);
                }
            } else if let Some(target) = nav_service.prev_workspace(&output, wrap).await? {
                waybar_sync.update_waybar().await?;
                println!("Navigated to \"{}\"", target);
            }
        }
        NavAction::PrevOnOutput { wrap } => {
            if let Some(target) = nav_service.prev_workspace_global(wrap).await? {
                waybar_sync.update_waybar().await?;
                println!("Navigated to \"{}\"", target);
            }
        }
        NavAction::Go { workspace, output: _ } => {
            let is_new_workspace = ipc_client.get_workspaces()
                .map(|ws| !ws.iter().any(|w| w.name == workspace))
                .unwrap_or(true);

            let pending_id = if is_new_workspace {
                workspace_service.register_pending_event(&workspace, "add").await.ok()
            } else {
                None
            };

            let result: anyhow::Result<()> = async {
                nav_service.go_workspace(&workspace).await?;
                waybar_sync.update_waybar().await?;
                println!("Navigated to \"{}\"", workspace);
                Ok(())
            }.await;

            if let Some(id) = pending_id {
                workspace_service.remove_pending_event(id).await.ok();
            }

            result?;
        }
        NavAction::Back => {
            if let Some(target) = nav_service.go_back().await? {
                waybar_sync.update_waybar().await?;
                println!("Navigated back to \"{}\"", target);
            } else {
                println!("No previous workspace found.");
            }
        }
    }
    Ok(())
}

async fn run_container(
    action: ContainerAction,
    nav_service: &NavigationService,
    group_service: &GroupService,
    workspace_service: &WorkspaceService,
    waybar_sync: &WaybarSyncService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<()> {
    match action {
        ContainerAction::Move { workspace, switch_to_workspace } => {
            let is_new_workspace = ipc_client.get_workspaces()
                .map(|ws| !ws.iter().any(|w| w.name == workspace))
                .unwrap_or(true);

            let pending_id = if is_new_workspace {
                workspace_service.register_pending_event(&workspace, "add").await.ok()
            } else {
                None
            };

            let result: anyhow::Result<()> = async {
                nav_service.move_to_workspace(&workspace).await?;

                if switch_to_workspace {
                    nav_service.focus_workspace(&workspace).await?;

                    if let Ok(groups) = workspace_service.get_groups_for_workspace(&workspace).await
                        && !groups.is_empty()
                            && let Ok(output) = ipc_client.get_primary_output() {
                                let current = group_service.get_active_group(&output).await.unwrap_or(None);
                                if !groups.iter().any(|g| current.as_deref() == Some(g.as_str())) {
                                    group_service.set_active_group_db_only(&output, &groups[0]).await?;
                                }
                            }
                }

                waybar_sync.update_waybar().await?;
                println!("Moved container to \"{}\"", workspace);
                Ok(())
            }.await;

            if let Some(id) = pending_id {
                workspace_service.remove_pending_event(id).await.ok();
            }

            result?;
        }
    }
    Ok(())
}

// Args mirror the `Command::Sync` clap variant 1:1; bundling them into a
// struct would just add an extra layer of packing/unpacking.
#[allow(clippy::too_many_arguments)]
async fn run_sync(
    all: bool,
    workspaces: bool,
    groups: bool,
    outputs: bool,
    repair: bool,
    init_bars: bool,
    init_bars_retries: u32,
    init_bars_delay_ms: u64,
    workspace_service: &WorkspaceService,
    group_service: &GroupService,
    waybar_sync: &WaybarSyncService,
) -> anyhow::Result<()> {
    let mut synced_ws = false;
    let mut synced_gr = false;
    let mut synced_out = false;

    if all || workspaces {
        workspace_service.sync_from_sway().await?;
        synced_ws = true;
    }
    if all || groups {
        synced_gr = true;
    }
    if all || outputs {
        synced_out = true;
    }

    if !all && !workspaces && !groups && !outputs && !repair && !init_bars {
        workspace_service.sync_from_sway().await?;
        synced_ws = true;
        synced_gr = true;
        synced_out = true;
    }

    if repair {
        let (removed_ws, added_ws, removed_groups) = workspace_service.repair(group_service).await?;
        println!("Repair complete:");
        println!("  Workspaces removed from DB: {}", removed_ws);
        if added_ws > 0 {
            let dg = group_service.default_group().to_string();
            if group_service.list_groups(None).await?.iter().all(|g| g.name != dg) {
                group_service.create_group(&dg).await?;
            }
            println!("  Workspaces added to group '{}': {}", dg, added_ws);
        }
        println!("  Empty groups removed: {}", removed_groups);
    }

    if init_bars {
        let delay = std::time::Duration::from_millis(init_bars_delay_ms);
        waybar_sync.update_waybar_with_retry(init_bars_retries, delay).await?;
        waybar_sync.update_waybar_groups_with_retry(init_bars_retries, delay).await?;
        println!("Bars initialized (retries={}, delay={}ms).", init_bars_retries, init_bars_delay_ms);
    } else {
        waybar_sync.update_waybar().await?;
        waybar_sync.update_waybar_groups().await?;
    }

    let mut parts = Vec::new();
    if synced_ws {
        parts.push("workspaces");
    }
    if synced_gr {
        parts.push("groups");
    }
    if synced_out {
        parts.push("outputs");
    }
    if repair {
        parts.push("repair");
    }
    if init_bars {
        parts.push("bars");
    }
    println!("Synced: {}", parts.join(", "));

    Ok(())
}

async fn run_init(
    db_path: PathBuf,
    _workspace_service: &WorkspaceService,
    _group_service: &GroupService,
    _waybar_sync: &WaybarSyncService,
    restart_daemon_service: bool,
) -> anyhow::Result<()> {
    if db_path.exists() {
        std::fs::remove_file(&db_path)?;
        println!("Removed existing database.");
    }

    let db = sway_groups_core::db::DatabaseManager::new(db_path).await?;
    let ipc = sway_groups_core::sway::SwayIpcClient::new()?;
    let group_svc = GroupService::new(db.clone(), ipc.clone());
    let workspace_svc = WorkspaceService::new(db.clone(), ipc.clone());
    let waybar_sync_svc = WaybarSyncService::new(db.clone(), ipc.clone(), sway_groups_core::sway::WaybarClient::new());

    workspace_svc.sync_from_sway().await?;
    waybar_sync_svc.update_waybar().await?;
    waybar_sync_svc.update_waybar_groups().await?;

    if group_svc.list_groups(None).await?.is_empty() {
        let dg = group_svc.default_group().to_string();
        group_svc.create_group(&dg).await?;
        println!("Created default group '{}' (no groups found after sync).", dg);
    }

    println!("Initialized: created database, synced workspaces and outputs.");

    if restart_daemon_service {
        let result = std::process::Command::new("systemctl")
            .args(["--user", "restart", "swayg-daemon.service"])
            .output()?;
        if result.status.success() {
            println!("Restarted swayg-daemon service.");
        } else {
            eprintln!("Failed to restart swayg-daemon service: {}",
                String::from_utf8_lossy(&result.stderr));
        }
    }

    Ok(())
}

async fn run_repair(
    workspace_service: &WorkspaceService,
    group_service: &GroupService,
    waybar_sync: &WaybarSyncService,
    _ipc_client: &SwayIpcClient,
) -> anyhow::Result<()> {
    let (removed_ws, added_ws, removed_groups) = workspace_service.repair(group_service).await?;

    if added_ws > 0 {
        let dg = group_service.default_group().to_string();
        if group_service.list_groups(None).await?.iter().all(|g| g.name != dg) {
            group_service.create_group(&dg).await?;
        }
    }
    waybar_sync.update_waybar().await?;

    println!("Repair complete:");
    println!("  Workspaces removed from DB: {}", removed_ws);
    println!("  Workspaces added to group '0': {}", added_ws);
    println!("  Empty groups removed: {}", removed_groups);

    Ok(())
}

async fn run_status(
    group_service: &GroupService,
    workspace_service: &WorkspaceService,
    _waybar_sync: &WaybarSyncService,
    ipc_client: &SwayIpcClient,
) -> anyhow::Result<()> {
    let outputs = ipc_client.get_outputs()?;

    let show_hidden = workspace_service.get_show_hidden().await.unwrap_or(false);
    println!("show_hidden_workspaces = {}", show_hidden);

    for output in &outputs {
        let active_group = group_service.get_active_group(&output.name).await
            .unwrap_or(None);
        println!("{}: active group = \"{}\"", output.name, active_group.as_deref().unwrap_or("(none)"));

        let visible = workspace_service.list_visible_workspaces(&output.name).await?;

        let all_ws = workspace_service.list_workspaces(Some(&output.name), None).await?;

        let mut inactive = Vec::new();
        let mut global_ws = Vec::new();
        let mut visible_names = Vec::new();
        let mut hidden_names: Vec<String> = Vec::new();

        for ws in &all_ws {
            if ws.is_global {
                global_ws.push(ws.name.clone());
            }
        }

        // Determine user-hidden workspaces for the active group (if any).
        if let Some(ref ag) = active_group {
            for ws in &all_ws {
                if workspace_service.is_hidden(&ws.name, ag).await.unwrap_or(false) {
                    hidden_names.push(ws.name.clone());
                }
            }
        }

        for ws in &visible {
            if all_ws.iter().any(|w| w.name == *ws) {
                if all_ws.iter().any(|w| w.name == *ws && w.is_global) {
                    global_ws.push(ws.clone());
                } else {
                    visible_names.push(ws.clone());
                }
            }
        }

        let sway_workspaces = ipc_client.get_workspaces()?;
        let sway_names: std::collections::HashSet<String> = sway_workspaces.iter()
            .filter(|w| w.output == output.name)
            .map(|w| w.name.clone())
            .collect();

        // "Inactive" = workspaces on this output that belong to other groups
        // (formerly called "Hidden"). Excludes user-hidden and globals.
        for ws in &all_ws {
            if ws.is_global {
                continue;
            }
            if visible_names.iter().any(|v| v == &ws.name) {
                continue;
            }
            if hidden_names.iter().any(|h| h == &ws.name) {
                continue;
            }
            if sway_names.contains(&ws.name) {
                inactive.push(ws.name.clone());
            }
        }

        visible_names.sort();
        inactive.sort();
        hidden_names.sort();
        global_ws.sort();
        global_ws.dedup();

        println!("  Visible:  {}", if visible_names.is_empty() { "(none)".to_string() } else { visible_names.join(", ") });
        println!("  Inactive: {}", if inactive.is_empty() { "(none)".to_string() } else { inactive.join(", ") });
        println!("  Hidden:   {}", if hidden_names.is_empty() { "(none)".to_string() } else { hidden_names.join(", ") });
        if !global_ws.is_empty() {
            println!("  Global:   {}", global_ws.join(", "));
        }
    }

    Ok(())
}

fn run_config(action: ConfigAction) -> anyhow::Result<()> {
    let config = sway_groups_config::SwaygConfig::default();
    match action {
        ConfigAction::Dump { output } => {
            match output {
                Some(path) => {
                    config.dump_to(&path)?;
                    println!("Wrote default config to {}", path.display());
                }
                None => {
                    print!("{}", config.dump()?);
                }
            }
        }
    }
    Ok(())
}