From f7b80e1913f4d9490020ce09ba6d3b3b75087b7d Mon Sep 17 00:00:00 2001 From: Dimitris Zorbas Date: Thu, 12 Mar 2020 22:02:01 +0000 Subject: [PATCH] Implement scrolling with ctrl+d and ctrl-u --- CHANGELOG | 6 ++++++ lib/tefter_cli/views/components/cursor.ex | 21 +++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index d2f795d..6a0256a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -4,6 +4,12 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## Unreleased +## [0.1.2] - 2020-03-12 + +### Added + +* Aliases and bookmark views allow scrolling with `ctrl + d` and `ctrl + u` + ## [0.1.1] - 2020-03-12 ### Added diff --git a/lib/tefter_cli/views/components/cursor.ex b/lib/tefter_cli/views/components/cursor.ex index 20451e4..9756544 100644 --- a/lib/tefter_cli/views/components/cursor.ex +++ b/lib/tefter_cli/views/components/cursor.ex @@ -4,14 +4,19 @@ defmodule TefterCli.Views.Components.Cursor do """ import Ratatouille.Constants, only: [key: 1] + alias TefterCli.Views.Components.Pagination @up [key(:arrow_up), key(:mouse_wheel_up), key(:ctrl_k), key(:ctrl_p)] @down [key(:arrow_down), key(:mouse_wheel_down), key(:ctrl_j), key(:ctrl_n)] + @next_page [key(:ctrl_d)] + @previous_page [key(:ctrl_u)] def update(state, msg, %{type: type}) do cursor = state[type][:cursor] resources_type = if TefterCli.Command.filter?(state), do: :filtered, else: :resources resources = state[type][resources_type] + scroll_size = round(Pagination.page_size() / 2) + resource_len = length(resources) case msg do _ when resources == [] -> @@ -21,14 +26,26 @@ defmodule TefterCli.Views.Components.Cursor do put_in(state, [type, :cursor], cursor - 1) {:event, %{key: k}} when k in @up -> - put_in(state, [type, :cursor], length(resources) - 1) + put_in(state, [type, :cursor], resource_len - 1) - {:event, %{key: k}} when k in @down and cursor < length(resources) - 1 -> + {:event, %{key: k}} when k in @down and cursor < resource_len - 1 -> put_in(state, [type, :cursor], cursor + 1) {:event, %{key: k}} when k in @down -> put_in(state, [type, :cursor], 0) + {:event, %{key: k}} when k in @next_page and cursor + scroll_size >= resource_len - 1 -> + put_in(state, [type, :cursor], resource_len - 1) + + {:event, %{key: k}} when k in @next_page -> + put_in(state, [type, :cursor], cursor + scroll_size) + + {:event, %{key: k}} when k in @previous_page and cursor - scroll_size >= 0 -> + put_in(state, [type, :cursor], cursor - scroll_size) + + {:event, %{key: k}} when k in @previous_page -> + put_in(state, [type, :cursor], 0) + _ -> state end