From ed64cf5e34caefce5161b2cdc447d580e5f2a4f3 Mon Sep 17 00:00:00 2001 From: Karolina Gontarek Date: Fri, 9 Apr 2021 21:08:56 +0200 Subject: [PATCH] Implementation of variable with killring entries --- src/env.cpp | 4 ++++ src/fish_tests.cpp | 27 +++++++++++++++++++++++++++ src/kill.cpp | 4 ++++ src/kill.h | 3 +++ 4 files changed, 38 insertions(+) diff --git a/src/env.cpp b/src/env.cpp index a7a04075d..32fbd529c 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -35,6 +35,7 @@ #include "termsize.h" #include "wcstringutil.h" #include "wutil.h" // IWYU pragma: keep +#include "kill.h" /// Some configuration path environment variables. #define FISH_DATADIR_VAR L"__fish_data_dir" @@ -93,6 +94,7 @@ static constexpr const electric_var_t electric_variables[] = { {L"fish_pid", electric_var_t::freadonly}, {L"history", electric_var_t::freadonly | electric_var_t::fcomputed}, {L"hostname", electric_var_t::freadonly}, + {L"killring", electric_var_t::freadonly | electric_var_t::fcomputed}, {L"pipestatus", electric_var_t::freadonly | electric_var_t::fcomputed}, {L"status", electric_var_t::freadonly | electric_var_t::fcomputed}, {L"status_generation", electric_var_t::freadonly | electric_var_t::fcomputed}, @@ -716,6 +718,8 @@ maybe_t env_scoped_impl_t::try_get_computed(const wcstring &key) cons wcstring_list_t result; if (history) history->get_history(result); return env_var_t(L"history", std::move(result)); + } else if (key == L"killring") { + return env_var_t(L"killring", kill_entries()); } else if (key == L"pipestatus") { const auto &js = perproc_data().statuses; wcstring_list_t result; diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 280c491f2..0bc96fc80 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -83,6 +83,7 @@ #include "wcstringutil.h" #include "wildcard.h" #include "wutil.h" // IWYU pragma: keep +#include "kill.h" static const char *const *s_arguments; static int s_test_run_count = 0; @@ -6409,6 +6410,31 @@ Executed in 500.00 micros fish external free(saved_locale); } +static void test_killring() { + say(L"Testing killring"); + + do_test(kill_entries().empty()); + + kill_add(L"a"); + kill_add(L"b"); + kill_add(L"c"); + + do_test((kill_entries() == wcstring_list_t{L"c", L"b", L"a"})); + + do_test(kill_yank_rotate() == L"b"); + do_test((kill_entries() == wcstring_list_t{L"b", L"a", L"c"})); + + do_test(kill_yank_rotate() == L"a"); + do_test((kill_entries() == wcstring_list_t{L"a", L"c", L"b"})); + + kill_add(L"d"); + + do_test((kill_entries() == wcstring_list_t{L"d", L"a", L"c", L"b"})); + + do_test(kill_yank_rotate() == L"a"); + do_test((kill_entries() == wcstring_list_t{L"a", L"c", L"b", L"d"})); +} + struct termsize_tester_t { static void test(); }; @@ -6617,6 +6643,7 @@ int main(int argc, char **argv) { // history_tests_t::test_history_speed(); if (should_test_function("termsize")) termsize_tester_t::test(); + if (should_test_function("killring")) test_killring(); say(L"Encountered %d errors in low-level tests", err_count); if (s_test_run_count == 0) say(L"*** No Tests Were Actually Run! ***"); diff --git a/src/kill.cpp b/src/kill.cpp index 8f6bc67a8..9e1007762 100644 --- a/src/kill.cpp +++ b/src/kill.cpp @@ -55,3 +55,7 @@ wcstring kill_yank() { } return kill_list.front(); } + +wcstring_list_t kill_entries() { + return {kill_list.begin(), kill_list.end()}; +} \ No newline at end of file diff --git a/src/kill.h b/src/kill.h index a089e4041..90456c9ec 100644 --- a/src/kill.h +++ b/src/kill.h @@ -19,4 +19,7 @@ wcstring kill_yank_rotate(); /// Paste from the killring. wcstring kill_yank(); +/// Get copy of kill ring as vector of strings +wcstring_list_t kill_entries(); + #endif