1
+ // @ts -ignore
2
+ const Me = imports . misc . extensionUtils . getCurrentExtension ( )
3
+
4
+ const { Gio, GLib } = imports . gi
5
+
6
+ import * as plugins from 'launcher_plugins'
7
+ import * as utils from 'utils'
8
+
9
+ import type { Response , Selection } from 'launcher_plugins'
10
+ import type { Ext } from './extension'
11
+
12
+ function add ( id : number , file : string , content_type : string ) : Selection {
13
+ const pos = file . lastIndexOf ( "/" )
14
+ return {
15
+ id,
16
+ name : pos === 0 ? file : file . substr ( pos + 1 ) ,
17
+ description : "~/" + file ,
18
+ content_type
19
+ }
20
+ }
21
+
22
+ export class ShellBuiltin extends plugins . Builtin {
23
+ selections : Array < Selection > = [ ]
24
+
25
+ init ( ) { }
26
+
27
+ query ( _ :Ext , query : string ) : Response . Response {
28
+ let id = 0
29
+ this . selections . splice ( 0 )
30
+ const search = query . substr ( query . indexOf ( " " ) + 1 ) . trim ( )
31
+ if ( search . length > 2 ) {
32
+ const cmd = utils . async_process_ipc ( [ "fdfind" , search ] )
33
+ if ( cmd ) {
34
+ while ( true ) {
35
+ try {
36
+ const [ bytes , read ] = cmd . stdout . read_line ( null )
37
+ if ( bytes === null || read === 0 ) break
38
+ const file = imports . byteArray . toString ( bytes )
39
+ const gfile = Gio . File . new_for_path ( file )
40
+ if ( gfile . query_exists ( null ) ) {
41
+ let content_type
42
+ if ( GLib . file_test ( file , GLib . FileTest . IS_DIR ) ) {
43
+ content_type = "inode/directory"
44
+ } else {
45
+ const [ c , ] = Gio . content_type_guess ( file , null )
46
+ content_type = c
47
+ }
48
+
49
+ this . selections . push ( add ( id , file , content_type ) )
50
+ id += 1
51
+ }
52
+ } catch ( e ) {
53
+ global . log ( `pop-shell: plugin-files: ${ e . message } ` )
54
+ break
55
+ }
56
+ }
57
+ }
58
+ } else {
59
+ this . selections . push ( { id : 0 , name : "file <requires 3 characters minimum>" , description : "" } )
60
+ }
61
+
62
+ return {
63
+ event : "queried" ,
64
+ selections : this . selections
65
+ }
66
+ }
67
+
68
+ submit ( _ : Ext , id : number ) : Response . Response {
69
+ const result = this . selections [ id ]
70
+
71
+ if ( result ) {
72
+ if ( result . description . length === 0 ) {
73
+ return { event : "noop" }
74
+ }
75
+
76
+ try {
77
+ GLib . spawn_command_line_async ( `xdg-open '${ result . description . substr ( 2 ) } '` )
78
+ } catch ( e ) {
79
+ global . log ( `xdg-open failed: ${ e } ` )
80
+ }
81
+ }
82
+
83
+ return { event : "close" }
84
+ }
85
+ }
0 commit comments