11use std:: time:: Duration ;
22
3- use fig_desktop_api:: requests:: Error ;
43use fig_proto:: fig:: server_originated_message:: Submessage as ServerOriginatedSubMessage ;
54use fig_proto:: fig:: {
6- EnvironmentVariable ,
7- PseudoterminalExecuteRequest ,
8- PseudoterminalExecuteResponse ,
95 RunProcessRequest ,
106 RunProcessResponse ,
117} ;
@@ -21,29 +17,9 @@ use fig_util::env_var::{
2117} ;
2218use tokio:: process:: Command ;
2319use tokio:: time:: timeout;
24- use tracing:: {
25- debug,
26- warn,
27- } ;
20+ use tracing:: debug;
2821
29- use super :: {
30- RequestResult ,
31- RequestResultImpl ,
32- } ;
33- use crate :: platform:: PlatformState ;
34-
35- fn shell_args ( shell_path : & str ) -> & ' static [ & ' static str ] {
36- let ( _, shell_name) = shell_path. rsplit_once ( [ '/' , '\\' ] ) . unwrap_or ( ( "" , shell_path) ) ;
37- match shell_name {
38- "bash" | "bash.exe" => & [ "--norc" , "--noprofile" , "-c" ] ,
39- "zsh" | "zsh.exe" => & [ "--norcs" , "-c" ] ,
40- "fish" | "fish.exe" => & [ "--no-config" , "-c" ] ,
41- _ => {
42- warn ! ( %shell_name, "unknown shell" ) ;
43- & [ ]
44- } ,
45- }
46- }
22+ use super :: RequestResult ;
4723
4824fn set_fig_vars ( cmd : & mut Command ) {
4925 cmd. env ( Q_TERM , env ! ( "CARGO_PKG_VERSION" ) ) ;
@@ -54,98 +30,6 @@ fn set_fig_vars(cmd: &mut Command) {
5430 cmd. env ( "TERM" , "xterm-256color" ) ;
5531}
5632
57- pub async fn execute ( request : PseudoterminalExecuteRequest , figterm_state : & FigtermState ) -> RequestResult {
58- debug ! ( {
59- term_session =? request. terminal_session_id,
60- command = request. command,
61- cwd = request. working_directory( ) ,
62- env =? request. env,
63- background = request. background_job,
64- pipelined = request. is_pipelined
65- } , "Executing command" ) ;
66-
67- let session_sender = figterm_state
68- . with_maybe_id ( & request. terminal_session_id . map ( FigtermSessionId :: new) , |session| {
69- session. sender . clone ( )
70- } ) ;
71-
72- if let Some ( session_sender) = session_sender {
73- let ( message, rx) = FigtermCommand :: pseudoterminal_execute (
74- request. command ,
75- request. working_directory ,
76- request. background_job ,
77- request. is_pipelined ,
78- request. env ,
79- ) ;
80- session_sender
81- . send ( message)
82- . map_err ( |err| format ! ( "failed sending command to figterm: {err}" ) ) ?;
83- drop ( session_sender) ;
84-
85- let response = timeout ( Duration :: from_secs ( 10 ) , rx)
86- . await
87- . map_err ( |err| Error :: from_std ( err) . wrap_err ( "Qterm response timed out after 10 sec" ) ) ?
88- . map_err ( |err| Error :: from_std ( err) . wrap_err ( "Qterm response failed to receive from sender" ) ) ?;
89-
90- if let hostbound:: response:: Response :: PseudoterminalExecute ( response) = response {
91- RequestResult :: Ok ( Box :: new ( ServerOriginatedSubMessage :: PseudoterminalExecuteResponse (
92- PseudoterminalExecuteResponse {
93- stdout : response. stdout ,
94- stderr : response. stderr ,
95- exit_code : response. exit_code ,
96- } ,
97- ) ) )
98- } else {
99- Err ( "invalid response type" . to_string ( ) . into ( ) )
100- }
101- } else {
102- debug ! ( "executing locally" ) ;
103-
104- let shell = PlatformState :: shell ( ) ;
105-
106- // note: we don't know what shell they use because we don't have any figterm sessions to check
107- let args = shell_args ( & shell) ;
108-
109- let mut cmd = Command :: new ( & * shell) ;
110- #[ cfg( target_os = "windows" ) ]
111- cmd. creation_flags ( windows:: Win32 :: System :: Threading :: DETACHED_PROCESS . 0 ) ;
112- // TODO: better SHELL_ARGs handling here based on shell.
113- // TODO: handle wsl distro from FigtermState here.
114- cmd. args ( args) ;
115- cmd. arg ( & request. command ) ;
116-
117- if let Some ( working_directory) = request. working_directory {
118- cmd. current_dir ( working_directory) ;
119- }
120-
121- set_fig_vars ( & mut cmd) ;
122-
123- for EnvironmentVariable { key, value } in & request. env {
124- match value {
125- Some ( value) => cmd. env ( key, value) ,
126- None => cmd. env_remove ( key) ,
127- } ;
128- }
129-
130- let output = cmd
131- . output ( )
132- . await
133- . map_err ( |err| format ! ( "Failed running command {:?}: {err}" , request. command) ) ?;
134-
135- RequestResult :: Ok ( Box :: new ( ServerOriginatedSubMessage :: PseudoterminalExecuteResponse (
136- PseudoterminalExecuteResponse {
137- stdout : String :: from_utf8_lossy ( & output. stdout ) . to_string ( ) ,
138- stderr : if output. stderr . is_empty ( ) {
139- None
140- } else {
141- Some ( String :: from_utf8_lossy ( & output. stderr ) . to_string ( ) )
142- } ,
143- exit_code : output. status . code ( ) ,
144- } ,
145- ) ) )
146- }
147- }
148-
14933pub async fn run ( request : RunProcessRequest , state : & FigtermState ) -> RequestResult {
15034 debug ! ( {
15135 term_session =? request. terminal_session_id,
@@ -230,7 +114,3 @@ pub async fn run(request: RunProcessRequest, state: &FigtermState) -> RequestRes
230114 ) ) )
231115 }
232116}
233-
234- pub async fn write ( ) -> RequestResult {
235- RequestResult :: error ( "PseudoterminalWriteRequest is deprecated" . to_string ( ) )
236- }
0 commit comments