Skip to content

Commit

Permalink
page/FindSupport: convert Find() to coroutine
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 10, 2024
1 parent 29f5845 commit dc0ed2f
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 45 deletions.
3 changes: 1 addition & 2 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,7 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen.find_support.Find(lw, *this, cmd);
SchedulePaint();
CoStart(screen.find_support.Find(lw, *this, cmd));
return true;
case Command::LIST_JUMP:
screen.find_support.Jump(lw, *this, *this);
Expand Down
6 changes: 2 additions & 4 deletions src/KeyDefPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,7 @@ CommandKeysPage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen.find_support.Find(lw, *this, cmd);
SchedulePaint();
CoStart(screen.find_support.Find(lw, *this, cmd));
return true;

default:
Expand Down Expand Up @@ -484,8 +483,7 @@ CommandListPage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen.find_support.Find(lw, *this, cmd);
SchedulePaint();
CoStart(screen.find_support.Find(lw, *this, cmd));
return true;

default:
Expand Down
12 changes: 9 additions & 3 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ class QueuePage final : public ListPage, ListRenderer, ListText {
bool PaintStatusBarOverride(Window window) const noexcept override;
void Update(struct mpdclient &c, unsigned events) noexcept override;
bool OnCommand(struct mpdclient &c, Command cmd) override;
void OnCoComplete() noexcept override;

#ifdef HAVE_GETMOUSE
bool OnMouse(struct mpdclient &c, Point p, mmask_t bstate) override;
Expand Down Expand Up @@ -548,9 +549,7 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen.find_support.Find(lw, *this, cmd);
SaveSelection();
SchedulePaint();
CoStart(screen.find_support.Find(lw, *this, cmd));
return true;
case Command::LIST_JUMP:
screen.find_support.Jump(lw, *this, *this);
Expand Down Expand Up @@ -644,6 +643,13 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
return false;
}

void
QueuePage::OnCoComplete() noexcept
{
SaveSelection();
ListPage::OnCoComplete();
}

const PageMeta screen_queue = {
"playlist",
N_("Queue"),
Expand Down
3 changes: 1 addition & 2 deletions src/TagListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ TagListPage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
find_support.Find(lw, *this, cmd);
SchedulePaint();
CoStart(find_support.Find(lw, *this, cmd));
return true;

case Command::LIST_JUMP:
Expand Down
66 changes: 34 additions & 32 deletions src/page/FindSupport.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "ui/Bell.hxx"
#include "ui/ListWindow.hxx"
#include "ui/Options.hxx"
#include "co/InvokeTask.hxx"
#include "util/LocaleString.hxx"

#include <ctype.h>
Expand All @@ -19,52 +20,53 @@
#define RFIND_PROMPT _("Find backward")
#define JUMP_PROMPT _("Jump")

bool
FindSupport::Find(ListWindow &lw, const ListText &text, Command findcmd) noexcept
inline Co::InvokeTask
FindSupport::DoFind(ListWindow &lw, const ListText &text, bool reversed) noexcept
{
bool found;
const char *prompt = FIND_PROMPT;
if (last.empty()) {
const char *const prompt = reversed ? RFIND_PROMPT : FIND_PROMPT;
char *value = ui_options.find_show_last_pattern
? (char *) -1 : nullptr;
last = screen_readln(screen,
prompt,
value,
&history,
nullptr);
}

if (last.empty())
co_return;

bool found = reversed
? lw.ReverseFind(text, last)
: lw.Find(text, last);
if (!found) {
screen_status_printf(_("Unable to find \'%s\'"),
last.c_str());
Bell();
}
}

Co::InvokeTask
FindSupport::Find(ListWindow &lw, const ListText &text, Command cmd) noexcept
{
const bool reversed =
findcmd == Command::LIST_RFIND ||
findcmd == Command::LIST_RFIND_NEXT;
if (reversed)
prompt = RFIND_PROMPT;
cmd == Command::LIST_RFIND ||
cmd == Command::LIST_RFIND_NEXT;

switch (findcmd) {
switch (cmd) {
case Command::LIST_FIND:
case Command::LIST_RFIND:
last.clear();
/* fall through */

case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
if (last.empty()) {
char *value = ui_options.find_show_last_pattern
? (char *) -1 : nullptr;
last = screen_readln(screen,
prompt,
value,
&history,
nullptr);
}
return DoFind(lw, text, reversed);

if (last.empty())
return true;

found = reversed
? lw.ReverseFind(text, last)
: lw.Find(text, last);
if (!found) {
screen_status_printf(_("Unable to find \'%s\'"),
last.c_str());
Bell();
}
return true;
default:
break;
return {};
}
return false;
}

void
Expand Down
10 changes: 8 additions & 2 deletions src/page/FindSupport.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include <string>

namespace Co { class InvokeTask; }
enum class Command : unsigned;
class ScreenManager;
class ListWindow;
Expand All @@ -30,11 +31,16 @@ public:
* @param findcmd the search command/mode
* @param callback_fn a function returning the text of a given line
* @param callback_data a pointer passed to callback_fn
* @return true if the command has been handled, false if not
* @return a task if the command has been handled, an empty task if not
*/
bool Find(ListWindow &lw, const ListText &text, Command cmd) noexcept;
[[nodiscard]]
Co::InvokeTask Find(ListWindow &lw, const ListText &text, Command cmd) noexcept;

/* query user for a string and jump to the entry
* which begins with this string while the users types */
void Jump(ListWindow &lw, const ListText &text, const ListRenderer &renderer) noexcept;

private:
[[nodiscard]]
Co::InvokeTask DoFind(ListWindow &lw, const ListText &text, bool reversed) noexcept;
};

0 comments on commit dc0ed2f

Please sign in to comment.