Skip to content

Commit

Permalink
feat(launcher): Add file search plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Jun 30, 2021
1 parent 7a2f36d commit e161d2b
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 20 deletions.
3 changes: 2 additions & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ Package: pop-shell
Architecture: all
Depends:
${misc:Depends},
pop-shell-shortcuts
pop-shell-shortcuts,
fdfind
Recommends: pop-shell-plugin-system76-power
Replaces: gnome-control-center-data (<< 1:3.38.1-2ubuntu1pop1~)
Breaks: gnome-control-center-data (<< 1:3.38.1-2ubuntu1pop1~)
Expand Down
3 changes: 2 additions & 1 deletion src/launcher_plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export interface Selection {
id: number,
name: string,
description: string,
fill?: string
fill?: string,
content_type?: string
}

/** The trait which all builtin plugins implement */
Expand Down
23 changes: 22 additions & 1 deletion src/launcher_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ import type { Plugin as PluginType, Response } from 'launcher_plugins'

const { Plugin } = plugins

import * as plugin_files from 'plugin_files'
import * as plugin_help from 'plugin_help'
import * as plugin_scripts from 'plugin_scripts'
import * as plugin_shell from 'plugin_shell'
import * as plugin_help from 'plugin_help'

const BUILTIN_HELP: PluginType.Source = {
backend: {
Expand All @@ -30,6 +31,21 @@ const BUILTIN_HELP: PluginType.Source = {
pattern: null
};

export var BUILTIN_FILES: PluginType.Source = {
backend: {
builtin: new plugin_files.ShellBuiltin()
},
config: {
name: "File Search",
description: "Search files in your home folder",
examples: "file COSMIC",
pattern: "^(file)\\s.*",
exec: "",
icon: "system-file-manager"
},
pattern: null
}

export var BUILTINS: Array<PluginType.Source> = [
{
backend: {
Expand Down Expand Up @@ -144,6 +160,11 @@ export class LauncherService {
return
}

if (query.startsWith("file ")) {
yield BUILTIN_FILES;
return;
}

for (const plugin of BUILTINS) {
if (!plugin.pattern || plugin.pattern.test(query)) {
yield plugin
Expand Down
85 changes: 85 additions & 0 deletions src/plugin_files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// @ts-ignore
const Me = imports.misc.extensionUtils.getCurrentExtension()

const { Gio, GLib } = imports.gi

import * as plugins from 'launcher_plugins'
import * as utils from 'utils'

import type { Response, Selection } from 'launcher_plugins'
import type { Ext } from './extension'

function add(id: number, file: string, content_type: string): Selection {
const pos = file.lastIndexOf("/")
return {
id,
name: pos === 0 ? file : file.substr(pos + 1),
description: "~/" + file,
content_type
}
}

export class ShellBuiltin extends plugins.Builtin {
selections: Array<Selection> = []

init() {}

query(_ :Ext, query: string): Response.Response {
let id = 0
this.selections.splice(0)
const search = query.substr(query.indexOf(" ") + 1).trim()
if (search.length > 2) {
const cmd = utils.async_process_ipc(["fdfind", search])
if (cmd) {
while (true) {
try {
const [bytes,read] = cmd.stdout.read_line(null)
if (bytes === null || read === 0) break
const file = imports.byteArray.toString(bytes)
const gfile = Gio.File.new_for_path(file)
if (gfile.query_exists(null)) {
let content_type
if (GLib.file_test (file, GLib.FileTest.IS_DIR)) {
content_type = "inode/directory"
} else {
const [c,] = Gio.content_type_guess(file, null)
content_type = c
}

this.selections.push(add(id, file, content_type))
id += 1
}
} catch (e) {
global.log(`pop-shell: plugin-files: ${e.message}`)
break
}
}
}
} else {
this.selections.push({ id: 0, name: "file <requires 3 characters minimum>", description: "" })
}

return {
event: "queried",
selections: this.selections
}
}

submit(_: Ext, id: number): Response.Response {
const result = this.selections[id]

if (result) {
if (result.description.length === 0) {
return { event: "noop" }
}

try {
GLib.spawn_command_line_async(`xdg-open '${result.description.substr(2)}'`)
} catch (e) {
global.log(`xdg-open failed: ${e}`)
}
}

return { event: "close" }
}
}
31 changes: 18 additions & 13 deletions src/plugin_help.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const Me = imports.misc.extensionUtils.getCurrentExtension()

import * as plugins from 'launcher_plugins'
import * as service from 'launcher_service'

import type { Response, Selection } from 'launcher_plugins'
import type { Ext } from './extension'
Expand All @@ -15,22 +16,26 @@ export class ShellBuiltin extends plugins.Builtin {
this.selections.splice(0);
let id = 0;

for (const [name, service] of ext.window_search.service.plugins) {
if (service.config.pattern?.length > 0) {
const example = service.config.examples
? service.config.examples
: service.config.pattern;

this.selections.push({
id,
name,
description: service.config.description + `: ${example}`,
fill: service.config.fill
})
id += 1;
const files = new Map([[service.BUILTIN_FILES.config.name, service.BUILTIN_FILES]])
for (const map of [files, ext.window_search.service.plugins]) {
for (const [name, service] of map) {
if (service.config.pattern?.length > 0) {
const example = service.config.examples
? service.config.examples
: service.config.pattern;

this.selections.push({
id,
name,
description: service.config.description + `: ${example}`,
fill: service.config.fill
})
id += 1;
}
}
}


return {
event: "queried",
selections: this.selections
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/recent/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class App {
}

query(input) {
input = input.trim()
input = input.substr(input.indexOf(" ") + 1).trim()

const items = this.items()

Expand All @@ -74,7 +74,7 @@ class App {
this.results = items
.filter(item => item.display_name.toLowerCase().includes(normalized))
.sort((a, b) => a.display_name.localeCompare(b.display_name))
.slice(0, 20)
.slice(0, 7)

let id = 0

Expand Down
5 changes: 3 additions & 2 deletions src/plugins/recent/meta.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"name": "Recent Documents",
"description": "Show recent documents in search results",
"pattern": "",
"description": "Show recently-opened files",
"pattern": "^(recent)\\s.*",
"examples": "recent COSMIC",
"exec": "main.js",
"icon": "system-file-manager"
}

0 comments on commit e161d2b

Please sign in to comment.