shellij 0.0.1

Simplify using Zellij over SSH.
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
use ansi_term::Color;
use anyhow::{Result, anyhow};
use clap::{Parser, Subcommand};
use indoc::{eprintdoc, formatdoc};

use crossterm::event::{Event, KeyCode, poll, read};
use crossterm::terminal::{disable_raw_mode, enable_raw_mode};

use std::fmt;
use std::fmt::Write as FmtWrite;
use std::io::Write;
use std::net::IpAddr;
use std::process::Command;
use std::process::Stdio;
use std::time::Duration;

#[derive(Debug, Parser)]
#[command(name = "shellij")]
#[command(about = "Helps you SSH directly into Zellij", long_about = None, version)]
pub struct Cli {
    #[arg(index = 1, required = true)]
    pub ssh_addr: String,

    #[command(subcommand)]
    pub command: Option<Commands>,
}

#[derive(Debug, Subcommand)]
pub enum Commands {
    /// Lists Zellij sessions over SSH
    List,

    /// Create and attach to a new Zellij session over SSH
    #[command(arg_required_else_help = true)]
    Create {
        /// Name of the new session
        session_name: String,
    },

    /// Delete a Zellij session over SSH
    #[command(arg_required_else_help = true)]
    Delete {
        /// Name of the session to delete
        #[arg(value_name = "SESSION NAME")]
        session_name: String,

        /// Force delete a session
        #[arg(
            long,
            short,
            action = clap::ArgAction::SetTrue,
            required = false,
            help ="Forces Zellij to delete a session. Required if the session is active."
        )]
        force: bool,
    },
}

impl Cli {
    pub fn validate(self) -> Result<Self> {
        if !self.args_are_valid() || !Self::env_is_valid() {
            return Err(anyhow!("Goodbye!"));
        }

        Ok(self)
    }

    fn args_are_valid(&self) -> bool {
        let error = Color::Red.paint("error: ");
        let usage_lbl = Color::White.bold().underline().paint("Usage");
        let exe = Color::White.bold().paint("ssh_zellij");
        let help = Color::White.bold().paint("--help");
        let usr = Color::Red.paint("<user>");
        let at = Color::Red.paint("@");
        let i = Color::Red.paint("<ip>");
        let addr = Color::White.bold().paint(self.ssh_addr.clone());

        let usage = formatdoc! {"
        {usage_lbl}: {exe} <SSH_ADDR>

        For more information, try '{help}'
        "};

        let Some((user, ip)) = self.ssh_addr.split_once("@") else {
            eprintdoc! {"
            {error} Malformed ssh address '{addr}':
              <user>{at}<ip>
                    ^ separator not found

            {usage}"};
            return false;
        };

        if user.is_empty() {
            eprintdoc! {"
            {error} Malformed ssh address '{addr}':
              {usr}@<ip>
               ^^^^ user not found

            {usage}"};

            return false;
        }

        if ip.is_empty() {
            eprintdoc! {"
            {error} Malformed ssh address '{addr}':
              <user>@{i}
                      ^^ ip not found

            {usage}"};

            return false;
        }

        if ip.parse::<IpAddr>().is_err() {
            eprintdoc! {"
            {error} Malformed ssh address '{addr}':
              <user>@{i}
                      ^^ invalid ip address

            {usage}"};
            return false;
        }

        true
    }

    fn env_is_valid() -> bool {
        let has_fzf = !Command::new("which")
            .arg("fzf")
            .output()
            .expect("Failed to check for fzf")
            .stdout
            .is_empty();

        if !has_fzf {
            let msg = "Failed to find fzf executable on PATH. Ensure fzf is executable to use this program";
            eprintln!("{msg}");
            return false;
        }

        let has_zellij = !Command::new("which")
            .arg("zellij")
            .output()
            .expect("Failed to check for zellij")
            .stdout
            .is_empty();

        if !has_zellij {
            eprintln!(
                "Failed to find zellij executable on PATH. Ensure zellij is executable to use this program"
            );
        }

        true
    }
}

/// Polls the keyboard. Return true if the next key pressed is <CR>.
fn poll_for_cr() -> Result<bool> {
    enable_raw_mode()?;
    loop {
        if poll(Duration::from_millis(100))? {
            let event = read()?;
            if event == Event::Key(KeyCode::Enter.into()) {
                disable_raw_mode()?;
                return Ok(true);
            } else {
                disable_raw_mode()?;
                return Ok(false);
            }
        }
    }
}

/// Puts remote zellij sessions into local fzf picker
fn fzf(zs: Vec<Zesh>, zs_raw: String) -> Result<Zesh> {
    let mut fzf = Command::new("fzf")
        .args([
            "--ansi",
            "--with-nth",
            "2..",
            "--header",
            "Which session do you want to attach to?",
        ])
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .spawn()?;

    let stdin = fzf.stdin.as_mut().expect("failed to capture stdin");
    stdin.write_all(zs_raw.as_bytes())?;

    let output = fzf.wait_with_output()?;
    if !output.status.success() {
        return Err(anyhow!("Fzf exited with failure code"));
    }

    let raw_choice = String::from_utf8(output.stdout).expect("fzf produced invalid utf-8");
    let raw_choice = raw_choice.trim();

    let Some((idx, _rest)) = raw_choice.split_once(' ') else {
        let errmsg = "Failed to parse choice output from fzf.";
        eprintln!("{errmsg}");
        return Err(anyhow!(errmsg));
    };

    let choice_idx = idx.parse::<usize>()?;
    Ok(zs[choice_idx].clone())
}

pub fn shellij_create(ssh_addr: &str, session_name: &str) -> Result<()> {
    Command::new("ssh")
        .arg("-t")
        .arg(ssh_addr)
        .arg(format!("zellij -s {session_name}"))
        .status()?;

    Ok(())
}

pub fn shellij_delete(ssh_addr: &str, session_name: &str, force: bool) -> Result<()> {
    let zellij_cmd = if force {
        format!("zellij delete-session {session_name}")
    } else {
        format!("zellij delete-session {session_name} --force")
    };

    Command::new("ssh")
        .arg(ssh_addr)
        .arg(&zellij_cmd)
        .status()?;

    Ok(())
}

pub fn shellij_list(ssh_addr: &str) -> Result<()> {
    let (_, stdout) = zellij_list_sessions(ssh_addr)?;

    if stdout.is_empty() {
        println!("No Zellij sessions found.");
        return Ok(());
    }

    println!("\nSessions Found:");
    println!("{stdout}");

    Ok(())
}

/// Attaches to a remote zellij session over ssh
pub fn shellij(ssh_addr: &str) -> Result<()> {
    let (zs, stdout) = zellij_list_sessions(ssh_addr)?;

    let zs_raw = stdout
        .lines()
        .enumerate()
        .fold(String::new(), |mut output, (i, l)| {
            let _ = writeln!(output, "{i} {}", l);
            output
        });

    if zs.is_empty() {
        println!("No active sessions found");
        println!("Create one? (Enter for yes, any other key for no)");

        if poll_for_cr()? {
            Command::new("ssh")
                .arg("-t")
                .arg(ssh_addr)
                .arg("zellij attach --create")
                .status()?;
            return Ok(());
        } else {
            println!("No sessions created. Goodbye!");
            return Ok(());
        }
    }

    if zs.len() == 1 {
        println!("Only one session found. Connecting...");
        Command::new("ssh")
            .arg("-t")
            .arg(ssh_addr)
            .arg("zellij")
            .arg("attach")
            .status()?;
        return Ok(());
    }

    let session_name = fzf(zs, zs_raw)?.name;
    println!("Attaching to {session_name}...");
    Command::new("ssh")
        .arg("-t")
        .arg(ssh_addr)
        .arg(format!("zellij attach {session_name}"))
        .status()?;

    Ok(())
}

/// Gets the list of zellij sessions from remote over ssh
fn zellij_list_sessions(ssh_addr: &str) -> Result<(Vec<Zesh>, String)> {
    let zellij_out = Command::new("ssh")
        .arg(ssh_addr)
        .arg("zellij ls -n")
        .output()?;

    let stdout = String::from_utf8(zellij_out.stdout)?;
    let stderr = String::from_utf8(zellij_out.stderr)?;

    if stderr.contains("No active zellij sessions found.") {
        return Ok((vec![], stdout));
    }

    if stdout.is_empty() {
        let err = "Command `zellij ls` failed on remote. Aborting.";
        eprintln!("{err}");
        return Err(anyhow!(err));
    }

    let zs = stdout.trim().split('\n').flat_map(Zesh::try_from).collect();

    // ssh.close().await?;
    Ok((zs, stdout.trim().to_string()))
}

#[derive(Debug, Clone)]
struct Zesh {
    pub name: String,
    pub created_at: String,
    pub exited: bool,
}

impl fmt::Display for Zesh {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let Zesh {
            name,
            created_at,
            exited,
        } = self;

        let s = formatdoc! {"
        {{
            name: {name}
            created_at: {created_at}
            exited: {exited}
        }}
        "};

        write!(f, "{s}")
    }
}

impl TryFrom<&str> for Zesh {
    type Error = anyhow::Error;

    fn try_from(s: &str) -> std::result::Result<Self, Self::Error> {
        let Some((name, rest)) = s.split_once(" ") else {
            println!("Failed to parse session list from remote zellij!");
            return Err(anyhow!("Failed to parse session list from remote zellij!"));
        };

        if name.is_empty() {
            let errmsg = "Failed to parse session name from remote zellij session list!";
            eprintln!("{errmsg}");
            return Err(anyhow!(errmsg));
        }

        if rest.is_empty() {
            let errmsg = "Unexpected end of input while parsing zellij session list!";
            eprintln!("{errmsg}");
            return Err(anyhow!(errmsg));
        }

        let Some((created_at, rest)) = rest[1..].split_once("]") else {
            eprintln!("Failed to parse session creation timestamp!");
            return Err(anyhow!("Failed to parse session creation timestamp!"));
        };

        let created_at = created_at.trim().to_string();
        let name = name.to_string();
        let rest = rest.trim();

        if rest.is_empty() {
            return Ok(Zesh {
                name,
                created_at,
                exited: false,
            });
        }

        if rest.contains("EXITED") {
            return Ok(Zesh {
                name,
                created_at,
                exited: true,
            });
        }

        println!("FAILED TO RETURN ZESH");

        Err(anyhow!("Failed to parse session exit status!"))
    }
}

#[cfg(test)]
mod test {

    use super::*;

    #[test]
    fn parse_zesh() {
        let s =
            "friendly-tiger [Created 1month 9days 13h 33m 5s ago] (EXITED - attach to resurrect)";

        let zesh = Zesh::try_from(s);
        assert!(zesh.is_ok());

        let s = "considerate-hill [Created 4s ago] ";
        let zesh = Zesh::try_from(s);
        assert!(zesh.is_ok());
    }
}