Skip to content

Commit

Permalink
✨ Refactor with lexopt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mostafa Qanbaryan committed Nov 26, 2024
1 parent 0c70802 commit 037e792
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 25 deletions.
9 changes: 8 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "zellij-switch"
version = "0.1.2"
version = "0.2.0"
authors = ["Mostafa Qanbaryan <[email protected]>"]
edition = "2018"

Expand All @@ -12,3 +12,4 @@ unicode-width = "0.1.10"
humantime = "2.1.0"
uuid = { version = "1.7.0", features = ["v4"] }
zellij-tile = "0.40.0"
lexopt = "0.3.0"
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ But works for me!

## Instruction

zellij pipe --plugin https://github.com/mostafaqanbaryan/zellij-switch/releases/download/v0.1.2/zellij-switch.wasm -- "$session_name::$cwd"
zellij pipe --plugin https://github.com/mostafaqanbaryan/zellij-switch/releases/download/v0.2.0/zellij-switch.wasm -- "--session zellij-session --cwd /home --layout default"

As of version 0.1.1, this works without specifying `$cwd`:
- `-s|--session` cannot have any space (like any other zellij session name).
- `-c|--cwd` must be an absolute path and is optional.
- `-l|--layout` is optional

zellij pipe --plugin https://github.com/mostafaqanbaryan/zellij-switch/releases/download/v0.1.2/zellij-switch.wasm -- "$session_name"

Starting at version 0.1.2, you may additionally specify a layout (without .kdl extension) in the third position:

zellij pipe --plugin https://github.com/mostafaqanbaryan/zellij-switch/releases/download/v0.1.2/zellij-switch.wasm -- "$session_name::$cwd::$layout"

3. (Optional) For better integration, [use this script](https://github.com/mostafaqanbaryan/dotfiles/blob/main/scripts/sessions)

## Build

Expand Down
54 changes: 40 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use lexopt::{prelude::*, Parser};
use std::{collections::BTreeMap, path::PathBuf};
use zellij_tile::prelude::*;

Expand All @@ -6,6 +7,40 @@ struct State {}

register_plugin!(State);

struct Args {
layout: LayoutInfo,
session: Option<String>,
cwd: Option<PathBuf>,
}

fn parse_args(mut parser: Parser) -> Result<Args, lexopt::Error> {
let mut temp = Args {
layout: LayoutInfo::File("default".to_string()),
cwd: None,
session: None,
};

while let Some(arg) = parser.next()? {
match arg {
Value(_) => {}
Short('c') | Long("cwd") => {
let cwd: String = parser.value()?.parse()?;
temp.cwd = Option::Some(PathBuf::from(cwd));
}
Short('s') | Long("session") => {
let session: String = parser.value()?.parse()?;
temp.session = Option::Some(session);
}
Short('l') | Long("layout") => {
let layout: String = parser.value()?.parse()?;
temp.layout = LayoutInfo::File(format!("{}.kdl", layout));
}
_ => {}
}
}
Ok(temp)
}

impl ZellijPlugin for State {
fn load(&mut self, _: BTreeMap<String, String>) {
request_permission(&[
Expand All @@ -15,21 +50,12 @@ impl ZellijPlugin for State {
}

fn pipe(&mut self, pipe_message: PipeMessage) -> bool {
let session_name = pipe_message.payload.unwrap().to_string();
let collection: Vec<&str> = session_name.split("::").collect::<Vec<&str>>().clone();
let session_name = collection[0];
let mut layout_name = "default".to_string();
let mut cwd = None;

if collection.len() >= 2 {
cwd = Some(PathBuf::from(collection[1]));
}
if collection.len() == 3 {
layout_name = format!("{}.kdl", collection[2]);
}
let payload = pipe_message.payload.unwrap().to_string();
let parser = lexopt::Parser::from_args(payload.split(" "));
let args = parse_args(parser).unwrap();
let session_name = args.session.unwrap();

let layout = LayoutInfo::File(layout_name);
switch_session_with_layout(Some(&session_name), layout, cwd);
switch_session_with_layout(Option::Some(session_name.as_str()), args.layout, args.cwd);
close_self();
true
}
Expand Down

0 comments on commit 037e792

Please sign in to comment.