Save history between command invocations

This commit is contained in:
Robert Bendun 2023-03-04 17:59:33 +01:00
parent ff58e6506d
commit 4dccde7830
6 changed files with 154 additions and 5 deletions

View File

@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Added
- `CTRL-C` handler that turns notes that are playing off
### Changed
- Moved from `bestline` to `replxx` Readline implementation due to lack of Windows support from bestline
## [0.4.0]
### Added

View File

@ -62,4 +62,11 @@ concept Three_Way_Comparable = requires (T const& lhs, T const& rhs) {
{ lhs <=> rhs };
};
template<typename Needle, typename ...Heystack>
requires (std::equality_comparable_with<Needle const&, Heystack const&> && ...)
constexpr bool one_of(Needle const& needle, Heystack const& ...heystack)
{
return ((needle == heystack) || ...);
}
#endif

View File

@ -14,9 +14,11 @@
#include <musique/lexer/lines.hh>
#include <musique/midi/midi.hh>
#include <musique/parser/parser.hh>
#include <musique/platform.hh>
#include <musique/pretty.hh>
#include <musique/try.hh>
#include <musique/unicode.hh>
#include <musique/user_directory.hh>
#include <musique/value/block.hh>
#include <replxx.hxx>
@ -251,8 +253,9 @@ void completion(char const* buf, bestlineCompletions *lc)
}
#endif
bool is_tty()
static bool is_tty()
{
// Can't use if constexpr since _isatty is not defined on non-windows platforms
#ifdef _WIN32
return _isatty(STDOUT_FILENO);
#else
@ -485,13 +488,11 @@ static std::optional<Error> Main(std::span<char const*> args)
replxx::Replxx repl;
{
std::ifstream history(".musique_history");
repl.history_load(history);
}
auto const history_path = (user_directory::data_home() / "history").string();
repl.set_max_history_size(2048);
repl.set_max_hint_rows(3);
repl.history_load(history_path);
for (;;) {
char const* input = nullptr;
@ -513,6 +514,7 @@ static std::optional<Error> Main(std::span<char const*> args)
}
repl.history_add(std::string(command));
repl.history_save(history_path);
if (command.starts_with(':')) {
command.remove_prefix(1);

25
musique/platform.hh Normal file
View File

@ -0,0 +1,25 @@
#ifndef MUSIQUE_PLATFORM_HH
#define MUSIQUE_PLATFORM_HH
namespace platform
{
enum class Operating_System
{
MacOS,
Unix,
Windows,
};
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
static constexpr Operating_System os = Operating_System::Windows;
#elif defined(__APPLE__)
static constexpr Operating_System os = Operating_System::MacOS;
Operating_System::MacOS
#elif defined(__linux__) || defined(__unix__)
static constexpr Operating_System os = Operating_System::Unix;
#else
#error "Unknown platform"
#endif
}
#endif // MUSIQUE_PLATFORM_HH

92
musique/user_directory.cc Normal file
View File

@ -0,0 +1,92 @@
#include <musique/user_directory.hh>
#include <musique/errors.hh>
#include <musique/platform.hh>
static std::filesystem::path home()
{
if constexpr (platform::os == platform::Operating_System::Windows) {
if (auto home = std::getenv("USERPROFILE")) return home;
if (auto drive = std::getenv("HOMEDRIVE")) {
if (auto path = std::getenv("HOMEPATH")) {
return std::string(drive) + path;
}
}
} else {
if (auto home = std::getenv("HOME")) {
return home;
}
}
unreachable();
}
std::filesystem::path user_directory::data_home()
{
std::filesystem::path path;
static_assert(one_of(platform::os,
platform::Operating_System::Unix,
platform::Operating_System::Windows,
platform::Operating_System::MacOS
));
if constexpr (platform::os == platform::Operating_System::Unix) {
if (auto data = std::getenv("XDG_DATA_HOME")) {
path = data;
} else {
path = home() / ".local" / "share";
}
}
if constexpr (platform::os == platform::Operating_System::Windows) {
if (auto data = std::getenv("LOCALAPPDATA")) {
path = data;
} else {
path = home() / "AppData" / "Local";
}
}
if constexpr (platform::os == platform::Operating_System::MacOS) {
path = home() / "Library";
}
path /= "musique";
std::filesystem::create_directories(path);
return path;
}
std::filesystem::path user_directory::config_home()
{
std::filesystem::path path;
static_assert(one_of(platform::os,
platform::Operating_System::Unix,
platform::Operating_System::Windows,
platform::Operating_System::MacOS
));
if constexpr (platform::os == platform::Operating_System::Unix) {
if (auto data = std::getenv("XDG_CONFIG_HOME")) {
path = data;
} else {
path = home() / ".config";
}
}
if constexpr (platform::os == platform::Operating_System::Windows) {
if (auto data = std::getenv("LOCALAPPDATA")) {
path = data;
} else {
path = home() / "AppData" / "Local";
}
}
if constexpr (platform::os == platform::Operating_System::MacOS) {
path = home() / "Library" / "Preferences";
}
path /= "musique";
std::filesystem::create_directories(path);
return path;
}

15
musique/user_directory.hh Normal file
View File

@ -0,0 +1,15 @@
#ifndef MUSIQUE_USER_DIRECTORY_HH
#define MUSIQUE_USER_DIRECTORY_HH
#include <filesystem>
namespace user_directory
{
/// Returns system-specific user directory for data; same as XDG_DATA_HOME
std::filesystem::path data_home();
/// Returns system-specific user directory for config; same as XDG_CONFIG_HOME
std::filesystem::path config_home();
}
#endif // MUSIQUE_USER_DIRECTORY_HH