LCOV - code coverage report
Current view: top level - cltools - Completion.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 10 30 33.3 %
Date: 2025-12-04 11:19:34 Functions: 6 7 85.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2018-2023 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 "core/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             : 
      31             : namespace PLMD {
      32             : namespace cltools {
      33             : 
      34             : //+PLUMEDOC TOOLS completion
      35             : /*
      36             : Dumps the body of a bash function to be used for auto completion.
      37             : 
      38             : You typically will not need to use this command it is involved during complation and is
      39             : used to construct everything that is required for the autocompletion functionality that
      40             : is described [here](module_cltools.md).
      41             : 
      42             : ## Examples
      43             : 
      44             : ```plumed
      45             : plumed completion
      46             : ```
      47             : 
      48             : 
      49             : */
      50             : //+ENDPLUMEDOC
      51             : 
      52             : class Completion:
      53             :   public CLTool {
      54             : public:
      55             :   static void registerKeywords( Keywords& keys );
      56             :   explicit Completion(const CLToolOptions& co );
      57             :   int main(FILE* in, FILE*out,Communicator& pc) override;
      58           5 :   std::string description()const override {
      59           5 :     return "dump a function usable for programmable completion";
      60             :   }
      61             : };
      62             : 
      63       17054 : PLUMED_REGISTER_CLTOOL(Completion,"completion")
      64             : 
      65        5683 : void Completion::registerKeywords( Keywords& keys ) {
      66        5683 :   CLTool::registerKeywords( keys );
      67        5683 : }
      68             : 
      69           5 : Completion::Completion(const CLToolOptions& co ):
      70           5 :   CLTool(co) {
      71           5 :   inputdata=inputType::commandline;
      72           5 : }
      73             : 
      74           0 : int Completion::main(FILE* in, FILE*out,Communicator& pc) {
      75             :   static const char completion [] = {
      76             : #include "completion.xxd"
      77             : // cppcheck-suppress syntaxError
      78             :     , 0x00
      79             :   };
      80             :   std::fprintf(out,"local cmds=\"help -h --help");
      81             : // Build list of available C++ tools:
      82           0 :   std::vector<std::string> availableCxx=cltoolRegister().list();
      83             : // Build list of available shell tools:
      84           0 :   std::vector<std::string> tmp=Tools::ls(std::string(config::getPlumedRoot()+"/scripts"));
      85           0 :   for(unsigned j=0; j<tmp.size(); ++j) {
      86           0 :     size_t ff=tmp[j].find(".sh");
      87           0 :     if(ff==std::string::npos) {
      88           0 :       tmp[j].erase();
      89             :     } else {
      90           0 :       tmp[j].erase(ff);
      91             :     }
      92             :   }
      93           0 :   for(unsigned j=0; j<availableCxx.size(); j++) {
      94             :     std::fprintf(out," %s",availableCxx[j].c_str());
      95             :   }
      96           0 :   for(unsigned j=0; j<tmp.size(); ++j)
      97           0 :     if(tmp[j].length()>0) {
      98             :       std::fprintf(out," %s",tmp[j].c_str());
      99             :     }
     100             :   std::fprintf(out,"\"\n");
     101             : 
     102             :   //here I am looping on the copies,
     103             :   //since I want to preprocess the string before printing it
     104           0 :   for(std::string cmd:availableCxx) {
     105           0 :     std::vector<std::string> keys=cltoolRegister().getKeys(cmd);
     106             : // handle - sign (convert to underscore)
     107           0 :     std::replace(cmd.begin(),cmd.end(),'-','_');
     108             :     std::fprintf(out,"local cmd_keys_%s=\"",cmd.c_str());
     109           0 :     for(std::string key: keys) {
     110             : // handle --help/-h
     111           0 :       std::replace(key.begin(),key.end(),'/',' ');
     112             :       std::fprintf(out," %s",key.c_str());
     113             :     }
     114             :     std::fprintf(out,"\"\n");
     115           0 :   }
     116             : 
     117             :   std::fprintf(out,"%s\n",completion);
     118           0 :   std::string plumedName=config::getPlumedProgramName();
     119             : 
     120             :   std::fprintf(out,
     121             :                "############################################\n"
     122             :                "## ADD THESE COMMANDS TO YOUR .bashrc FILE:\n"
     123             :                "############################################\n"
     124             :                "# _%s() { eval \"$(%s --no-mpi completion 2>/dev/null)\";}\n"
     125             :                "# complete -F _%s -o default %s\n"
     126             :                "############################################\n",
     127             :                plumedName.c_str(),
     128             :                plumedName.c_str(),
     129             :                plumedName.c_str(),
     130             :                plumedName.c_str());
     131             : 
     132           0 :   return 0;
     133           0 : }
     134             : 
     135             : } // End of namespace
     136             : }

Generated by: LCOV version 1.16