Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
persist search and commandline history.
Browse files Browse the repository at this point in the history
  • Loading branch information
akrennmair committed Feb 9, 2009
1 parent d2ff629 commit 0bb3daf
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -22,6 +22,7 @@ Changes for newsbeuter:
Added ability to configure local files as feeds.
Added a "random-unread" key to go to a random unread article.
When opening articles from a search result dialog, make search phrase stand out in article view.
Persist commandline and search history.

1.3 (2008-12-06):
Changed some internal data structures to smart pointers (stability improvement).
Expand Down
2 changes: 1 addition & 1 deletion TODO
Expand Up @@ -6,7 +6,6 @@ TODO:
- fix :source for bind-key: bind-key j down etc.
- implement internal alias mechanism to allow transition of renamed config commands.
- improve logging of which feeds are broken.
- persist search and command history to text files.
- make search phrase stand out in help (just like article view instead of limit) - configurable? combine both limit and standout?


Expand Down Expand Up @@ -146,3 +145,4 @@ DONE:
- add license to -V output
- rename -v to something else to avoid confusion
- make last search phrase stand out in article view.
- persist search and command history to text files.
7 changes: 6 additions & 1 deletion doc/chapter-cmdline.txt
Expand Up @@ -7,5 +7,10 @@ To start the commandline, type ":". You will see a ":" prompt at the bottom of
the screen, similar to tools like vi(m) or mutt. You can now enter commands.
Pressing the return key executes the command (possibly giving feedback to the
user) and closes the commandline. You can cancel entering commands by pressing
the ESC key. Currently, the following commands are available:
the ESC key. The history of all the commands that you enter will be saved to
\~/.newsbeuter/history.cmdline. The backlog is limited to 100 entries by default,
but can be influenced by setting the "history-limit" configuration variable.
To disable history saving, set the history-limit to 0.

Currently, the following commands are available:

9 changes: 8 additions & 1 deletion doc/chapter-firststeps.txt
Expand Up @@ -81,4 +81,11 @@ Searching for articles is possible in newsbeuter, too. Just press the "/" key,
enter your search phrase, and the title and content of all articles are
searched for it. When you do a search from the list of feeds, all articles of
all feeds will be searched. When you do a search from the article list of a
feed, only the articles of the currently viewed feed are searched.
feed, only the articles of the currently viewed feed are searched. When opening
an article from a search result dialog, the search phrase is highlighted.

