|
Lines 1-6
Link Here
|
| 1 |
//////////////////////////////////////////////////////////////////////////////// |
1 |
//////////////////////////////////////////////////////////////////////////////// |
| 2 |
// |
2 |
// |
| 3 |
// Copyright 2006 - 2014, Paul Beckingham, Federico Hernandez. |
3 |
// Copyright 2006 - 2017, Paul Beckingham, Federico Hernandez. |
| 4 |
// |
4 |
// |
| 5 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
5 |
// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
// of this software and associated documentation files (the "Software"), to deal |
6 |
// of this software and associated documentation files (the "Software"), to deal |
|
Lines 26-36
Link Here
|
| 26 |
|
26 |
|
| 27 |
#include <cmake.h> |
27 |
#include <cmake.h> |
| 28 |
#include <iostream> |
28 |
#include <iostream> |
|
|
29 |
#include <vector> |
| 29 |
#include <string> |
30 |
#include <string> |
| 30 |
#include <cstring> |
31 |
#include <cstring> |
|
|
32 |
#include <cstdio> |
| 31 |
#include <stdlib.h> |
33 |
#include <stdlib.h> |
| 32 |
#include <i18n.h> |
34 |
#include <unistd.h> |
| 33 |
#include <text.h> |
35 |
#include <shared.h> |
| 34 |
|
36 |
|
| 35 |
#ifdef HAVE_READLINE |
37 |
#ifdef HAVE_READLINE |
| 36 |
#include <readline/readline.h> |
38 |
#include <readline/readline.h> |
|
Lines 44-59
Link Here
|
| 44 |
// tasksh commands. |
46 |
// tasksh commands. |
| 45 |
int cmdHelp (); |
47 |
int cmdHelp (); |
| 46 |
int cmdDiagnostics (); |
48 |
int cmdDiagnostics (); |
| 47 |
std::string composePrompt (); |
49 |
int cmdReview (const std::vector <std::string>&, bool); |
|
|
50 |
int cmdShell (const std::vector <std::string>&); |
| 51 |
std::string promptCompose (); |
| 48 |
std::string findTaskwarrior (); |
52 |
std::string findTaskwarrior (); |
| 49 |
|
53 |
|
| 50 |
//////////////////////////////////////////////////////////////////////////////// |
54 |
//////////////////////////////////////////////////////////////////////////////// |
| 51 |
static int commandLoop () |
55 |
static void welcome () |
| 52 |
{ |
56 |
{ |
| 53 |
// TODO Local data: timer, context stack. No globals. |
57 |
std::cout << PACKAGE_STRING << "\n"; |
|
|
58 |
cmdHelp (); |
| 59 |
} |
| 54 |
|
60 |
|
| 55 |
// Compose the prompt. |
61 |
//////////////////////////////////////////////////////////////////////////////// |
| 56 |
std::string prompt = composePrompt (); |
62 |
const std::string getResponse (const std::string& prompt) |
|
|
63 |
{ |
| 64 |
std::string response {""}; |
| 57 |
|
65 |
|
| 58 |
// Display prompt, get input. |
66 |
// Display prompt, get input. |
| 59 |
#ifdef HAVE_READLINE |
67 |
#ifdef HAVE_READLINE |
|
Lines 61-100
Link Here
|
| 61 |
if (! line_read) |
69 |
if (! line_read) |
| 62 |
{ |
70 |
{ |
| 63 |
std::cout << "\n"; |
71 |
std::cout << "\n"; |
| 64 |
return -1; |
72 |
response = "<EOF>"; |
| 65 |
} |
73 |
} |
|
|
74 |
else |
| 75 |
{ |
| 76 |
// Save history. |
| 77 |
if (*line_read) |
| 78 |
add_history (line_read); |
| 66 |
|
79 |
|
| 67 |
// Save history. |
80 |
response = std::string (line_read); |
| 68 |
if (*line_read) |
81 |
free (line_read); |
| 69 |
add_history (line_read); |
82 |
} |
| 70 |
|
|
|
| 71 |
std::string command (line_read); |
| 72 |
free (line_read); |
| 73 |
#else |
83 |
#else |
| 74 |
std::cout << prompt; |
84 |
std::cout << prompt; |
| 75 |
std::string command; |
85 |
std::getline (std::cin, response); |
| 76 |
std::getline (std::cin, command); |
|
|
| 77 |
if (std::cin.eof () == 1) |
86 |
if (std::cin.eof () == 1) |
| 78 |
{ |
87 |
{ |
| 79 |
std::cout << "\n"; |
88 |
std::cout << "\n"; |
| 80 |
return -1; |
89 |
response = "<EOF>"; |
| 81 |
} |
90 |
} |
| 82 |
#endif |
91 |
#endif |
| 83 |
|
92 |
|
| 84 |
// Dispatch command |
93 |
return response; |
|
|
94 |
} |
| 95 |
|
| 96 |
//////////////////////////////////////////////////////////////////////////////// |
| 97 |
static int commandLoop (bool autoClear) |
| 98 |
{ |
| 99 |
// Compose the prompt. |
| 100 |
auto prompt = promptCompose (); |
| 101 |
|
| 102 |
// Display prompt, get input. |
| 103 |
auto command = getResponse (prompt); |
| 104 |
|
| 105 |
// Obey Taskwarrior's rc.tasksh.autoclear. |
| 106 |
if (autoClear) |
| 107 |
std::cout << "\033[2J\033[0;0H"; |
| 108 |
|
| 85 |
int status = 0; |
109 |
int status = 0; |
| 86 |
if (closeEnough ("exit", command, 3)) status = -1; |
110 |
if (! isatty (fileno (stdin)) && command == "") |
| 87 |
else if (closeEnough ("quit", command, 3)) status = -1; |
111 |
{ |
| 88 |
else if (closeEnough ("help", command, 3)) status = cmdHelp (); |
112 |
status = -1; |
| 89 |
else if (closeEnough ("diagnostics", command, 3)) status = cmdDiagnostics (); |
113 |
} |
| 90 |
else if (command != "") |
114 |
else if (command != "") |
| 91 |
{ |
115 |
{ |
| 92 |
std::cout << "[task " << command << "]\n"; |
116 |
auto args = split (command, ' '); |
| 93 |
command = "task " + command; |
117 |
|
| 94 |
system (command.c_str ()); |
118 |
// Dispatch command. |
|
|
119 |
if (args[0] == "<EOF>") status = -1; |
| 120 |
else if (closeEnough ("exit", args[0], 3)) status = -1; |
| 121 |
else if (closeEnough ("quit", args[0], 3)) status = -1; |
| 122 |
else if (closeEnough ("help", args[0], 3)) status = cmdHelp (); |
| 123 |
else if (closeEnough ("diagnostics", args[0], 3)) status = cmdDiagnostics (); |
| 124 |
else if (closeEnough ("review", args[0], 3)) status = cmdReview (args, autoClear); |
| 125 |
else if (closeEnough ("exec", args[0], 3) || |
| 126 |
args[0][0] == '!') status = cmdShell (args); |
| 127 |
else if (command != "") |
| 128 |
{ |
| 129 |
command = "task " + command; |
| 130 |
std::cout << "[" << command << "]\n"; |
| 131 |
system (command.c_str ()); |
| 95 |
|
132 |
|
| 96 |
// Deliberately ignoreѕ taskwarrior exit status, otherwise empty filters |
133 |
// Deliberately ignoreѕ taskwarrior exit status, otherwise empty filters |
| 97 |
// cause the shell to terminate. |
134 |
// cause the shell to terminate. |
|
|
135 |
} |
| 98 |
} |
136 |
} |
| 99 |
|
137 |
|
| 100 |
return status; |
138 |
return status; |
|
Lines 114-120
Link Here
|
| 114 |
{ |
152 |
{ |
| 115 |
try |
153 |
try |
| 116 |
{ |
154 |
{ |
| 117 |
while ((status = commandLoop ()) == 0) |
155 |
// Get the Taskwarrior rc.tasksh.autoclear Boolean setting. |
|
|
156 |
bool autoClear = false; |
| 157 |
std::string input; |
| 158 |
std::string output; |
| 159 |
execute ("task", {"_get", "rc.tasksh.autoclear"}, input, output); |
| 160 |
output = lowerCase (output); |
| 161 |
autoClear = (output == "true\n" || |
| 162 |
output == "1\n" || |
| 163 |
output == "y\n" || |
| 164 |
output == "yes\n" || |
| 165 |
output == "on\n"); |
| 166 |
|
| 167 |
if (isatty (fileno (stdin))) |
| 168 |
welcome (); |
| 169 |
|
| 170 |
while ((status = commandLoop (autoClear)) == 0) |
| 118 |
; |
171 |
; |
| 119 |
} |
172 |
} |
| 120 |
|
173 |
|