from __future__ import absolute_import, unicode_literals
import curses
from curses import panel
from pytify.strategy import get_pytify_class_by_platform


class SongList():
    def __init__(self, items):
        self.pytify = get_pytify_class_by_platform()()
        self.items = items

        self.position = 2
        self.song_length = len(items) - 1

        # Init curses screen
        self.window = curses.initscr()

        self.window.keypad(1)

        self.panel = panel.new_panel(self.window)
        self.panel.hide()
        panel.update_panels()

        # Show shortcuts
        self.shortcuts()

        # Disable echoing of keys to the screen
        curses.noecho()

        # Disable blinking cursor
        curses.curs_set(False)

        # Use user terminal settings
        curses.endwin()

        # Display window
        self.display()

    def shortcuts(self):
        self.items.append(' ')
        self.items.append('Keyboard shortcuts')
        self.items.append('==================')
        self.items.append('Navigation (Vim bindings):')
        self.items.append('  <K> <up> ')
        self.items.append('  <J> <down> ')
        self.items.append('  <H> <left> ')
        self.items.append('  <L> <right> ')
        self.items.append('Play: <P>')
        self.items.append('Search: <S>')
        self.items.append('Play/Pause: <SPACEBAR>')

    def navigate(self, n):
        self.position += n

        if self.position < 2:
            self.position = 2
        elif self.position > self.song_length:
            self.position = self.song_length

    def display(self):
        self.panel.top()
        self.panel.show()
        self.window.clear()

        while True:
            self.window.refresh()
            curses.doupdate()

            for index, item in enumerate(self.items):
                if index == self.position:
                    mode = curses.A_REVERSE
                else:
                    mode = curses.A_NORMAL

                self.window.addstr(index, 1, str(item), mode)

            key = self.window.getch()

            # Start song
            if key == ord('p'):
                self.pytify.listen(int(self.position - 1))

            # Up
            elif key == ord('k'):
                self.navigate(-1)

            # Down
            elif key == ord('j'):
                self.navigate(1)

            # Left
            elif key == ord('h'):
                self.pytify.prev()

            # Rights
            elif key == ord('l'):
                self.pytify.next()

            # Play/Pause
            elif key == ord(' '):
                self.pytify.play_pause()

            # Search
            elif key == ord('s'):
                break

        self.window.clear()
        self.panel.hide()
        panel.update_panels()
        curses.doupdate()

@@ -2240,16 +2240,40 @@
 ord('k')
+ or key == curses.KEY_UP
 :%0A      
@@ -2352,16 +2352,42 @@
 ord('j')
+ or key == curses.KEY_DOWN
 :%0A      
@@ -2465,16 +2465,42 @@
 ord('h')
+ or key == curses.KEY_LEFT
 :%0A      
@@ -2582,16 +2582,43 @@
 ord('l')
+ or key == curses.KEY_RIGHT
 :%0A      

Add support for keyboard arrow keys