@@ -5,6 +5,8 @@ import path from "path"
55import fs from "fs/promises"
66import * as Filesystem from "../../../../util/filesystem"
77import * as Process from "../../../../util/process"
8+ import { Config } from "../../../../config/index.js"
9+ import { AppRuntime } from "../../../../effect/app-runtime.js"
810
911// Lazy load which and clipboardy to avoid expensive execa/which/isexe chain at startup
1012const getWhich = lazy ( async ( ) => {
@@ -126,16 +128,35 @@ const getCopyMethod = lazy(async () => {
126128 if ( process . env [ "WAYLAND_DISPLAY" ] && which ( "wl-copy" ) ) {
127129 console . log ( "clipboard: using wl-copy" )
128130 return async ( text : string ) => {
131+ const config = await AppRuntime . runPromise ( Config . Service . use ( ( cfg ) => cfg . get ( ) ) ) . catch (
132+ ( ) => ( { } ) as Config . Info ,
133+ )
134+ const enablePrimary = config . clipboard ?. linux ?. enablePrimaryCopy ?? false
129135 const proc = Process . spawn ( [ "wl-copy" ] , { stdin : "pipe" , stdout : "ignore" , stderr : "ignore" } )
130136 if ( ! proc . stdin ) return
131137 proc . stdin . write ( text )
132138 proc . stdin . end ( )
133139 await proc . exited . catch ( ( ) => { } )
140+ if ( enablePrimary ) {
141+ const procPrimary = Process . spawn ( [ "wl-copy" , "--primary" ] , {
142+ stdin : "pipe" ,
143+ stdout : "ignore" ,
144+ stderr : "ignore" ,
145+ } )
146+ if ( ! procPrimary . stdin ) return
147+ procPrimary . stdin . write ( text )
148+ procPrimary . stdin . end ( )
149+ await procPrimary . exited . catch ( ( ) => { } )
150+ }
134151 }
135152 }
136153 if ( which ( "xclip" ) ) {
137154 console . log ( "clipboard: using xclip" )
138155 return async ( text : string ) => {
156+ const config = await AppRuntime . runPromise ( Config . Service . use ( ( cfg ) => cfg . get ( ) ) ) . catch (
157+ ( ) => ( { } ) as Config . Info ,
158+ )
159+ const enablePrimary = config . clipboard ?. linux ?. enablePrimaryCopy ?? false
139160 const proc = Process . spawn ( [ "xclip" , "-selection" , "clipboard" ] , {
140161 stdin : "pipe" ,
141162 stdout : "ignore" ,
@@ -145,11 +166,26 @@ const getCopyMethod = lazy(async () => {
145166 proc . stdin . write ( text )
146167 proc . stdin . end ( )
147168 await proc . exited . catch ( ( ) => { } )
169+ if ( enablePrimary ) {
170+ const procPrimary = Process . spawn ( [ "xclip" , "-selection" , "primary" ] , {
171+ stdin : "pipe" ,
172+ stdout : "ignore" ,
173+ stderr : "ignore" ,
174+ } )
175+ if ( ! procPrimary . stdin ) return
176+ procPrimary . stdin . write ( text )
177+ procPrimary . stdin . end ( )
178+ await procPrimary . exited . catch ( ( ) => { } )
179+ }
148180 }
149181 }
150182 if ( which ( "xsel" ) ) {
151183 console . log ( "clipboard: using xsel" )
152184 return async ( text : string ) => {
185+ const config = await AppRuntime . runPromise ( Config . Service . use ( ( cfg ) => cfg . get ( ) ) ) . catch (
186+ ( ) => ( { } ) as Config . Info ,
187+ )
188+ const enablePrimary = config . clipboard ?. linux ?. enablePrimaryCopy ?? false
153189 const proc = Process . spawn ( [ "xsel" , "--clipboard" , "--input" ] , {
154190 stdin : "pipe" ,
155191 stdout : "ignore" ,
@@ -159,6 +195,17 @@ const getCopyMethod = lazy(async () => {
159195 proc . stdin . write ( text )
160196 proc . stdin . end ( )
161197 await proc . exited . catch ( ( ) => { } )
198+ if ( enablePrimary ) {
199+ const procPrimary = Process . spawn ( [ "xsel" , "--primary" , "--input" ] , {
200+ stdin : "pipe" ,
201+ stdout : "ignore" ,
202+ stderr : "ignore" ,
203+ } )
204+ if ( ! procPrimary . stdin ) return
205+ procPrimary . stdin . write ( text )
206+ procPrimary . stdin . end ( )
207+ await procPrimary . exited . catch ( ( ) => { } )
208+ }
162209 }
163210 }
164211 }
0 commit comments