From 20b2cbe3f876035dc338c2f2fe84dc7ea3cee828 Mon Sep 17 00:00:00 2001 From: Vladimir Bauer Date: Wed, 13 Nov 2024 07:33:12 +0500 Subject: [PATCH 1/2] print message when no client is focused address issue #15 --- src/kamp.rs | 14 +++++++++++++- src/kamp/cmd/edit.rs | 10 ++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/kamp.rs b/src/kamp.rs index 96cbcc8..aa061be 100644 --- a/src/kamp.rs +++ b/src/kamp.rs @@ -77,7 +77,19 @@ impl Dispatcher for SubCommand { fn dispatch(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); diff --git a/src/kamp/cmd/edit.rs b/src/kamp/cmd/edit.rs index 5d5281b..326594b 100644 --- a/src/kamp/cmd/edit.rs +++ b/src/kamp/cmd/edit.rs @@ -3,7 +3,7 @@ use std::path::PathBuf; use super::{Context, Error, Result}; -pub(crate) fn edit(ctx: Context, focus: bool, files: Vec) -> Result<()> { +pub(crate) fn edit(ctx: Context, focus: bool, files: Vec) -> Result { let mut buf = String::new(); let mut pair = [None; 2]; let mut coord = None; @@ -47,7 +47,8 @@ pub(crate) fn edit(ctx: Context, focus: bool, files: Vec) -> Result<()> } } - if buf.is_empty() { + let is_scratch = buf.is_empty(); + if is_scratch { buf.push_str("edit -scratch"); } @@ -55,11 +56,12 @@ pub(crate) fn edit(ctx: Context, focus: bool, files: Vec) -> Result<()> if focus { buf.push_str("\nfocus"); } - ctx.send(None, &buf).map(drop) + ctx.send(None, &buf).map(|_| is_scratch) } else if focus { Err(anyhow::Error::msg("no client in context").into()) } else { - ctx.connect(&buf) // this one acts like attach + // this one acts like attach + ctx.connect(&buf).map(|_| is_scratch) } } From e4af513ce7ad6ae0cb09e919367528099eac048d Mon Sep 17 00:00:00 2001 From: Vladimir Bauer Date: Wed, 13 Nov 2024 23:26:18 +0500 Subject: [PATCH 2/2] edit: return early if prereq is not met --- src/kamp/cmd/edit.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/kamp/cmd/edit.rs b/src/kamp/cmd/edit.rs index 326594b..039beba 100644 --- a/src/kamp/cmd/edit.rs +++ b/src/kamp/cmd/edit.rs @@ -4,6 +4,9 @@ use std::path::PathBuf; use super::{Context, Error, Result}; pub(crate) fn edit(ctx: Context, focus: bool, files: Vec) -> Result { + 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; @@ -57,8 +60,6 @@ pub(crate) fn edit(ctx: Context, focus: bool, files: Vec) -> Result