Skip to content

Commit

Permalink
Minor edits (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagprog5 authored Jul 28, 2023
1 parent f4b4e96 commit 493c8a4
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
2 changes: 1 addition & 1 deletion scripts/choose.bash
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ch_hist() {
IFS= read -r -d ''
cat
} | grep -zi -- "$*" |
choose -0uet --delimit-not-at-end --flip -p "Select a line to edit then run.")
choose -0uet --delimit-not-at-end --reverse -p "Select a line to edit then run.")

if [ -z "$LINE" ]; then
echo "empty line"
Expand Down
20 changes: 8 additions & 12 deletions src/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,18 +223,14 @@ void print_help_message() {
" --buf-size <# bytes, default: " choose_xstr(BUF_SIZE_DEFAULT) ">\n"
" size of match buffer used. patterns that require more room will\n"
" never successfully match\n"
" --buf-size-frag <# bytes, default: <buf-size * 8>\n"
" this is only applicable if --match is not specified, meaning the\n"
" input delimiter is being matched. if the match buffer is full\n"
" when attempting to complete a match, the bytes at the beginning\n"
" of the buffer that have no possibility of being a part of a\n"
" match are moved to free up space in the buffer. they are moved\n"
" either directly to the output if the args allow for it, or to a\n"
" separate buffer where they are accumulated until the token is\n"
" completed. this separate buffer is cleared if its size would\n"
" exceed this arg. the bytes are instead sent directly to the\n"
" output if no ordered ops are specified, and there is no sorting,\n"
" no uniqueness, no reverse, and no tui used\n"
" --buf-size-frag <# bytes, default: <<buf-size> * 8>\n"
" this is only applicable if --match and --sed are not specified,\n"
" meaning the input delimiter is being matched. assuming buf-size\n"
" is less than buf-size-frag, then this is the maximum size of a\n"
" token that can be created. if it's size would exceed this arg\n"
" then the content thus far is discarded. this limit is avoided\n"
" in the special case where there is no ordered ops, no sorting,\n"
" no uniqueness, no reverse, and no tui used.\n"
" --comp <less than comparison>\n"
" user defined comparison. pairs of tokens are evaluated. if only\n"
" one matches, then it is less than the other. required by\n"
Expand Down
6 changes: 6 additions & 0 deletions src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,12 @@ BOOST_AUTO_TEST_CASE(sort) {
BOOST_REQUIRE_EQUAL(out, correct_output);
}

BOOST_AUTO_TEST_CASE(sort_partial) {
choose_output out = run_choose("this\nis\na\ntest", {"--sort", "--out=2", "-t"});
choose_output correct_output{std::vector<choose::Token>{"a", "is"}};
BOOST_REQUIRE_EQUAL(out, correct_output);
}

BOOST_AUTO_TEST_CASE(unique) {
choose_output out = run_choose("this\nis\nis\na\na\ntest", {"--unique", "-t"});
choose_output correct_output{std::vector<choose::Token>{"this", "is", "a", "test"}};
Expand Down
15 changes: 11 additions & 4 deletions src/token.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,10 +658,17 @@ std::vector<Token> create_tokens(choose::Arguments& args) {
set->clear();
}

if (args.comp_sort) {
std::stable_sort(output.begin(), output.end(), user_defined_comparison);
} else if (args.sort) {
std::sort(output.begin(), output.end(), lexicographical_comparison);
if (args.out.has_value() && output.size() > *args.out && args.sort && !args.comp_sort) {
// if lexicographically sorting and the output is being truncated then do a
// partial sort instead. can only be applied to lexicographical since there's no
// stable partial sort (and stability is required for user defined comp sort)
std::partial_sort(output.begin(), output.begin() + *args.out, output.end(), lexicographical_comparison);
} else {
if (args.comp_sort) {
std::stable_sort(output.begin(), output.end(), user_defined_comparison);
} else if (args.sort) {
std::sort(output.begin(), output.end(), lexicographical_comparison);
}
}

if (args.reverse) {
Expand Down

0 comments on commit 493c8a4

Please sign in to comment.