UNPKG

2.57 kBJavaScriptView Raw
1/*
2 Terminal Kit
3
4 Copyright (c) 2009 - 2020 Cédric Ronvel
5
6 The MIT License (MIT)
7
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
14
15 The above copyright notice and this permission notice shall be included in all
16 copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25*/
26
27"use strict" ;
28
29
30
31const Promise = require( 'seventh' ) ;
32const string = require( 'string-kit' ) ;
33
34require( './patches.js' ) ;
35const execAsync = require( 'child_process' ).execAsync ;
36const execFileAsync = require( 'child_process' ).execFileAsync ;
37const spawn = require( 'child_process' ).spawn ;
38
39
40
41const XCLIP_SELECTION_ARG = {
42 c: 'clipboard' ,
43 p: 'primary' ,
44 s: 'secondary'
45} ;
46
47
48
49if ( process.platform === 'linux' ) {
50 exports.getClipboard = async ( source ) => {
51 var arg = XCLIP_SELECTION_ARG[ source[ 0 ] ] || 'clipboard' ;
52 return await execFileAsync( 'xclip' , [ '-o' , '-selection' , arg ] ) ;
53 } ;
54
55 exports.setClipboard = async ( str , source ) => {
56 var promise = new Promise() ;
57 var arg = XCLIP_SELECTION_ARG[ source[ 0 ] ] || 'clipboard' ;
58 var xclip = spawn( 'xclip' , [ '-i' , '-selection' , arg ] ) ;
59
60 xclip.on( 'error' , error => {
61 //console.error( 'xclip error:' , error ) ;
62 promise.reject( error ) ;
63 } ) ;
64
65 xclip.on( 'exit' , code => {
66 //console.log( 'xclip exited with code:' , code ) ;
67 if ( code !== 0 ) { promise.reject( code ) ; }
68 else { promise.resolve() ; }
69 } ) ;
70
71 // Send the string to push to the clipboard
72 xclip.stdin.end( str ) ;
73
74 return promise ;
75 } ;
76}
77else {
78 exports.getClipboard = () => Promise.reject( new Error( 'No clipboard manipulation program found' ) ) ;
79 exports.setClipboard = () => Promise.reject( new Error( 'No clipboard manipulation program found' ) ) ;
80}
81