aboutsummaryrefslogtreecommitdiffstats
path: root/bruiser/CompletionHints.cpp
blob: 43f4345b3d58a4eef2786f011516624235614060 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/***************************************************Project Mutator****************************************************/
//-*-c++-*-
/*first line intentionally left blank.*/
/*the source code for bruiser's auto-completion and suggestions.*/
/*Copyright (C) 2017 Farzad Sadeghi

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.*/
/**********************************************************************************************************************/
/*included modules*/
#include "bruiser-extra.h"
/*project headers*/
/*standard headers*/
#include <iostream>
#include <string>
/*LLVM headers*/
/*other*/
#include "linenoise/linenoise.h"
/**********************************************************************************************************************/
/*used namespaces*/
/**********************************************************************************************************************/
/*macros*/

/**********************************************************************************************************************/
namespace bruiser
{
  void ShellCompletion(const char* __buf, linenoiseCompletions* __lc)
  {
    if (__buf != NULL)
    {
      for(auto &iter : LUA_FUNCS)
      {
        if (iter.find(__buf) == 0U)
        {
          linenoiseAddCompletion(__lc, iter.c_str());
        }
      }
    }
  }

  char* ShellHints(const char* __buf, int* __color, int* __bold)
  {
    if (__buf != NULL)
    {
      auto dummy = std::string(__buf);

      for(auto &iter : LUA_FUNCS)
      {
        if (dummy == "")
        {
          break;
        }

        if (iter.find(__buf) == 0U)
        {
          *__color = 35;
          *__bold = 1;
          int sizet = dummy.length();

          std::string dummy2 = iter.substr(sizet, std::string::npos);

          /*@DEVI-apparently linenoise expects the return value to live past the hints callback function returning, 
           * i mean that's why our vector version returns junk. linenoise later frees the returned hint so there 
           * should be no leaked memory(it calls freeHintsCallback).*/
          char* returnchar = new char[dummy2.size() + 1];
          std::copy(dummy2.begin(), dummy2.end(), returnchar);
          returnchar[dummy2.size()] = '\0';

#if 0
          std::vector<char> retchar(dummy2.begin(), dummy2.end());
          retchar.push_back('\0');
          //std::cout << "\n" << retchar.data() << "\n";
          char* c = (char*)retchar.data();
          std::cout << "\n" << c << "\n";
#endif

          return returnchar;
          //return c;
          //return retchar.data();
          //return &retchar[0];
        }
      }
    }

    return NULL;
  }
} //end of namespace bruiser
/**********************************************************************************************************************/
/*last line intentionally left blank*/