Skip to content

Commit

Permalink
Merge pull request #16 from vbauerster/req-15
Browse files Browse the repository at this point in the history
Feature request: 15
  • Loading branch information
vbauerster authored Nov 13, 2024
2 parents dbea64d + e4af513 commit 1f2b5db
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/kamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,19 @@ impl Dispatcher for SubCommand {
fn dispatch<W: Write>(self, ctx: Context, mut writer: W) -> Result<()> {
match self {
SubCommand::Attach(opt) => cmd::attach(ctx, opt.buffer),
SubCommand::Edit(opt) => cmd::edit(ctx, opt.focus, opt.files),
SubCommand::Edit(opt) => {
let session = ctx.session();
let client = ctx.client();
let scratch = cmd::edit(ctx, opt.focus, opt.files)?;
if let (Some(client), false) = (client, opt.focus) {
writeln!(
writer,
"{} is opened in client: {client}, session: {session}",
if scratch { "scratch" } else { "file" }
)?;
}
Ok(())
}
SubCommand::Send(opt) => {
if opt.command.is_empty() {
return Err(Error::CommandRequired);
Expand Down
15 changes: 9 additions & 6 deletions src/kamp/cmd/edit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use std::path::PathBuf;

use super::{Context, Error, Result};

pub(crate) fn edit(ctx: Context, focus: bool, files: Vec<String>) -> Result<()> {
pub(crate) fn edit(ctx: Context, focus: bool, files: Vec<String>) -> Result<bool> {
if focus && ctx.is_draft() {
return Err(anyhow::Error::msg("no client in context").into());
}
let mut buf = String::new();
let mut pair = [None; 2];
let mut coord = None;
Expand Down Expand Up @@ -47,19 +50,19 @@ pub(crate) fn edit(ctx: Context, focus: bool, files: Vec<String>) -> Result<()>
}
}

if buf.is_empty() {
let is_scratch = buf.is_empty();
if is_scratch {
buf.push_str("edit -scratch");
}

if !ctx.is_draft() {
if focus {
buf.push_str("\nfocus");
}
ctx.send(None, &buf).map(drop)
} else if focus {
Err(anyhow::Error::msg("no client in context").into())
ctx.send(None, &buf).map(|_| is_scratch)
} else {
ctx.connect(&buf) // this one acts like attach
// this one acts like attach
ctx.connect(&buf).map(|_| is_scratch)
}
}

Expand Down

0 comments on commit 1f2b5db

Please sign in to comment.