summaryrefslogtreecommitdiff
path: root/scripts/desktop/sway-ftp-master
blob: 3e6fa5e46ec79ab9ab41fd27bcc4cce50b5a9860 (plain)
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
#!/usr/bin/perl

# sway-ftp-master -- set up some windows for doing work on Debian's ftp-master
#
# Copyright (C) 2019, 2021 Sean Whitton
#
# GPL-3 or later

use 5.032;
use strict;
use warnings;
use lib "$ENV{HOME}/src/dotfiles/perl5";

use JSON;
use Local::Desktop;

sub walk_tree (&$) {
    my ($pred, $tree) = @_;
    $pred->($tree) and return $tree;
    map &walk_tree($pred, $_), $tree->{nodes}->@*, $tree->{floating_nodes}->@*;
}

sub title_exists {
    my $name = shift;
    walk_tree { $_->{name} and $_->{name} =~ /^\Q$name\E/ }
    decode_json `swaymsg -t get_tree`;
}

sub marked_exists {
    my $mark = shift;
    walk_tree { grep $_ eq $mark, $_->{marks}->@* }
    decode_json `swaymsg -t get_tree`;
}

sub focus_title { system "swaymsg", qq|[title="$_[0]"]|, "focus" }

sub bring_title { system "swaymsg", qq|[title="$_[0]"]|,
		    "move to workspace current,", "focus" }

sub focus_marked { system "swaymsg", qq|[con_mark="$_[0]"]|, "focus" }

sub bring_marked { system "swaymsg", qq|[con_mark="$_[0]"]|,
		     "move to workspace current,", "focus" }

if (marked_exists "dak") {
    focus_marked "dak";
} else {
    # first establish a control socket, so that the two interactive ssh
    # connections don't race to do this and one of them disables multiplexing
    system "ssh", "-fN", "ftp-master.debian.org";

    # start Emacs on ftp-master
    system "ssh", "ftp-master.debian.org", "pgrep", "-u", "spwhitton",
      "emacs", "||", "ssh", "ftp-master.debian.org", "emacs", "--daemon", "&";

    my $first = fresh_workspace(go => 1) // die "no fresh workspace";

    fork // die "couldn't fork"
      or exec "foot", "--override=locked-title=yes",
      "--title=ftp-master dak command session",
      "ssh-and-tmux", "ftp-master.debian.org", "dak";
    sleep 0.3 until marked_exists "dak";
    bring_marked "dak";

    fork // die "couldn't fork"
      or exec "foot", "--override=locked-title=yes",
      "--title=ftp-master GNU mc session",
      "ssh-and-tmux", "ftp-master.debian.org", "mc";
    sleep 0.3 until marked_exists "mc";
    bring_marked "mc";

    system "emacsclient", "-c", "-a", "", "-n", "-e",
      '(progn (switch-to-buffer "*NEW notes*") (text-mode))';
    sleep 0.3 until title_exists "*NEW notes*";
    bring_title "\\*NEW notes\\*";

    # currently we implement only the layout the X11 script chose for melete

    system "swaymsg",
      "move down,",
      "focus up,",
      "focus left,",
      "move right,",
      "move right,",
      "move right,",
      "split v,",
      "layout tabbed,",
      "resize set width 38,",
      "focus left,",
      "split v,",
      "layout tabbed,",
      "resize set height 65,",
      "focus right";
}