Skip to content

Commit

Permalink
page/FindSupport: use TextInputDialog instead of a custom screen_getc…
Browse files Browse the repository at this point in the history
…h() loop
  • Loading branch information
MaxKellermann committed Sep 11, 2024
1 parent 813a480 commit a8f90d3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 51 deletions.
3 changes: 1 addition & 2 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -389,8 +389,7 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd)
CoStart(screen.find_support.Find(lw, *this, cmd));
return true;
case Command::LIST_JUMP:
screen.find_support.Jump(lw, *this, *this);
SchedulePaint();
CoStart(screen.find_support.Jump(lw, *this, *this));
return true;

default:
Expand Down
4 changes: 1 addition & 3 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,7 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
CoStart(screen.find_support.Find(lw, *this, cmd));
return true;
case Command::LIST_JUMP:
screen.find_support.Jump(lw, *this, *this);
SaveSelection();
SchedulePaint();
CoStart(screen.find_support.Jump(lw, *this, *this));
return true;

default:
Expand Down
3 changes: 1 addition & 2 deletions src/TagListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,7 @@ TagListPage::OnCommand(struct mpdclient &c, Command cmd)
return true;

case Command::LIST_JUMP:
find_support.Jump(lw, *this, *this);
SchedulePaint();
CoStart(find_support.Jump(lw, *this, *this));
return true;

default:
Expand Down
53 changes: 10 additions & 43 deletions src/page/FindSupport.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,14 @@

#include "FindSupport.hxx"
#include "screen.hxx"
#include "screen_utils.hxx"
#include "screen_status.hxx"
#include "screen.hxx"
#include "AsyncUserInput.hxx"
#include "i18n.h"
#include "Command.hxx"
#include "dialogs/TextInputDialog.hxx"
#include "ui/Bell.hxx"
#include "ui/Keys.hxx"
#include "ui/ListWindow.hxx"
#include "ui/Options.hxx"
#include "co/InvokeTask.hxx"
#include "util/LocaleString.hxx"

#include <ctype.h>

#define FIND_PROMPT _("Find")
#define RFIND_PROMPT _("Find backward")
Expand Down Expand Up @@ -76,48 +69,22 @@ FindSupport::Find(ListWindow &lw, const ListText &text, Command cmd) noexcept
}
}

void
Co::InvokeTask
FindSupport::Jump(ListWindow &lw,
const ListText &text,
const ListRenderer &renderer) noexcept
{
constexpr size_t WRLN_MAX_LINE_SIZE = 1024;
int key = 65;

char buffer[WRLN_MAX_LINE_SIZE];
TextInputDialog dialog{
screen,
JUMP_PROMPT,
};

/* In last is the whole string which is displayed in the status_window
* and search_str is the string the user entered (without the prompt) */
char *search_str = buffer + snprintf(buffer, WRLN_MAX_LINE_SIZE, "%s: ", JUMP_PROMPT);
char *iter = search_str;
dialog.SetFragile();

while(1) {
key = screen_getch(screen, buffer);
/* if backspace or delete was pressed, process instead of ending loop */
if (IsBackspace(key) || key == KEY_DC) {
const char *prev = PrevCharMB(buffer, iter);
if (search_str <= prev)
iter = const_cast<char *>(prev);
*iter = '\0';
continue;
}
/* if a control key was pressed, end loop */
else if (iscntrl(key) || key == KEY_NPAGE || key == KEY_PPAGE) {
break;
}
else if (iter < buffer + WRLN_MAX_LINE_SIZE - 3) {
*iter++ = key;
*iter = '\0';
}
lw.Jump(text, search_str);

/* repaint the list_window */
dialog.SetModifiedCallback([&lw, &text, &renderer](std::string_view value) noexcept {
lw.Jump(text, value);
lw.Paint(renderer);
lw.Refresh();
}

last = search_str;
});

/* ncmpc should get the command */
keyboard_unread(key);
co_await dialog;
}
3 changes: 2 additions & 1 deletion src/page/FindSupport.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public:

/* 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;
[[nodiscard]]
Co::InvokeTask Jump(ListWindow &lw, const ListText &text, const ListRenderer &renderer) noexcept;

private:
[[nodiscard]]
Expand Down

0 comments on commit a8f90d3

Please sign in to comment.