LCOV - code coverage report
Current view: top level - cltools - Completion.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 5 31 16.1 %
Date: 2021-11-18 15:22:58 Functions: 5 11 45.5 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2018-2020 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    plumed is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : #include "CLTool.h"
      23             : #include "CLToolRegister.h"
      24             : #include "tools/Tools.h"
      25             : #include "config/Config.h"
      26             : #include "core/ActionRegister.h"
      27             : #include <cstdio>
      28             : #include <string>
      29             : #include <vector>
      30             : #include <iostream>
      31             : 
      32             : using namespace std;
      33             : 
      34             : namespace PLMD {
      35             : namespace cltools {
      36             : 
      37             : //+PLUMEDOC TOOLS completion
      38             : /*
      39             : Dumps the body of a bash function to be used for auto completion.
      40             : 
      41             : Users will typically not need this command.
      42             : See more at \ref BashAutocompletion
      43             : 
      44             : \par Examples
      45             : 
      46             : \verbatim
      47             : plumed completion
      48             : \endverbatim
      49             : 
      50             : 
      51             : */
      52             : //+ENDPLUMEDOC
      53             : 
      54           0 : class Completion:
      55             :   public CLTool
      56             : {
      57             : public:
      58             :   static void registerKeywords( Keywords& keys );
      59             :   explicit Completion(const CLToolOptions& co );
      60             :   int main(FILE* in, FILE*out,Communicator& pc);
      61           0 :   string description()const {
      62           0 :     return "dump a function usable for programmable completion";
      63             :   }
      64             : };
      65             : 
      66        7356 : PLUMED_REGISTER_CLTOOL(Completion,"completion")
      67             : 
      68        1839 : void Completion::registerKeywords( Keywords& keys ) {
      69        1839 :   CLTool::registerKeywords( keys );
      70        1839 : }
      71             : 
      72           0 : Completion::Completion(const CLToolOptions& co ):
      73           0 :   CLTool(co)
      74             : {
      75           0 :   inputdata=commandline;
      76           0 : }
      77             : 
      78           0 : int Completion::main(FILE* in, FILE*out,Communicator& pc) {
      79             :   static const char completion [] = {
      80             : #include "completion.xxd"
      81             :     , 0x00
      82             :   };
      83             :   fprintf(out,"local cmds=\"help -h --help");
      84             : // Build list of available C++ tools:
      85           0 :   std::vector<string> availableCxx=cltoolRegister().list();
      86             : // Build list of available shell tools:
      87           0 :   vector<string> tmp=Tools::ls(string(config::getPlumedRoot()+"/scripts"));
      88           0 :   for(unsigned j=0; j<tmp.size(); ++j) {
      89             :     size_t ff=tmp[j].find(".sh");
      90           0 :     if(ff==string::npos) tmp[j].erase();
      91           0 :     else                 tmp[j].erase(ff);
      92             :   }
      93           0 :   for(unsigned j=0; j<availableCxx.size(); j++) fprintf(out," %s",availableCxx[j].c_str());
      94           0 :   for(unsigned j=0; j<tmp.size(); ++j) if(tmp[j].length()>0) fprintf(out," %s",tmp[j].c_str());
      95             :   fprintf(out,"\"\n");
      96             : 
      97           0 :   for(unsigned j=0; j<availableCxx.size(); j++) {
      98             :     std::string s=availableCxx[j];
      99             : // handle - sign (convert to underscore)
     100             :     for(;;) {
     101             :       size_t n=s.find("-");
     102           0 :       if(n==std::string::npos) break;
     103           0 :       s[n]='_';
     104           0 :     }
     105             :     fprintf(out,"local cmd_keys_%s=\"",s.c_str());
     106           0 :     std::vector<std::string> keys=cltoolRegister().getKeys(availableCxx[j]);
     107           0 :     for(unsigned k=0; k<keys.size(); k++) {
     108             : // handle --help/-h
     109             :       std::string s=keys[k];
     110             :       for(;;) {
     111             :         size_t n=s.find("/");
     112           0 :         if(n==std::string::npos) break;
     113           0 :         s[n]=' ';
     114           0 :       }
     115             :       fprintf(out," %s",s.c_str());
     116             :     }
     117             :     fprintf(out,"\"\n");
     118             :   }
     119             : 
     120             : ////  ALTERNATIVE IMPLEMENTATION
     121             : ////  checking tools on the fly
     122             : ////     for(unsigned j=0; j<tmp.size(); j++) {
     123             : ////       std::string s=tmp[j];
     124             : ////   // handle - sign (convert to underscore)
     125             : ////       for(;;) {
     126             : ////         size_t n=s.find("-");
     127             : ////         if(n==std::string::npos) break;
     128             : ////         s[n]='_';
     129             : ////       }
     130             : ////       fprintf(out,"local cmd_keys_%s=\"",s.c_str());
     131             : ////       std::string cmd=config::getEnvCommand()+" \""+config::getPlumedRoot()+"/scripts/"+s+".sh\" --options";
     132             : ////       FILE *fp=popen(cmd.c_str(),"r");
     133             : ////       std::string line,manual;
     134             : ////       while(Tools::getline(fp,line))manual+=line;
     135             : ////       pclose(fp);
     136             : ////       std::vector<std::string> keys=Tools::getWords(manual);
     137             : ////       for(unsigned k=0; k<keys.size(); k++) {
     138             : ////   // handle --help/-h
     139             : ////         std::string s=keys[k];
     140             : ////         for(;;) {
     141             : ////           size_t n=s.find("/");
     142             : ////           if(n==std::string::npos) break;
     143             : ////           s[n]=' ';
     144             : ////         }
     145             : ////         fprintf(out," %s",s.c_str());
     146             : ////       }
     147             : ////       fprintf(out,"\"\n");
     148             : ////     }
     149             : 
     150             :   fprintf(out,"%s\n",completion);
     151           0 :   std::string name=config::getPlumedProgramName();
     152             : 
     153             :   fprintf(out,
     154             :           "############################################\n"
     155             :           "## ADD THESE COMMANDS TO YOUR .bashrc FILE:\n"
     156             :           "############################################\n"
     157             :           "# _%s() { eval \"$(%s --no-mpi completion 2>/dev/null)\";}\n"
     158             :           "# complete -F _%s -o default %s\n"
     159             :           "############################################\n",
     160             :           name.c_str(),name.c_str(),name.c_str(),name.c_str());
     161             : 
     162           0 :   return 0;
     163             : }
     164             : 
     165             : } // End of namespace
     166        5517 : }

Generated by: LCOV version 1.14