From 20f1400a11837005f0904f2c7cdb40dcd4d5b669 Mon Sep 17 00:00:00 2001 From: Katja Marttila Date: Fri, 8 May 2020 08:42:53 +0300 Subject: CLI: Add option to read arbitrary user input Added new readConsoleLine() function which can be used to read user input from console. Function can be only called when command line instance is used without gui. Task-number: QTIFW-1631 Change-Id: Ic856f4bafc4a8d469d3fe497b4e0321e104df499 Reviewed-by: Tarja Sundqvist Reviewed-by: Arttu Tarkiainen --- doc/scripting-api/packagemanagercore.qdoc | 14 ++++++++++++++ src/libs/installer/packagemanagercore.cpp | 23 +++++++++++++++++++++++ src/libs/installer/packagemanagercore.h | 1 + 3 files changed, 38 insertions(+) diff --git a/doc/scripting-api/packagemanagercore.qdoc b/doc/scripting-api/packagemanagercore.qdoc index 50666d3be..ac6cad2ce 100644 --- a/doc/scripting-api/packagemanagercore.qdoc +++ b/doc/scripting-api/packagemanagercore.qdoc @@ -357,6 +357,20 @@ empty string is returned. */ +/*! + \qmlmethod string installer::readConsoleLine(string title, int maxlen) + + Prints \a title to a console and reads console input. Function halts the + installer and waits for user input. Function returns a line which user has + typed into the console. The maximum allowed line length is set to + \a maxlen. If the stream contains lines longer than this, then the line is + split after \a maxlen characters. If \a maxlen is 0, the line can be of + any length. + + \note This function can only be called when installing from a command line + instance without GUI. +*/ + /*! \qmlmethod boolean installer::fileExists(string filePath) diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index 14d7114f6..bb754f568 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -877,6 +877,29 @@ QString PackageManagerCore::readFile(const QString &filePath, const QString &cod return stream.readAll(); } +/*! + * Prints \a title to console and reads console input. Function will halt the + * installer and wait for user input. Returns a line which user has typed into + * console. The maximum allowed line length is set to \a maxlen. If the stream + * contains lines longer than this, then the line will be split after maxlen + * characters. If \a maxlen is 0, the line can be of any length. + * + * \note Can be only called when installing from command line instance without GUI. + * + * \sa {installer::readConsoleLine}{installer.readConsoleLine} + */ +QString PackageManagerCore::readConsoleLine(const QString &title, qint64 maxlen) const +{ + if (!isCommandLineInstance()) + return QString(); + if (!title.isEmpty()) + qDebug() << title; + QTextStream stream(stdin); + QString input; + stream.readLineInto(&input, maxlen); + return input; +} + /*! Checks whether the target directory \a targetDirectory exists and has contents: \list diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index dceaa4fef..9e24b5c0a 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -190,6 +190,7 @@ public: Q_INVOKABLE bool isFileExtensionRegistered(const QString &extension) const; Q_INVOKABLE bool fileExists(const QString &filePath) const; Q_INVOKABLE QString readFile(const QString &filePath, const QString &codecName) const; + Q_INVOKABLE QString readConsoleLine(const QString &title = QString(), qint64 maxlen = 0) const; bool checkTargetDir(const QString &targetDirectory); QString targetDirWarning(const QString &targetDirectory) const; -- cgit v1.2.3