Skip to content

Commit

Permalink
Merge branch 'only_save_successful_commands_in_history' into 'master'
Browse files Browse the repository at this point in the history
Only save successful commands in the history file

See merge request npneq/inq!1091
  • Loading branch information
xavierandrade committed Jun 29, 2024
2 parents 7d91fd9 + 90b16fb commit c6fd8e3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 47 deletions.
2 changes: 2 additions & 0 deletions src/interface/actions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#include <inq_config.h>
#include <interface/history_file.hpp>

namespace inq {
namespace interface {
namespace actions {

void normal_exit() {
history_file.write();
exit(0);
}

Expand Down
68 changes: 34 additions & 34 deletions src/interface/history.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,25 @@ These are the available subcommands:
- `history`
Prints a full list of the inq commands that have been executed in the
current directory.
Prints a full list of the inq commands that have been executed in the
current directory.
Shell example: `inq history`
Shell example: `inq history`
- `history clear`
Clears the list of saved commands.
Clears the list of saved commands.
Shell example: `inq history clear`
Shell example: `inq history clear`
- `history script`
Prints a bash script containing the list of commands that reproduce
the calculation in the current directory. The recommended script
headers are included. Any command issues before `inq clear` will not
be included.
Prints a bash script containing the list of commands that reproduce
the calculation in the current directory. The recommended script
headers are included. Any command issues before `inq clear` will not
be included.
Shell example: `inq history script > my_calculation.sh`
Shell example: `inq history script > my_calculation.sh`
)"""";
Expand All @@ -63,50 +63,50 @@ These are the available subcommands:
template <typename ArgsType>
void command(ArgsType const & args, bool quiet) const {

if(args.size() == 0) {
std::ifstream hist_file(".inq_history");
if(hist_file.is_open()) std::cout << hist_file.rdbuf();
actions::normal_exit();
}
if(args.size() == 0) {
std::ifstream hist_file(".inq_history");
if(hist_file.is_open()) std::cout << hist_file.rdbuf();
actions::normal_exit();
}

if(args.size() == 1 and args[0] == "clear") {
std::ofstream hist_file(".inq_history");
std::ofstream hist_file(".inq_history");
actions::normal_exit();
}

if(args.size() == 1 and args[0] == "script") {
if(args.size() == 1 and args[0] == "script") {

auto starts_with = [](auto str, auto substr) {
return utils::lowercase(str).rfind(substr, 0) == 0;
};

std::cout << "#!/bin/bash\n\n"
<< "set -e #make the script fail if a command fails\n"
<< "set -x #output commands to the terminal\n\n"
<< "inq clear\n";
std::cout << "#!/bin/bash\n\n"
<< "set -e #make the script fail if a command fails\n"
<< "set -x #output commands to the terminal\n\n"
<< "inq clear\n";

std::ifstream hist_file(".inq_history");
std::ifstream hist_file(".inq_history");

std::list<std::string> file_lines;
while(hist_file) {
std::string line;
std::getline(hist_file, line);
std::list<std::string> file_lines;
while(hist_file) {
std::string line;
std::getline(hist_file, line);

if(starts_with(line, "inq history")) continue;
if(starts_with(line, "inq history")) continue;

file_lines.push_back(line);
}
file_lines.push_back(line);
}

auto rit = file_lines.rbegin();
for(; rit != file_lines.rend(); ++rit){
if(starts_with(*rit, "inq clear") or starts_with(*rit, "inq clean")) break;
}

for(auto it = rit.base(); it != file_lines.end(); ++it) std::cout << *it << '\n';
actions::normal_exit();
}
for(auto it = rit.base(); it != file_lines.end(); ++it) std::cout << *it << '\n';
actions::normal_exit();
}
actions::error(input::environment::global().comm(), "Invalid syntax in the 'history' command");
}

Expand Down
58 changes: 58 additions & 0 deletions src/interface/history_file.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* -*- indent-tabs-mode: t -*- */

#ifndef INQ__INTERFACE__HISTORY_FILE
#define INQ__INTERFACE__HISTORY_FILE

// Copyright (C) 2019-2024 Lawrence Livermore National Security, LLC., Xavier Andrade, Alfredo A. Correa
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

namespace inq {
namespace interface {

struct {

std::string entries_;

public:

void add_entry(int argc, char ** argv) {

for(int iarg = 0; iarg < argc; iarg++) {
auto arg = std::string(argv[iarg]);
auto & npos = std::string::npos;
if(arg.find(' ') != npos or arg.find('(') != npos or arg.find(')') != npos){
arg = '\"' + arg + '\"';
}
if(iarg > 0) entries_ += ' ';
entries_ += arg;
}
entries_ += '\n';
}

void write() const {
auto history_file = std::ofstream(".inq_history", std::ofstream::app);
history_file << entries_;
}

} history_file;

}
}

#endif

#ifdef INQ_INTERFACE_HISTORY_FILE_UNIT_TEST
#undef INQ_INTERFACE_HISTORY_FILE_UNIT_TEST

#include <catch2/catch_all.hpp>

TEST_CASE(INQ_TEST_FILE, INQ_TEST_TAG) {

using namespace inq;
using namespace Catch::literals;

}
#endif
14 changes: 1 addition & 13 deletions src/main/inq.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,7 @@ int main(int argc, char* argv[]) {
interface::actions::normal_exit();
}

{
auto history_file = std::ofstream(".inq_history", std::ofstream::app);
for(int iarg = 0; iarg < argc; iarg++) {
auto arg = std::string(argv[iarg]);
auto & npos = std::string::npos;
if(arg.find(' ') != npos or arg.find('(') != npos or arg.find(')') != npos){
arg = '\"' + arg + '\"';
}
if(iarg > 0) history_file << ' ';
history_file << arg;
}
history_file << std::endl;
}
interface::history_file.add_entry(argc, argv);

auto quiet = false;
auto debug = false;
Expand Down

0 comments on commit c6fd8e3

Please sign in to comment.