-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from watawuwu/feature/albert
Feature/albert
- Loading branch information
Showing
8 changed files
with
193 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
#[cfg(target_os = "linux")] | ||
use failure::*; | ||
#[cfg(target_os = "linux")] | ||
use log::*; | ||
#[cfg(target_os = "linux")] | ||
use std::fs; | ||
#[cfg(target_os = "linux")] | ||
use std::path::PathBuf; | ||
|
||
use crate::cargo::CargoConfig; | ||
use crate::error::Result; | ||
#[cfg(target_os = "linux")] | ||
use crate::fs::write_file; | ||
use crate::launcher::LauncherConfig; | ||
#[cfg(target_os = "linux")] | ||
use crate::tpl::{self, Param}; | ||
#[cfg(target_os = "linux")] | ||
const MODULE_TEMPLATE: &[u8] = include_bytes!("asset/albert/__init__.py"); | ||
|
||
#[cfg(target_os = "linux")] | ||
pub fn install(cargo_conf: &CargoConfig, launcher_conf: &LauncherConfig) -> Result<()> { | ||
let workflow_path = make(cargo_conf, launcher_conf)?; | ||
copy(cargo_conf, workflow_path)?; | ||
Ok(()) | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn make(cargo_conf: &CargoConfig, launcher_conf: &LauncherConfig) -> Result<Vec<PathBuf>> { | ||
let module = module_path(&launcher_conf.work_dir)?; | ||
write_file(&module, module_bin(cargo_conf)?.as_bytes())?; | ||
|
||
let icon = icon_path(&launcher_conf.work_dir)?; | ||
write_file(&icon, &launcher_conf.icon(cargo_conf)?[..])?; | ||
|
||
Ok(vec![module, icon]) | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn module_bin(config: &CargoConfig) -> Result<String> { | ||
let mut params = Param::new(); | ||
params.insert("prettyname", config.name()); | ||
params.insert("version", config.version()); | ||
params.insert("trigger", config.name()); | ||
params.insert("author", &config.author()); | ||
|
||
let tpl = String::from_utf8_lossy(MODULE_TEMPLATE).into_owned(); | ||
let contents = tpl::render(&tpl, ¶ms)?; | ||
|
||
Ok(contents) | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn copy(conf: &CargoConfig, paths: Vec<PathBuf>) -> Result<()> { | ||
let sink_dir = application_config(conf)?; | ||
fs::create_dir_all(&sink_dir)?; | ||
for path in paths { | ||
debug!("path: {:?}", &path); | ||
debug!("sink: {:?}", &sink_dir); | ||
let name = path.file_name().ok_or_else(|| err_msg("Not file type"))?; | ||
let mut sink = sink_dir.clone(); | ||
sink.push(name); | ||
fs::copy(&path, sink)?; | ||
} | ||
|
||
show_help(&sink_dir); | ||
Ok(()) | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn show_help(path: &PathBuf) { | ||
let msg = r#" | ||
Install completed!! | ||
Please check the checkbox of the python extension list and activate the setting. | ||
Installed path: "#; | ||
println!("{}{}", msg, path.to_string_lossy()); | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn application_config(cargo_conf: &CargoConfig) -> Result<PathBuf> { | ||
let mut path = dirs::home_dir().ok_or_else(|| err_msg("Notfound home dir"))?; | ||
path.push(".local/share/albert/org.albert.extension.python/modules"); | ||
path.push(cargo_conf.name()); | ||
Ok(path) | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn module_path(dir: &PathBuf) -> Result<PathBuf> { | ||
path(dir, "__init__.py") | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn icon_path(dir: &PathBuf) -> Result<PathBuf> { | ||
path(dir, "icon.png") | ||
} | ||
|
||
#[cfg(target_os = "linux")] | ||
fn path(dir: &PathBuf, name: &str) -> Result<PathBuf> { | ||
let dir_s = dir.to_str().ok_or_else(|| err_msg("NotFound dir path"))?; | ||
Ok(PathBuf::from(format!("{}/{}", dir_s, name))) | ||
} | ||
|
||
#[cfg(not(target_os = "linux"))] | ||
pub fn install(_cargo_conf: &CargoConfig, _launcher_conf: &LauncherConfig) -> Result<()> { | ||
failure::bail!("Albert supported only linux") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import subprocess | ||
|
||
from albertv0 import * | ||
|
||
__iid__ = "PythonInterface/v0.2" | ||
__prettyname__ = "{{prettyname}}" | ||
__version__ = "{{version}}" | ||
__trigger__ = "{{trigger}}" | ||
__author__ = "{{author}}" | ||
__dependencies__ = [] | ||
|
||
iconPath = os.path.join(os.path.dirname(__file__), 'icon.png') | ||
pathlist = ["/usr/local/bin", "~/.local/bin", "~/.cargo/bin"] | ||
|
||
def handleQuery(query): | ||
if not query.isTriggered: | ||
return None | ||
|
||
if len(query.string) <= 1: | ||
return None | ||
|
||
os.environ["PATH"] += os.pathsep + os.pathsep.join(pathlist) | ||
cmd = ["url"] + query.string.split() | ||
pipes = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
|
||
items = [] | ||
if pipes.returncode != 0: | ||
err_msg = "%s. exit code: %s" % (pipes.stderr.strip().decode("utf-8"), pipes.returncode) | ||
items.append(Item( | ||
id = __prettyname__, | ||
icon = iconPath, | ||
text = err_msg, | ||
subtext = "Failed", | ||
actions = [] | ||
)) | ||
else: | ||
out = pipes.stdout.decode("utf-8") | ||
items.append(Item( | ||
id = __prettyname__, | ||
icon = iconPath, | ||
text = out, | ||
subtext = "Success", | ||
actions = [ | ||
ClipAction("Added to Clipboard", out) | ||
] | ||
)) | ||
return items | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod albert; | ||
mod alfred; | ||
mod args; | ||
mod cargo; | ||
|