| 1 |
/* |
| 2 |
* This file is part of the apvlv package |
| 3 |
* Copyright (C) <2008> Alf |
| 4 |
* |
| 5 |
* Contact: Alf <[email protected]> |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License along |
| 18 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 19 |
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 |
* |
| 21 |
*/ |
| 22 |
/* @CPPFILE ApvlvCompletion.cc |
| 23 |
* |
| 24 |
* Author: Alf <[email protected]> |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <QDebug> |
| 28 |
#include <filesystem> |
| 29 |
#include <string> |
| 30 |
|
| 31 |
#include "ApvlvCompletion.h" |
| 32 |
#include "ApvlvUtil.h" |
| 33 |
|
| 34 |
namespace apvlv |
| 35 |
{ |
| 36 |
using namespace std; |
| 37 |
|
| 38 |
string |
| 39 |
ApvlvCompletion::complete (const string &prefix) |
| 40 |
{ |
| 41 |
auto iter = std::ranges::find_if (mItems, [prefix] (const string &item) { |
| 42 |
return item.find (prefix) == 0; |
| 43 |
}); |
| 44 |
if (iter != mItems.cend ()) |
| 45 |
return *iter; |
| 46 |
else |
| 47 |
return ""; |
| 48 |
} |
| 49 |
|
| 50 |
void |
| 51 |
ApvlvCompletion::addItems (const vector<string> &items) |
| 52 |
{ |
| 53 |
mItems.insert (mItems.end (), items.begin (), items.end ()); |
| 54 |
} |
| 55 |
|
| 56 |
void |
| 57 |
ApvlvCompletion::addPath (const string &path) |
| 58 |
{ |
| 59 |
vector<string> items; |
| 60 |
|
| 61 |
auto fspath = filesystem::path{ path }; |
| 62 |
auto filename = fspath.filename (); |
| 63 |
auto dirname = fspath.parent_path (); |
| 64 |
for (auto &entry : filesystem::directory_iterator (dirname)) |
| 65 |
{ |
| 66 |
auto entry_filename = entry.path ().filename ().string (); |
| 67 |
if (filename.empty () || entry_filename.find (filename.string ()) == 0) |
| 68 |
{ |
| 69 |
auto item = entry.path ().string () |
| 70 |
+ (entry.is_directory () ? PATH_SEP_S : ""); |
| 71 |
qDebug () << "add a item: " << item; |
| 72 |
items.emplace_back (item); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
addItems (items); |
| 77 |
} |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
// Local Variables: |
| 82 |
// mode: c++ |
| 83 |
// End: |