The history of all your searches is saved to the filesystem, to
\~/.newsbeuter/history.search. By default, the last 100 search phrases are
stored, but this limited can be influenced through the "history-limit"
configuration variable. To disable search history saving, simply set the
history-limit to 0.
1 change: 1 addition & 0 deletions doc/configcommands.dsv
Expand Up @@ -23,6 +23,7 @@ feed-sort-order|<sortorder>|none|If set to "firsttag", the feeds in the feed lis
feedlist-format|<format>|"%4i %n %11u %t"|This variable defines the format of entries in the feed list. See below for more information on format strings.|feedlist-format " %n %4i - %11u -%> %t"
goto-next-feed|[yes/no]|yes|If set to yes, then the next-unread and prev-unread keys will search in other feeds for unread articles if all articles in the current feed are read. If set to no, then the next-unread and prev-unread keys will stop in the current feed.|goto-next-feed no
highlight|<target> <regex> <fgcolor> [<bgcolor> [<attribute> ...]]|n/a|With this command, you can highlight text parts in the feed list, the article list and the article view. For a detailed documentation, see the chapter on highlighting.|highlight all "newsbeuter" red
history-limit|<number>|100|Defines the maximum number of entries of commandline resp. search history to be saved. To disable history saving, set history-limit to 0.|history-limit 0
html-renderer|<path>|internal|If set to "internal", then the internal HTML renderer will be used. Otherwise, the specified command will be executed, the HTML to be rendered will be written to the command's stdin, and the program's output will be displayed. This makes it possible to use other, external programs, such as w3m, links or lynx, to render HTML.|html-renderer "w3m -dump -T text/html"
ignore-article|<feed> <filterexpr>|n/a|If a downloaded article from <feed> matches <filterexpr>, then it is ignored and not presented to the user. This command is further explained in the "kill file" section below.|ignore-article "*" "title =~ \\"Windows\\""
include|<path>|n/a|With this command, you can include other files to be interpreted as configuration files. This is especially useful to separate your configuration into several files, e.g. key configuration, color configuration, ...|include "~/.newsbeuter/colors"
Expand Down
3 changes: 3 additions & 0 deletions include/formaction.h
Expand Up @@ -53,6 +53,9 @@ class formaction {

virtual std::string title() = 0;

static void load_histories(const std::string& searchfile, const std::string& cmdlinefile);
static void save_histories(const std::string& searchfile, const std::string& cmdlinefile, unsigned int limit);

protected:
virtual void process_operation(operation op, bool automatic = false, std::vector<std::string> * args = NULL) = 0;
virtual void set_keymap_hints();
Expand Down
2 changes: 2 additions & 0 deletions include/history.h
Expand Up @@ -13,6 +13,8 @@ namespace newsbeuter {
void add_line(const std::string& line);
std::string prev();
std::string next();
void load_from_file(const std::string& file);
void save_to_file(const std::string& file, unsigned int limit);
private:
std::vector<std::string> lines;
unsigned int idx;
Expand Down
2 changes: 1 addition & 1 deletion src/configcontainer.cpp
Expand Up @@ -68,7 +68,7 @@ configcontainer::configcontainer()
config_data["mark-as-read-on-hover"] = configdata("false", configdata::BOOL);
config_data["search-highlight-colors"] = configdata("black yellow bold", configdata::STR, true);
config_data["pager"] = configdata("internal", configdata::PATH);

config_data["history-limit"] = configdata("100", configdata::INT);

/* undocumented: */
config_data["feedlist-title-format"] = configdata(_("%N %V - Your feeds (%u unread, %t total)%?T? - tag `%T'&?"), configdata::STR);
Expand Down
10 changes: 10 additions & 0 deletions src/controller.cpp
Expand Up @@ -237,6 +237,9 @@ void controller::run(int argc, char * argv[]) {
}
}

std::string searchfile = utils::strprintf("%s%shistory.search", config_dir.c_str(), NEWSBEUTER_PATH_SEP);
std::string cmdlinefile = utils::strprintf("%s%shistory.cmdline", config_dir.c_str(), NEWSBEUTER_PATH_SEP);

if (!silent)
std::cout << _("Loading configuration...");
std::cout.flush();
Expand Down Expand Up @@ -441,9 +444,16 @@ void controller::run(int argc, char * argv[]) {
v->set_keymap(&keys);
v->set_tags(tags);


formaction::load_histories(searchfile, cmdlinefile);

// run the view
v->run();

unsigned int history_limit = cfg.get_configvalue_as_int("history-limit");
LOG(LOG_DEBUG, "controller::run: history-limit = %u", history_limit);
formaction::save_histories(searchfile, cmdlinefile, history_limit);

std::cout << _("Cleaning up cache...");
std::cout.flush();
try {
Expand Down
9 changes: 9 additions & 0 deletions src/formaction.cpp
Expand Up @@ -271,5 +271,14 @@ void formaction::start_next_question() {
}
}

void formaction::load_histories(const std::string& searchfile, const std::string& cmdlinefile) {
searchhistory.load_from_file(searchfile);
cmdlinehistory.load_from_file(cmdlinefile);
}

void formaction::save_histories(const std::string& searchfile, const std::string& cmdlinefile, unsigned int limit) {
searchhistory.save_to_file(searchfile, limit);
cmdlinehistory.save_to_file(cmdlinefile, limit);
}

}
30 changes: 30 additions & 0 deletions src/history.cpp
@@ -1,3 +1,4 @@
#include <fstream>
#include <history.h>

namespace newsbeuter {
Expand Down Expand Up @@ -35,5 +36,34 @@ std::string history::next() {
return "";
}

void history::load_from_file(const std::string& file) {
std::fstream f;
f.open(file.c_str(), std::fstream::in);
if (f.is_open()) {
std::string line;
do {
getline(f, line);
if (!f.eof() && line.length() > 0) {
add_line(line);
}
} while (!f.eof());
}
}

void history::save_to_file(const std::string& file, unsigned int limit) {
std::fstream f;
f.open(file.c_str(), std::fstream::out | std::fstream::trunc);
if (f.is_open()) {
if (limit > lines.size())
limit = lines.size();
if (limit > 0) {
for (unsigned int i=limit-1;i>0;i--) {
f << lines[i] << std::endl;
}
f << lines[0] << std::endl;
}
}
}


}

0 comments on commit 0bb3daf

Please sign in to comment.