Skip to content

Commit

Permalink
screen_find: convert to class
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Sep 9, 2024
1 parent b01d1ee commit 22a3edb
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 99 deletions.
1 change: 0 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,6 @@ ncmpc = executable('ncmpc',
'src/screen_utils.cxx',
'src/screen_status.cxx',
'src/screen_list.cxx',
'src/screen_find.cxx',
'src/screen_client.cxx',
'src/QueuePage.cxx',
'src/FileListPage.cxx',
Expand Down
6 changes: 5 additions & 1 deletion src/ChatPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "ChatPage.hxx"
#include "PageMeta.hxx"
#include "screen.hxx"
#include "screen_utils.hxx"
#include "screen_status.hxx"
#include "mpdclient.hxx"
Expand All @@ -25,14 +26,17 @@ using std::string_view_literals::operator""sv;
static constexpr char chat_channel[] = "chat";

class ChatPage final : public TextPage {
ScreenManager &screen;

unsigned last_connection_id = 0;
bool was_supported = false;

std::string prefix;

public:
ChatPage(ScreenManager &_screen, const Window _window, Size size)
:TextPage(_screen, _window, size) {}
:TextPage(_screen, _window, size, _screen.find_support),
screen(_screen) {}

private:
bool CheckChatSupport(struct mpdclient &c);
Expand Down
5 changes: 2 additions & 3 deletions src/FileListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "EditPlaylistPage.hxx"
#include "Command.hxx"
#include "screen_status.hxx"
#include "screen_find.hxx"
#include "screen.hxx"
#include "i18n.h"
#include "Options.hxx"
Expand Down Expand Up @@ -387,11 +386,11 @@ FileListPage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen_find(screen, lw, cmd, *this);
screen.find_support.Find(lw, *this, cmd);
SchedulePaint();
return true;
case Command::LIST_JUMP:
screen_jump(screen, lw, *this, *this);
screen.find_support.Jump(lw, *this, *this);
SchedulePaint();
return true;

Expand Down
3 changes: 1 addition & 2 deletions src/HelpPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "HelpPage.hxx"
#include "PageMeta.hxx"
#include "screen_find.hxx"
#include "Bindings.hxx"
#include "GlobalBindings.hxx"
#include "config.h"
Expand Down Expand Up @@ -310,7 +309,7 @@ HelpPage::OnCommand(struct mpdclient &c, Command cmd)
search */
lw.SetCursorFromOrigin(0);

if (screen_find(screen, lw, cmd, *this)) {
if (screen.find_support.Find(lw, *this, cmd)) {
SchedulePaint();
return true;
}
Expand Down
5 changes: 2 additions & 3 deletions src/KeyDefPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "PageMeta.hxx"
#include "screen.hxx"
#include "screen_status.hxx"
#include "screen_find.hxx"
#include "KeyName.hxx"
#include "i18n.h"
#include "ConfigFile.hxx"
Expand Down Expand Up @@ -277,7 +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(screen, lw, cmd, *this);
screen.find_support.Find(lw, *this, cmd);
SchedulePaint();
return true;

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

Expand Down
2 changes: 1 addition & 1 deletion src/LibraryPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class LibraryTagListPage final : public TagListPage {
const enum mpd_tag_type _tag,
const char *_all_text,
const Window _window, Size size) noexcept
:TagListPage(_container, _screen, _parent, _tag, _all_text, _window, size),
:TagListPage(_container, _screen.find_support, _parent, _tag, _all_text, _window, size),
library_page(_library_page) {}

protected:
Expand Down
12 changes: 8 additions & 4 deletions src/LyricsPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ class LyricsPage final : public TextPage, PluginResponseHandler {
CoarseTimerEvent plugin_timeout;

public:
LyricsPage(ScreenManager &_screen, const Window _window, Size size) noexcept
:TextPage(_screen, _window, size),
plugin_timeout(_screen.GetEventLoop(),
LyricsPage(EventLoop &event_loop,
PageContainer &_parent, const Window _window, Size size,
FindSupport &_find_support) noexcept
:TextPage(_parent, _window, size, _find_support),
plugin_timeout(event_loop,
BIND_THIS_METHOD(OnTimeout)) {}

~LyricsPage() noexcept override {
Expand Down Expand Up @@ -272,7 +274,9 @@ LyricsPage::Reload() noexcept
static std::unique_ptr<Page>
lyrics_screen_init(ScreenManager &_screen, const Window window, Size size) noexcept
{
return std::make_unique<LyricsPage>(_screen, window, size);
return std::make_unique<LyricsPage>(_screen.GetEventLoop(),
_screen, window, size,
_screen.find_support);
}

void
Expand Down
5 changes: 2 additions & 3 deletions src/QueuePage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "QueuePage.hxx"
#include "PageMeta.hxx"
#include "screen_status.hxx"
#include "screen_find.hxx"
#include "save_playlist.hxx"
#include "config.h"
#include "i18n.h"
Expand Down Expand Up @@ -544,12 +543,12 @@ QueuePage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen_find(screen, lw, cmd, *this);
screen.find_support.Find(lw, *this, cmd);
SaveSelection();
SchedulePaint();
return true;
case Command::LIST_JUMP:
screen_jump(screen, lw, *this, *this);
screen.find_support.Jump(lw, *this, *this);
SaveSelection();
SchedulePaint();
return true;
Expand Down
3 changes: 1 addition & 2 deletions src/SongPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "SongPage.hxx"
#include "PageMeta.hxx"
#include "screen_find.hxx"
#include "Command.hxx"
#include "i18n.h"
#include "screen.hxx"
Expand Down Expand Up @@ -480,7 +479,7 @@ SongPage::OnCommand(struct mpdclient &c, Command cmd)
if (ListPage::OnCommand(c, cmd))
return true;

if (screen_find(screen, lw, cmd, *this)) {
if (screen.find_support.Find(lw, *this, cmd)) {
SchedulePaint();
return true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/TagListPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

#include "TagListPage.hxx"
#include "screen_status.hxx"
#include "screen_find.hxx"
#include "FileListPage.hxx"
#include "Command.hxx"
#include "i18n.h"
#include "charset.hxx"
#include "mpdclient.hxx"
#include "page/FindSupport.hxx"
#include "util/StringUTF8.hxx"

#include <algorithm>
Expand Down Expand Up @@ -240,12 +240,12 @@ TagListPage::OnCommand(struct mpdclient &c, Command cmd)
case Command::LIST_RFIND:
case Command::LIST_FIND_NEXT:
case Command::LIST_RFIND_NEXT:
screen_find(screen, lw, cmd, *this);
find_support.Find(lw, *this, cmd);
SchedulePaint();
return true;

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

Expand Down
10 changes: 6 additions & 4 deletions src/TagListPage.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
#include <vector>
#include <string>

class ScreenManager;
class FindSupport;

class TagListPage : public ListPage, ListRenderer, ListText {
ScreenManager &screen;
FindSupport &find_support;
Page *const parent;

const enum mpd_tag_type tag;
Expand All @@ -26,11 +26,13 @@ class TagListPage : public ListPage, ListRenderer, ListText {
std::vector<std::string> values;

public:
TagListPage(PageContainer &_container, ScreenManager &_screen, Page *_parent,
TagListPage(PageContainer &_container,
FindSupport &_find_support, Page *_parent,
const enum mpd_tag_type _tag,
const char *_all_text,
Window _window, Size size) noexcept
:ListPage(_container, _window, size), screen(_screen), parent(_parent),
:ListPage(_container, _window, size),
find_support(_find_support), parent(_parent),
tag(_tag), all_text(_all_text) {}

auto GetTag() const noexcept {
Expand Down
39 changes: 18 additions & 21 deletions src/screen_find.cxx → src/page/FindSupport.cxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#include "screen_find.hxx"
#include "FindSupport.hxx"
#include "screen_utils.hxx"
#include "screen_status.hxx"
#include "screen.hxx"
Expand All @@ -19,10 +19,8 @@
#define RFIND_PROMPT _("Find backward")
#define JUMP_PROMPT _("Jump")

/* query user for a string and find it in a list window */
bool
screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd,
const ListText &text) noexcept
FindSupport::Find(ListWindow &lw, const ListText &text, Command findcmd) noexcept
{
bool found;
const char *prompt = FIND_PROMPT;
Expand All @@ -36,29 +34,30 @@ screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd,
switch (findcmd) {
case Command::LIST_FIND:
case Command::LIST_RFIND:
screen.findbuf.clear();
last.clear();
/* fall through */

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

if (screen.findbuf.empty())
if (last.empty())
return true;

found = reversed
? lw.ReverseFind(text, screen.findbuf)
: lw.Find(text, screen.findbuf);
? lw.ReverseFind(text, last)
: lw.Find(text, last);
if (!found) {
screen_status_printf(_("Unable to find \'%s\'"),
screen.findbuf.c_str());
last.c_str());
Bell();
}
return true;
Expand All @@ -68,19 +67,17 @@ screen_find(ScreenManager &screen, ListWindow &lw, Command findcmd,
return false;
}

/* query user for a string and jump to the entry
* which begins with this string while the users types */
void
screen_jump(ScreenManager &screen, ListWindow &lw,
const ListText &text,
const ListRenderer &renderer) noexcept
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];

/* In screen.findbuf is the whole string which is displayed in the status_window
/* 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;
Expand Down Expand Up @@ -110,7 +107,7 @@ screen_jump(ScreenManager &screen, ListWindow &lw,
lw.Refresh();
}

screen.findbuf = search_str;
last = search_str;

/* ncmpc should get the command */
keyboard_unread(key);
Expand Down
40 changes: 40 additions & 0 deletions src/page/FindSupport.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright The Music Player Daemon Project

#pragma once

#include "History.hxx"

#include <string>

enum class Command : unsigned;
class ScreenManager;
class ListWindow;
class ListRenderer;
class ListText;

class FindSupport {
ScreenManager &screen;

std::string last;
History history;

public:
explicit FindSupport(ScreenManager &_screen) noexcept
:screen(_screen) {}

/**
* query user for a string and find it in a list window
*
* @param lw the list window to search
* @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
*/
bool 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;
};
10 changes: 5 additions & 5 deletions src/page/TextPage.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright The Music Player Daemon Project

#include "TextPage.hxx"
#include "screen_find.hxx"
#include "FindSupport.hxx"
#include "charset.hxx"
#include "ui/TextListRenderer.hxx"
#include "screen.hxx"
Expand All @@ -12,9 +12,9 @@
#include <assert.h>
#include <string.h>

TextPage::TextPage(ScreenManager &_screen,
Window window, Size size) noexcept
:ListPage(_screen, window, size), screen(_screen)
TextPage::TextPage(PageContainer &_parent, Window window, Size size,
FindSupport &_find_support) noexcept
:ListPage(_parent, window, size), find_support(_find_support)
{
lw.HideCursor();
}
Expand Down Expand Up @@ -90,7 +90,7 @@ TextPage::OnCommand(struct mpdclient &c, Command cmd)
search */
lw.SetCursorFromOrigin(0);

if (screen_find(screen, lw, cmd, *this)) {
if (find_support.Find(lw, *this, cmd)) {
SchedulePaint();
return true;
}
Expand Down
Loading

0 comments on commit 22a3edb

Please sign in to comment.