Skip to content

Commit

Permalink
Implement scrolling with ctrl+d and ctrl-u
Browse files Browse the repository at this point in the history
  • Loading branch information
zorbash committed Mar 12, 2020
1 parent da5cbda commit f7b80e1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions lib/tefter_cli/views/components/cursor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 == [] ->
Expand All @@ -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
Expand Down

0 comments on commit f7b80e1

Please sign in to comment.