Allow opening paths from the CLI
Co-Authored-By: Antonio Scandurra <me@as-cii.com>
This commit is contained in:
parent
05c44b9414
commit
43763fa2f8
4 changed files with 47 additions and 34 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1000,6 +1000,7 @@ dependencies = [
|
||||||
"clap 3.1.8",
|
"clap 3.1.8",
|
||||||
"core-foundation",
|
"core-foundation",
|
||||||
"core-services",
|
"core-services",
|
||||||
|
"dirs 3.0.1",
|
||||||
"ipc-channel",
|
"ipc-channel",
|
||||||
"serde",
|
"serde",
|
||||||
]
|
]
|
||||||
|
|
|
@ -16,5 +16,6 @@ anyhow = "1.0"
|
||||||
core-foundation = "0.9"
|
core-foundation = "0.9"
|
||||||
core-services = "0.2"
|
core-services = "0.2"
|
||||||
clap = { version = "3.1", features = ["derive"] }
|
clap = { version = "3.1", features = ["derive"] }
|
||||||
|
dirs = "3.0"
|
||||||
ipc-channel = "0.16"
|
ipc-channel = "0.16"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
|
@ -48,8 +48,10 @@ fn main() -> Result<()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn locate_app() -> Result<PathBuf> {
|
fn locate_app() -> Result<PathBuf> {
|
||||||
Ok("/Users/nathan/src/zed/target/debug/bundle/osx/Zed.app".into())
|
Ok(std::env::current_exe()?
|
||||||
// Ok("/Applications/Zed.app".into())
|
.parent()
|
||||||
|
.unwrap()
|
||||||
|
.join("bundle/osx/Zed.app"))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn launch_app(app_path: PathBuf) -> Result<(IpcSender<CliRequest>, IpcReceiver<CliResponse>)> {
|
fn launch_app(app_path: PathBuf) -> Result<(IpcSender<CliRequest>, IpcReceiver<CliResponse>)> {
|
||||||
|
|
|
@ -13,7 +13,7 @@ use futures::{
|
||||||
channel::{mpsc, oneshot},
|
channel::{mpsc, oneshot},
|
||||||
SinkExt, StreamExt,
|
SinkExt, StreamExt,
|
||||||
};
|
};
|
||||||
use gpui::{App, AssetSource, Task};
|
use gpui::{App, AssetSource, AsyncAppContext, Task};
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use project::Fs;
|
use project::Fs;
|
||||||
|
@ -94,32 +94,14 @@ fn main() {
|
||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
app.on_open_urls(|urls, cx| {
|
let (cli_connections_tx, mut cli_connections_rx) = mpsc::unbounded();
|
||||||
|
app.on_open_urls(move |urls, _| {
|
||||||
if let Some(server_name) = urls.first().and_then(|url| url.strip_prefix("zed-cli://")) {
|
if let Some(server_name) = urls.first().and_then(|url| url.strip_prefix("zed-cli://")) {
|
||||||
if let Some((mut requests, responses)) = connect_to_cli(server_name).log_err() {
|
if let Some(cli_connection) = connect_to_cli(server_name).log_err() {
|
||||||
cx.spawn(|_| async move {
|
cli_connections_tx
|
||||||
for request in requests.next().await {
|
.unbounded_send(cli_connection)
|
||||||
match request {
|
.map_err(|_| anyhow!("no listener for cli connections"))
|
||||||
CliRequest::Open { paths, wait } => {
|
.log_err();
|
||||||
if wait {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
|
|
||||||
log::info!("open paths {:?}", paths);
|
|
||||||
responses
|
|
||||||
.send(CliResponse::Stdout {
|
|
||||||
message: "Hello CLI!".to_string(),
|
|
||||||
})
|
|
||||||
.log_err();
|
|
||||||
responses.send(CliResponse::Exit { status: 0 }).log_err();
|
|
||||||
|
|
||||||
// TODO... get rid of AppState so we can do this here?
|
|
||||||
// cx.update(|cx| open_paths(&paths, app_state, cx));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.detach();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -207,13 +189,25 @@ fn main() {
|
||||||
|
|
||||||
if stdout_is_a_pty() {
|
if stdout_is_a_pty() {
|
||||||
cx.platform().activate(true);
|
cx.platform().activate(true);
|
||||||
}
|
let paths = collect_path_args();
|
||||||
|
if paths.is_empty() {
|
||||||
let paths = collect_path_args();
|
cx.dispatch_global_action(OpenNew(app_state.clone()));
|
||||||
if paths.is_empty() {
|
} else {
|
||||||
cx.dispatch_global_action(OpenNew(app_state.clone()));
|
cx.dispatch_global_action(OpenPaths { paths, app_state });
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
cx.dispatch_global_action(OpenPaths { paths, app_state });
|
if let Ok(Some(connection)) = cli_connections_rx.try_next() {
|
||||||
|
cx.spawn(|cx| handle_cli_connection(connection, app_state.clone(), cx))
|
||||||
|
.detach();
|
||||||
|
} else {
|
||||||
|
cx.dispatch_global_action(OpenNew(app_state.clone()));
|
||||||
|
}
|
||||||
|
cx.spawn(|cx| async move {
|
||||||
|
while let Some(connection) = cli_connections_rx.next().await {
|
||||||
|
handle_cli_connection(connection, app_state.clone(), cx.clone()).await;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -358,3 +352,18 @@ fn connect_to_cli(
|
||||||
|
|
||||||
Ok((async_request_rx, response_tx))
|
Ok((async_request_rx, response_tx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn handle_cli_connection(
|
||||||
|
(mut requests, responses): (mpsc::Receiver<CliRequest>, IpcSender<CliResponse>),
|
||||||
|
app_state: Arc<AppState>,
|
||||||
|
mut cx: AsyncAppContext,
|
||||||
|
) {
|
||||||
|
if let Some(request) = requests.next().await {
|
||||||
|
match request {
|
||||||
|
CliRequest::Open { paths, .. } => {
|
||||||
|
cx.update(|cx| cx.dispatch_global_action(OpenPaths { paths, app_state }));
|
||||||
|
responses.send(CliResponse::Exit { status: 0 }).log_err();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue