From 7167ce41b61d2ba2cdb526777a4233eb84a3b66a Mon Sep 17 00:00:00 2001 From: Unit 193 Date: Sat, 6 Dec 2014 17:33:25 -0500 Subject: Imported Upstream version 2.99.6 --- SQLiteStudio3/sqlitestudiocli/cliutils.cpp | 99 ++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 SQLiteStudio3/sqlitestudiocli/cliutils.cpp (limited to 'SQLiteStudio3/sqlitestudiocli/cliutils.cpp') diff --git a/SQLiteStudio3/sqlitestudiocli/cliutils.cpp b/SQLiteStudio3/sqlitestudiocli/cliutils.cpp new file mode 100644 index 0000000..2b5c44d --- /dev/null +++ b/SQLiteStudio3/sqlitestudiocli/cliutils.cpp @@ -0,0 +1,99 @@ +#include "cliutils.h" +#include + +#if defined(Q_OS_WIN32) +#include +#elif defined(Q_OS_UNIX) +#include +#include +#endif + +#if defined(Q_OS_WIN32) + +int getCliColumns() +{ + CONSOLE_SCREEN_BUFFER_INFO data; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &data); + return data.dwSize.X; +} + +int getCliRows() +{ + CONSOLE_SCREEN_BUFFER_INFO data; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &data); + return data.dwSize.Y; +} + +#elif defined(Q_OS_UNIX) + +int getCliColumns() +{ + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + return w.ws_col; +} + +int getCliRows() +{ + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + return w.ws_row; +} + +#endif + +QStringList toAsciiTree(const AsciiTree& tree, const QList& indents, bool topLevel, bool lastNode) +{ + static const QString indentStr = " | "; + static const QString indentStrEmpty = " "; + static const QString branchStr = " +-"; + static const QString branchStrLast = " `-"; + + QStringList lines; + QString line; + + if (!topLevel) + { + // Draw indent before this node + foreach (bool indent, indents) + line += (indent ? indentStr : indentStrEmpty); + + // Draw node prefix + line += (lastNode ? branchStrLast : branchStr); + } + + // Draw label + line += tree.label; + lines << line; + + if (tree.childs.size() == 0) + return lines; + + // Draw childs + int i = 0; + int lastIdx = tree.childs.size() - 1; + QList subIndents = indents; + + if (!topLevel) + subIndents << (lastNode ? false : true); + + foreach (const AsciiTree& subTree, tree.childs) + { + lines += toAsciiTree(subTree, subIndents, false, i == lastIdx); + i++; + } + + return lines; +} + +QString toAsciiTree(const AsciiTree& tree) +{ + QList subIndents; + QStringList lines = toAsciiTree(tree, subIndents, true, true); + return lines.join("\n"); +} + +void initCliUtils() +{ + qRegisterMetaType(); +} -- cgit v1.2.3