Debian Codegraph @

bin/circle-fe-term

raw
1 #!/usr/bin/perl
2
3 # You may distribute under the terms of the GNU General Public License
4 #
5 # (C) Paul Evans, 2010-2023 -- [email protected]
6
7 use v5.26;
8 use warnings;
9
10 use Tickit::Async;
11 use Tickit::Widget::Tabbed 0.028;
12 use Tickit::Widget::VBox;
13 use Tickit::Window 0.57; # ->bind_event
14
15 use Tickit::App::Plugin::EscapePrefix;
16
17 use Net::Async::Tangence::Client 0.15; # ->connect_url 'family' argument
18
19 use Circle::FE::Term::Tab;
20 use Circle::FE::Term::Ribbon;
21
22 use IO::Async::Loop;
23
24 use Text::Balanced qw( extract_bracketed );
25 use Getopt::Long;
26
27 my $loop = IO::Async::Loop->new();
28
29 use constant HAVE_METRICS => eval { require Metrics::Any::Adapter; };
30
31 my $IDENTITY;
32 my $FAMILY;
33
34 GetOptions(
35 'identity|i=s' => \$IDENTITY,
36 'metrics-port=i' => \my $METRICS_PORT,
37 'help' => sub { usage(0) },
38 '4|ipv4' => sub { $FAMILY = "inet4" },
39 '6|ipv6' => sub { $FAMILY = "inet6" },
40 ) or usage(1);
41
42 sub usage
43 {
44 my ( $exitcode ) = @_;
45
46 print { $exitcode ? \*STDERR : \*STDOUT } <<'EOF';
47 circle-fe-term [options...] [URL]
48
49 Options:
50
51 --identity, -i IDENTITY Use the given session identity for reconnection
52
53 URL should be one of:
54
55 sshunix://host/path/to/socket
56 sshexec://host/path/to/command?with+arguments
57 exec:///path/to/command?with+arguments
58 tcp://host:port/
59 unix:///path/to/socket
60
61 EOF
62
63 exit $exitcode;
64 }
65
66 if( HAVE_METRICS and $METRICS_PORT ) {
67 Metrics::Any::Adapter->import( 'Prometheus' );
68
69 require Net::Prometheus;
70 Net::Prometheus->new->export_to_IO_Async( $loop, port => $METRICS_PORT );
71 };
72
73 my $URL = shift @ARGV or usage(1);
74
75 if( !defined $IDENTITY ) {
76 my $hostname = `hostname -f`; chomp $hostname;
77 $IDENTITY = $ENV{USER} . "@" . $hostname . "/Term";
78 }
79
80 my $client = Net::Async::Tangence::Client->new(
81 identity => $IDENTITY,
82
83 on_closed => sub {
84 warn "Connection closed\n";
85 exit(0);
86 },
87
88 on_error => sub { warn "Received MSG_ERROR: $_[0]\n"; },
89 );
90
91 $loop->add( $client );
92
93 my $rootobj = $client->connect_url( $URL, family => $FAMILY )->get;
94
95 my $t = Tickit::Async->new;
96
97 $loop->add( $t );
98
99 Tickit::App::Plugin::EscapePrefix->apply( $t );
100
101 my $top_vbox = Tickit::Widget::VBox->new;
102
103 # TODO: Consider a menubar
104
105 my $tabbed = Tickit::Widget::Tabbed->new(
106 tab_position => "bottom",
107 style => {
108 'b:active' => 1,
109 'u:active' => 1,
110 },
111 tab_class => "Circle::FE::Term::Tab",
112 ribbon_class => "Circle::FE::Term::Ribbon",
113 );
114
115 $t->bind_key( "C-n" => sub { $tabbed->next_tab } );
116 $t->bind_key( "C-p" => sub { $tabbed->prev_tab } );
117
118 $top_vbox->add( $tabbed, expand => 1 );
119
120 $t->set_root_widget( $top_vbox );
121
122 my $f = $rootobj->call_method(
123 "get_session" => [ 'tabs' ],
124 )->then( sub {
125 my ( $session ) = @_;
126
127 $session->watch_property_with_initial( "tabs",
128 on_set => sub {
129 my ( $objarray ) = @_;
130
131 foreach my $obj ( @$objarray ) {
132 $tabbed->add_tab( Tickit::Widget::VBox->new, object => $obj );
133 }
134 },
135 on_push => sub {
136 my @new = @_;
137 foreach my $obj ( @new ) {
138 $tabbed->add_tab( Tickit::Widget::VBox->new, object => $obj );
139 }
140 },
141 on_shift => sub {
142 my ( $count ) = @_;
143 $tabbed->remove_tab( 0 ) for 1 .. $count;
144 },
145 on_splice => sub {
146 my ( $index, $count, @objs ) = @_;
147
148 # $count times, remove the one at $index, as they'll shuffle down
149 $tabbed->remove_tab( $index ) for 1 .. $count;
150
151 # TODO: I have no idea wtf is going on here
152 foreach my $i ( 0 .. $#objs ) {
153 my $obj = $objs[$i];
154 die "TODO: insert tab\n";
155 }
156 },
157 on_move => sub {
158 my ( $index, $delta ) = @_;
159
160 $tabbed->move_tab( $index, $delta );
161 },
162 );
163 });
164 $t->adopt_future( $f );
165
166 $t->run;