Merge pull request #141 from kdheepak/add-shortcuts

This commit is contained in:
Dheepak Krishnamurthy 2021-03-18 07:34:24 -06:00 committed by GitHub
commit 1e67c5af7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 158 additions and 0 deletions

View file

@ -47,6 +47,8 @@ Keybindings for task report:
!: {string} - Custom shell command
0-9: {string} - Run user defined shortcuts
c: context switcher menu - Open context switcher menu
?: help - Help menu

View file

@ -103,6 +103,9 @@ Keybindings for task report:
`!`
: {string} - Custom shell command
`0-9`
: {string} - Run user defined shortcuts
`c`
: context switcher menu - Open context switcher menu

View file

@ -1083,6 +1083,47 @@ impl TTApp {
}
}
pub fn task_shortcut(&mut self, s: usize) -> Result<(), String> {
if self.tasks.lock().unwrap().is_empty() {
return Ok(());
}
let selected = self.task_table_state.selected().unwrap_or_default();
let task_id = self.tasks.lock().unwrap()[selected].id().unwrap_or_default();
let task_uuid = *self.tasks.lock().unwrap()[selected].uuid();
let shell = &self.config.uda_shortcuts[s];
if shell.is_empty() {
return Err("Trying to run empty shortcut.".to_string());
}
let shell = format!("{} {}", shell, task_uuid);
let shell = shellexpand::tilde(&shell).into_owned();
match shlex::split(&shell) {
Some(cmd) => {
let mut command = Command::new(&cmd[0]);
for s in cmd.iter().skip(1) {
command.arg(&s);
}
let output = command.output();
match output {
Ok(o) => {
if o.status.success() {
Ok(())
} else {
Err(format!(
"Unable to run shortcut {}. Failed with status code {}",
s,
o.status.code().unwrap()
))
}
}
Err(s) => Err(format!("`{}` failed: {}", shell, s,)),
}
}
None => Err(format!("Unable to run `{}`. Cannot shlex split `{}`", shell, shell)),
}
}
pub fn task_modify(&mut self) -> Result<(), String> {
if self.tasks.lock().unwrap().is_empty() {
return Ok(());
@ -1562,6 +1603,86 @@ impl TTApp {
self.mode = AppMode::TaskHelpPopup;
} else if input == self.keyconfig.filter {
self.mode = AppMode::TaskFilter;
} else if input == self.keyconfig.shortcut0 {
match self.task_shortcut(0) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut1 {
match self.task_shortcut(1) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut2 {
match self.task_shortcut(2) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut3 {
match self.task_shortcut(3) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut4 {
match self.task_shortcut(4) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut5 {
match self.task_shortcut(5) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut6 {
match self.task_shortcut(6) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut7 {
match self.task_shortcut(7) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut8 {
match self.task_shortcut(8) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.shortcut9 {
match self.task_shortcut(9) {
Ok(_) => self.update(true)?,
Err(e) => {
self.mode = AppMode::TaskError;
self.error = e;
}
}
} else if input == self.keyconfig.zoom {
self.task_report_show_info = !self.task_report_show_info;
} else if input == self.keyconfig.context_menu {

View file

@ -52,6 +52,7 @@ pub struct Config {
pub uda_calendar_months_per_row: usize,
pub uda_style_context_active: Style,
pub uda_style_calendar_title: Style,
pub uda_shortcuts: Vec<String>,
}
impl Config {
@ -76,6 +77,7 @@ impl Config {
uda_calendar_months_per_row: Self::get_uda_months_per_row(),
uda_style_calendar_title: Self::get_uda_style("calendar.title").unwrap_or_default(),
uda_style_context_active: Self::get_uda_style("context.active").unwrap_or_default(),
uda_shortcuts: Self::get_uda_shortcuts(),
})
}
@ -83,6 +85,16 @@ impl Config {
HashMap::new()
}
fn get_uda_shortcuts() -> Vec<String> {
let mut v = vec![];
for s in 0..9 {
let c = format!("uda.taskwarrior-tui.shortcuts.{}", s);
let s = Self::get_config(&c);
v.push(s);
}
v
}
fn get_uda_style(config: &str) -> Option<Style> {
let c = format!("uda.taskwarrior-tui.style.{}", config);
let s = Self::get_config(&c);

View file

@ -31,6 +31,16 @@ pub struct KeyConfig {
pub context_menu: Key,
pub next_tab: Key,
pub previous_tab: Key,
pub shortcut0: Key,
pub shortcut1: Key,
pub shortcut2: Key,
pub shortcut3: Key,
pub shortcut4: Key,
pub shortcut5: Key,
pub shortcut6: Key,
pub shortcut7: Key,
pub shortcut8: Key,
pub shortcut9: Key,
}
impl Default for KeyConfig {
@ -60,6 +70,16 @@ impl Default for KeyConfig {
context_menu: Key::Char('c'),
next_tab: Key::Char(']'),
previous_tab: Key::Char('['),
shortcut0: Key::Char('0'),
shortcut1: Key::Char('1'),
shortcut2: Key::Char('2'),
shortcut3: Key::Char('3'),
shortcut4: Key::Char('4'),
shortcut5: Key::Char('5'),
shortcut6: Key::Char('6'),
shortcut7: Key::Char('7'),
shortcut8: Key::Char('8'),
shortcut9: Key::Char('9'),
}
}
}