@@ -121,7 +121,6 @@ func handleRPTY(inv *serpent.Invocation, client *codersdk.Client, args handleRPT
121
121
if err := cliui .Agent (ctx , inv .Stderr , agt .ID , cliui.AgentOptions {
122
122
FetchInterval : 0 ,
123
123
Fetch : client .WorkspaceAgent ,
124
- FetchLogs : client .WorkspaceAgentLogsAfter ,
125
124
Wait : false ,
126
125
}); err != nil {
127
126
return err
@@ -165,24 +164,19 @@ func handleRPTY(inv *serpent.Invocation, client *codersdk.Client, args handleRPT
165
164
defer conn .Close ()
166
165
167
166
cliui .Infof (inv .Stderr , "Connected to %s (agent id: %s)" , args .NamedWorkspace , agt .ID )
167
+ cliui .Infof (inv .Stderr , "Reconnect ID: %s" , reconnectID )
168
168
closeUsage := client .UpdateWorkspaceUsageWithBodyContext (ctx , ws .ID , codersdk.PostWorkspaceUsageRequest {
169
169
AgentID : agt .ID ,
170
170
AppName : codersdk .UsageAppNameReconnectingPty ,
171
171
})
172
172
defer closeUsage ()
173
173
174
- stdinDone := make ( chan struct {} )
175
- stdoutDone := make ( chan struct {})
176
- stderrDone := make ( chan struct {} )
177
- done := make ( chan struct {} )
174
+ br := bufio . NewScanner ( inv . Stdin )
175
+ // Split on bytes, otherwise you have to send a newline to flush the buffer.
176
+ br . Split ( bufio . ScanBytes )
177
+ je := json . NewEncoder ( conn )
178
178
179
179
go func () {
180
- defer close (stdinDone )
181
- // This is how we send commands to the agent.
182
- br := bufio .NewScanner (inv .Stdin )
183
- // Split on bytes, otherwise you have to send a newline to flush the buffer.
184
- br .Split (bufio .ScanBytes )
185
- je := json .NewEncoder (conn )
186
180
for br .Scan () {
187
181
if err := je .Encode (map [string ]string {
188
182
"data" : br .Text (),
@@ -191,27 +185,32 @@ func handleRPTY(inv *serpent.Invocation, client *codersdk.Client, args handleRPT
191
185
}
192
186
}
193
187
}()
188
+
189
+ windowChange := listenWindowSize (ctx )
194
190
go func () {
195
- defer func () {
196
- close ( stdoutDone )
197
- }()
198
- _ , _ = io . Copy ( inv . Stdout , conn )
199
- }()
200
- go func () {
201
- defer func () {
202
- close ( stderrDone )
203
- }()
204
- _ , _ = io . Copy ( inv . Stderr , conn )
205
- }()
206
- go func () {
207
- defer close ( done )
208
- <- stdoutDone
209
- <- stderrDone
210
- _ = conn . Close ()
211
- _ , _ = fmt . Fprintf ( inv . Stderr , "Connection closed \n " )
191
+ for {
192
+ select {
193
+ case <- ctx . Done ():
194
+ return
195
+ case <- windowChange :
196
+ }
197
+ width , height , err := term . GetSize ( int ( stdoutFile . Fd ()))
198
+ if err != nil {
199
+ continue
200
+ }
201
+ if err := je . Encode ( map [ string ] int {
202
+ "width" : width ,
203
+ "height" : height ,
204
+ }); err != nil {
205
+ cliui . Errorf ( inv . Stderr , "Failed to send window size: %v" , err )
206
+ }
207
+ }
212
208
}()
213
209
214
- <- done
210
+ _ , _ = io .Copy (inv .Stdout , conn )
211
+ cancel ()
212
+ _ = conn .Close ()
213
+ _ , _ = fmt .Fprintf (inv .Stderr , "Connection closed\n " )
215
214
216
215
return nil
217
216
}
0 commit comments