mirror of
https://github.com/Dushistov/sdcv.git
synced 2025-12-15 17:31:56 +00:00
Merge pull request #57 from alcah/master
Return exit code 2 if search term not found
This commit is contained in:
@@ -316,10 +316,10 @@ private:
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
search_result Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
||||||
{
|
{
|
||||||
if (nullptr == loc_str)
|
if (nullptr == loc_str)
|
||||||
return true;
|
return SEARCH_SUCCESS;
|
||||||
|
|
||||||
std::string query;
|
std::string query;
|
||||||
|
|
||||||
@@ -330,6 +330,7 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
|||||||
gsize bytes_read;
|
gsize bytes_read;
|
||||||
gsize bytes_written;
|
gsize bytes_written;
|
||||||
glib::Error err;
|
glib::Error err;
|
||||||
|
search_result rval = SEARCH_SUCCESS;
|
||||||
glib::CharStr str;
|
glib::CharStr str;
|
||||||
if (!utf8_input_)
|
if (!utf8_input_)
|
||||||
str.reset(g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err)));
|
str.reset(g_locale_to_utf8(loc_str, -1, &bytes_read, &bytes_written, get_addr(err)));
|
||||||
@@ -339,11 +340,11 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
|||||||
if (nullptr == get_impl(str)) {
|
if (nullptr == get_impl(str)) {
|
||||||
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
|
fprintf(stderr, _("Can not convert %s to utf8.\n"), loc_str);
|
||||||
fprintf(stderr, "%s\n", err->message);
|
fprintf(stderr, "%s\n", err->message);
|
||||||
return false;
|
return SEARCH_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str[0] == '\0')
|
if (str[0] == '\0')
|
||||||
return true;
|
return SEARCH_SUCCESS;
|
||||||
|
|
||||||
TSearchResultList res_list;
|
TSearchResultList res_list;
|
||||||
|
|
||||||
@@ -443,10 +444,11 @@ bool Library::process_phrase(const char *loc_str, IReadLine &io, bool force)
|
|||||||
loc_str = utf8_to_locale_ign_err(get_impl(str));
|
loc_str = utf8_to_locale_ign_err(get_impl(str));
|
||||||
if (!json_)
|
if (!json_)
|
||||||
printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? get_impl(str) : loc_str.c_str());
|
printf(_("Nothing similar to %s, sorry :(\n"), utf8_output_ ? get_impl(str) : loc_str.c_str());
|
||||||
|
rval = SEARCH_NO_RESULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json_) {
|
if (json_) {
|
||||||
fputs("]\n", stdout);
|
fputs("]\n", stdout);
|
||||||
}
|
}
|
||||||
return true;
|
return rval;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,6 +23,13 @@ struct TSearchResult {
|
|||||||
|
|
||||||
typedef std::vector<TSearchResult> TSearchResultList;
|
typedef std::vector<TSearchResult> TSearchResultList;
|
||||||
|
|
||||||
|
//possible return values for Library.process_phase()
|
||||||
|
enum search_result {
|
||||||
|
SEARCH_SUCCESS = 0,
|
||||||
|
SEARCH_FAILURE,
|
||||||
|
SEARCH_NO_RESULT
|
||||||
|
};
|
||||||
|
|
||||||
//this class is wrapper around Dicts class for easy use
|
//this class is wrapper around Dicts class for easy use
|
||||||
//of it
|
//of it
|
||||||
class Library : public Libs
|
class Library : public Libs
|
||||||
@@ -38,7 +45,7 @@ public:
|
|||||||
setFuzzy(!no_fuzzy);
|
setFuzzy(!no_fuzzy);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool process_phrase(const char *loc_str, IReadLine &io, bool force = false);
|
search_result process_phrase(const char *loc_str, IReadLine &io, bool force = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool utf8_input_;
|
bool utf8_input_;
|
||||||
|
|||||||
@@ -207,15 +207,16 @@ int main(int argc, char *argv[]) try {
|
|||||||
|
|
||||||
std::unique_ptr<IReadLine> io(create_readline_object());
|
std::unique_ptr<IReadLine> io(create_readline_object());
|
||||||
if (optind < argc) {
|
if (optind < argc) {
|
||||||
|
search_result rval = SEARCH_SUCCESS;
|
||||||
for (int i = optind; i < argc; ++i)
|
for (int i = optind; i < argc; ++i)
|
||||||
if (!lib.process_phrase(argv[i], *io, non_interactive)) {
|
if ((rval = lib.process_phrase(argv[i], *io, non_interactive)) != SEARCH_SUCCESS) {
|
||||||
return EXIT_FAILURE;
|
return rval;
|
||||||
}
|
}
|
||||||
} else if (!non_interactive) {
|
} else if (!non_interactive) {
|
||||||
|
|
||||||
std::string phrase;
|
std::string phrase;
|
||||||
while (io->read(_("Enter word or phrase: "), phrase)) {
|
while (io->read(_("Enter word or phrase: "), phrase)) {
|
||||||
if (!lib.process_phrase(phrase.c_str(), *io))
|
if (lib.process_phrase(phrase.c_str(), *io) == SEARCH_FAILURE)
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
phrase.clear();
|
phrase.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
22
tests/t_return_code
Executable file
22
tests/t_return_code
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
SDCV="$1"
|
||||||
|
TEST_DIR="$2"
|
||||||
|
|
||||||
|
unset SDCV_PAGER
|
||||||
|
|
||||||
|
test_return_code() {
|
||||||
|
WORD=$1
|
||||||
|
EXPECTED=$2
|
||||||
|
$SDCV -e -n --data-dir "$TEST_DIR" -u "Test synonyms" $WORD > /dev/null
|
||||||
|
RC=$?
|
||||||
|
if [ $RC -ne $EXPECTED ]; then
|
||||||
|
echo "Return code for $WORD should be '$EXPECTED' but was '$RC'"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
test_return_code testawordy 0
|
||||||
|
test_return_code testawordies 2
|
||||||
|
|
||||||
|
exit 0
|
||||||
Reference in New Issue
Block a user