Skip to content

Cleanup line reader code for CLI#13330

Open
aollier wants to merge 3 commits into
keepassxreboot:developfrom
aollier:refactor/lineReader
Open

Cleanup line reader code for CLI#13330
aollier wants to merge 3 commits into
keepassxreboot:developfrom
aollier:refactor/lineReader

Conversation

@aollier

@aollier aollier commented May 9, 2026

Copy link
Copy Markdown
Contributor

These changes make the LineReader class a concrete class whose implementation depends on whether or not USE_READLINE is defined. This greatly simplifies keepassxc-cli.cpp.
The readline library's search and internal and public variables now conform to the CMake standard (https://cmake.org/cmake/help/latest/manual/cmake-developer.7.html).

If the readline library is not found, the LineReader class then depends on the TextStream class. The cli library also depends on the TextStream class, but only for its implementation (Export.cpp).
These relationships are now expressed between the line_reader, text_stream, and cli libraries.
Also, by putting the code of LineReader in the line_reader library, we isolate the use of USE_READLINE into the single line_reader library. The cli library does not use anymore the USE_READLINE definition.

Finally, the new version of the search modules allows the use of CMake's modern library dependency syntax.

The FindReadline.cmake module was written with the help of Le Chat (https://chat.mistral.ai/chat).

Type of change

  • ✅ Refactor (significant modification to existing code)

@aollier
aollier force-pushed the refactor/lineReader branch 2 times, most recently from ab7924c to f2a3033 Compare May 9, 2026 20:53
@varjolintu varjolintu added the pr: ai-assisted Pull request contains significant contributions by generative AI label May 12, 2026
@varjolintu

Copy link
Copy Markdown
Member

Is the NCurses dependency really needed for this? I don't really get the idea behind this whole change.

@aollier

aollier commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

Hello @varjolintu and thank you for reviewing my pull request.

Your question touches on two points:

  1. According to https://vcpkg.io/en/package/readline and https://vcpkg.io/en/package/readline-unix.html, the readline library depends on ncurses on Unix only. That's why I introduced the FindNcurses module. Traces of this dependency can be seen in the current code with the Ncurses_LIBRARY variable:
    if(Readline_INCLUDE_DIR AND Readline_LIBRARY AND Ncurses_LIBRARY)
    set(READLINE_FOUND TRUE)
  2. The idea behind this whole change is the simplification of src/cli/keepassxc-cli.cpp and the elimination of unnecessary classes SimpleLineReader and ReadlineLineReader. This also sets the USE_READLINE definition only where it is necessary: for the LineReader class only, which is now isolated. So the cli library does not carry the USE_READLINE definition anymore. By introducing two new libraries, these changes also clarify the dependency of the code: the keepassxc_core library does not require TextStream.cpp. cli library does with Export.cpp, and LineReader may depend on it if the readline library dependency is not found.

@aollier

aollier commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

I've just realized that it is not necessary to introduce the FindNcurses module because it already exists one in the CMake modules library: https://cmake.org/cmake/help/latest/module/FindCurses.html.

On the other hand, I admit that I have not found any trace of the readline library's dependency on the ncurses library anywhere other than on vcpkg and the Ncurses_LIBRARY variable. What is your experience with this?

@aollier

aollier commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

I finally found the repository of the readline library: git://git.git.savannah.gnu.org/readline.git.
In the INSTALL file, I can read this:

`--with-curses'
This tells readline that it can find the termcap library functions
(tgetent, et al.) in the curses library, rather than a separate
termcap library. Readline uses the termcap functions, but does not
usually link with the termcap or curses library itself, allowing
applications which link with readline the to choose an appropriate
library. This option tells readline to link the example programs with
the curses library rather than libtermcap.

This is an option passed to the configure script. So it seems that the (n)curses library may not be mandatory, if the --with-curses option is NOT passed to the configure script.

So what is your recommandation with this change? Should I let the FindReadline module unchanged even if on the vcpkg website we can see that the readline library requires the ncurses library?

@droidmonkey

droidmonkey commented May 13, 2026

Copy link
Copy Markdown
Member

We do not use vcpkg on linux. If ncurses is a dependency of readline on macOS then finding the readline library would be sufficient as that should also bring in the ncurses library as needed. If we dont directly utilize a library than we shouldn't need to "find" it and link to it directly.

@aollier
aollier force-pushed the refactor/lineReader branch 2 times, most recently from e8e9876 to 4b8c0fb Compare May 13, 2026 18:20
@aollier

aollier commented May 13, 2026

Copy link
Copy Markdown
Contributor Author

Ok I've updated my change by removing the FindNcurses module and every reference to the ncurses library.
I think it is correct now.

@aollier
aollier force-pushed the refactor/lineReader branch from 4b8c0fb to 522cfe6 Compare May 13, 2026 19:09
@aollier
aollier force-pushed the refactor/lineReader branch from 522cfe6 to 39cb356 Compare July 2, 2026 11:29
@droidmonkey

Copy link
Copy Markdown
Member

@aollier sorry for the delay, will review this today

@aollier

aollier commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Thank you @droidmonkey for reviewing my pull request. 😃

@aollier

aollier commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

@droidmonkey I've just found PR #8918 which seemed to be a good replacement for readline and that you found "outstanding" but this PR seems to have been forgotten. Would you like me to use linenoise instead of readline in this pull request?

@aollier

aollier commented Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

The healthiest and most rigorous approach would be to switch libraries in a separate pull request. This one redefines the architecture. Switching dependencies will be very easy afterwards.

aollier added 2 commits July 12, 2026 18:27
…ler way

The LineReader is made a concrete class whose declaration
is independent of the readline library.
Only its implementation depends on the presence or absence
of the readline library.
The LineReader code is embedded in a new library : line_reader,
to make the cli library independant of the USE_READLINE definition.
The readline library is now searched with pkg-config first.
If pkg-config is not installed, the search is manual.
In both cases, the Readline::readline target is created,
embedding transitive requirements.

FindReadline module was written with the help of Le Chat AI (https://chat.mistral.ai/chat)
@aollier
aollier force-pushed the refactor/lineReader branch from 39cb356 to 9c2a9e6 Compare July 12, 2026 16:30
@droidmonkey droidmonkey added this to the v2.8.0 milestone Jul 21, 2026
@droidmonkey droidmonkey changed the title Refactor/line reader Cleanup line reader code for CLI Jul 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cleanup feature: CLI pr: ai-assisted Pull request contains significant contributions by generative AI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants