cmds.pl 8.71 KB
Newer Older
1
# Copyright 2012 - 2013, Steve Rader
2
# Copyright 2013 - 2018, Scott Kostyshak
3 4 5 6 7

#------------------------------------------------------------------

sub prompt_quit {
  my $yes;
8 9 10 11 12 13
  if ($confirmation) {
    $yes = &prompt_y("Quit?");
    if ( ! $yes ) {
      &draw_prompt_line('');
      return;
    }
14
  }
15
  &clean_exit()
16 17 18 19 20 21 22 23 24 25
}

#------------------------------------------------------------------

sub task_add {
  my $str = &prompt_str("Add: ");
  if ( $str eq '' ) {
    &draw_prompt_line('');
    return;
  }
26 27 28 29
  # TODO: get rid off escaping by replacing the shell call in task_exec() by something like IPC::Open2().
  #       That would allow us to get rid of all shell expansions, quotations and escaping. [BaZo]
  $str =~ s{[&^\\]}{\\\&}g;
  my ($es,$result) = &task_exec("add $str");
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  if ( $es != 0 ) {
    $error_msg = $result;
    &draw_error_msg();
    return;
  }
  $feedback_msg = $result;
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_annotate {
  my $id = $report2taskid[$task_selected_idx];
  my $str = &prompt_str("Annotate: ");
  if ( $str eq '' ) {
    &draw_prompt_line('');
    return;
  }
48 49 50 51 52 53 54 55 56 57 58 59 60
  # This task_exec is different (from, e.g., the one in task_add)
  # because for annotatate we embed quotes since there is nothing
  # else for Taskwarrior to interpret. The advantage is that the
  # user does not need to worry about proper quoting.
  #
  # Because single quotes are the enclosing characters, they must be escaped.
  # This replaces each single quote inside $str with (1) a single quote to end
  # the single-quoting, a double quote to begin a new type of quoting, a single
  # quote (which is now embedded in a double quote so does not need escaping),
  # an end double-quote, and a begin-single-quote that will begin the rest of
  # the quoting. In summary, we rely on the sh and bash feature of stringing together multiple types of quotes
  $str =~ s/'/'"'"'/g;
  my ($es,$result) = &task_exec("$id annotate '$str'");
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  if ( $es != 0 ) {
    $error_msg = $result;
    &draw_error_msg();
    return;
  }
  $feedback_msg = "Annotated task $id.";
  &draw_feedback_msg();
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_den_or_del {
  my ($ch, $str, $yes);
  my $id = $report2taskid[$task_selected_idx];
  for my $t (0 .. $#{ $report_tokens[$task_selected_idx] } ) {
    $str .= "$report_tokens[$task_selected_idx][$t]";
  }
79
  my $target = ( $str !~ s/^\s*\d+[\/-]\d+[\/-]\d+\s+// )
80 81 82
             ? "task"
             : "annotation";
  $str =~ s/\s+$//;
83 84 85 86 87 88
  if ($confirmation) {
    $yes = &prompt_y("Delete current $target? ");
    if ( ! $yes ) {
      &draw_prompt_line('');
      return;
    }
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  }
  my ($es,$result) = ($target eq "annotation")
                   ? &task_exec("$id denotate \"$str\"")
                   : &task_exec("$id delete rc.confirmation:no");
  if ( $es != 0 ) {
    $error_msg = $result;
    &draw_error_msg();
    return;
  }
  $feedback_msg = "Deleted $target.";
  &draw_feedback_msg();
  &flash_current_task();
  $reread_needed = 1;
}

#------------------------------------------------------------------

106
sub task_start_stop {
107 108
  my ($ch, $str, $yes);
  my $id = $report2taskid[$task_selected_idx];
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  my ($state, $result1) = &task_exec("$id active");
  my $prompt = "stop";
  $feedback_msg = "Stopped task";

  if ($state != 0) {
    $prompt = "start";
    $feedback_msg = "Started task";
  }

  if ($confirmation) {
    $yes = &prompt_y("$prompt task?");
    if ( ! $yes ) {
      &draw_prompt_line('');
      return;
    }
  }

  my ($es, $result2) = &task_exec("$id $prompt");
  if ( $es != 0 ) {
    $error_msg = $result2;
    &draw_error_msg();
131 132
    return;
  }
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

  &draw_feedback_msg();
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_done {
  my ($ch, $str, $yes);
  my $id = $report2taskid[$task_selected_idx];
  if ($confirmation) {
    $yes = &prompt_y("Mark task $id done? ");
    if ( ! $yes ) {
      &draw_prompt_line('');
      return;
    }
  }
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  my ($es,$result) = &task_exec("$id done");
  if ( $es != 0 ) {
    $error_msg = $result;
    &draw_error_msg();
    return;
  }
  $feedback_msg = "Marked task done.";
  &draw_feedback_msg();
  &flash_current_task();
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_filter {
  my ($c, $f);
  if ( $current_command =~ /(.*?)\s+(.*)/ ) {
    ($c,$f) = ($1,$2);
  } else {
    $c = $current_command;
    $f = '';
  }
  my $str = &prompt_str("Filter: $f");
  if ( $str eq '' ) {
    &draw_prompt_line('');
    $current_command = $c;
    if ( $f ne '' ) { $reread_needed = 1; }
    return;
  }
  $prev_command = $current_command;
  $current_command = "$c $str";
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_modify {
  my $args = $_[0];
  my $id = $report2taskid[$task_selected_idx];
189
  &shell_exec("$task $id modify $args",'wait');
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_modify_prompt {
  my $id = $report2taskid[$task_selected_idx];
  my $str = &prompt_str("Modify: ");
  if ( $str eq '' ) {
    &draw_prompt_line('');
    return;
  }
  &task_modify("$str");
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_set_priority {
  my $id = $report2taskid[$task_selected_idx];
  my $prio = &task_info('Priority');
  if ( $prio eq '' ) {
    $prio = 'N';
  }
  my $p = &prompt_chr("Change priority (l/m/h/n): ");
  $p = uc($p);
  if ( $p ne $prio && $p =~ /[LMHN]/ ) {
    if ( $p eq 'N' ) {
      $p = '';
    }
220
    my ($es,$result) = &task_exec("$id modify 'priority:$p'");
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    if ( $es != 0 ) {
      $error_msg = $result;
      &draw_error_msg();
      return;
    }
    $feedback_msg = "Modified task $id.";
    &flash_current_task();
    $reread_needed = 1;
  }
  else {
    &draw_prompt_line('');
    return;
  }
}

#------------------------------------------------------------------

sub task_set_project {
  my $id = $report2taskid[$task_selected_idx];
  my $p = &prompt_str("Project: ");
  if ( $p eq '' ) {
    &draw_prompt_line('');
    return;
  }
  my $proj = &task_info('Project');
  if ( $p eq $proj ) {
    beep();
    return;
  }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
  my ($es,$result) = &task_exec("$id modify 'project:$p'");
  if ( $es != 0 ) {
    $error_msg = $result;
    &draw_error_msg();
    return;
  }
  $feedback_msg = "Modified task $id.";
  &flash_current_task();
  $reread_needed = 1;
}

#------------------------------------------------------------------

sub task_set_wait {
  my $id = $report2taskid[$task_selected_idx];
  my $w = &prompt_str("Wait: ");
  if ( $w eq '' ) {
    &draw_prompt_line('');
    return;
  }
  my ($es,$result) = &task_exec("$id modify 'wait:$w'");
271 272 273 274 275 276 277 278 279 280 281 282
  if ( $es != 0 ) {
    $error_msg = $result;
    &draw_error_msg();
    return;
  }
  $feedback_msg = "Modified task $id.";
  &flash_current_task();
  $reread_needed = 1;
}

#------------------------------------------------------------------

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
sub task_set_tag {
  my $id = $report2taskid[$task_selected_idx];
  my $tags = &prompt_str("Tag: ");
  if ( $tags eq '' ) {
    &draw_prompt_line('');
    return;
  }

  # multiple tags can be input separated by a combination of spaces and commas
  # note that this input format precludes inputting a comma as part of a tag
  #
  # keep track of current modifier (default:+) and use it for subsequent tags
  # so "+a b c" means "+a +b +c" and "+a -b c" means "+a -b -c"
  my $mod='+';
  foreach my $t (split(/,?\s+|,/,$tags)) {
    next if $t =~ m/^\s*$/;

    # check if a + or - was specified
    my $fc = substr($t,0,1);
    if ( $fc eq '+'  or  $fc eq '-' ) {
      # if so, save the current modifier
      $mod = $fc;
    } else {
      # if not, add the current modifier
      $t = $mod . $t;
    }

    my ($es,$result) = &task_exec("$id modify '$t'");
    if ( $es != 0 ) {
      $error_msg = $result;
      &draw_error_msg();
      return;
    }
  }

  $feedback_msg = "Modified task $id.";
  &flash_current_task();
  $reread_needed = 1;
}

#------------------------------------------------------------------

325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
sub shell_command {
  my $args = $_[0];
  my ($opts, $cmd);
  if ( $args =~ /([^ ]*) (.+)/ ) {
    $opts = $1;
    $cmd = $2;
  }
  else {
    $error_msg = "Empty shell command for ':!'. See help (:h).";
    &draw_error_msg();
    return;
  }

  my $wait = "no-wait";
  foreach my $l ( split //, $opts ) {
    if ( $l eq 'r' ) {
      $reread_needed = 1;
    } elsif ( $l eq 'w' ) {
      $wait = "wait";
    } else {
      $error_msg = "$l is not a valid command option to ':!'. See help (:h).";
      &draw_error_msg();
      return;
    }
  }

  $cmd =~ s/%TASKID/$report2taskid[$task_selected_idx]/g;
  $cmd =~ s/%TASKARGS/$current_command/g;

  &shell_exec($cmd,"$wait");
}

return 1;