old_contents: import curses
  from curses import panel
  
  def run_menu(menu_items,x=0,y=0,name=None):
      """
      will display the menu at x, y on a newly created window
      """
      length = max(len(s) for s in menu_items) + 4
      height = len(menu_items)
      if name:
          height += 2
      current_option = 0
      option_selected = -1
  
      w = curses.newwin(height+2,length,y,x)
      w.keypad(1)
      w.refresh()
  
      while option_selected == -1:
          attribut=[curses.A_NORMAL]*height
          attribut[current_option]=curses.A_REVERSE+curses.A_BOLD
          line = 1
          if name:
              w.addstr(line,2,name,curses.A_BOLD+curses.A_UNDERLINE)
              line += 2
          for i,v in enumerate(menu_items):
              w.addstr(line,2,str(v),attribut[i])
              w.clrtoeol()
              line += 1
          w.border()
          w.refresh()
          a = w.getch()
          if a == curses.KEY_DOWN:
              current_option+=1
          elif a == curses.KEY_UP:
              current_option-=1
          elif a==ord('\n') or a == 32 :
          # validation can be done by CR or space bar
              option_selected=current_option
          elif a in range(ord('0'),ord('0')+height):
          # in case key pressed is a number
              current_option=a-ord('0')
              option_selected=current_option
          if current_option>height-1:
              current_option=height-1
          elif current_option <0:
              current_option=0
      return option_selected
  
new_contents: import curses
  from curses import panel
  
  def run_menu(menu_items,x=0,y=0,name=None,border=True):
      """
      Display menu at x,y on a newly created window
      params:
        x(int) - x coordinate to create window
        y(int) - y coordinate to create window
        name(str) - optional title for menu
        border(bool) - display border around the menu
      """
      length = max(len(s) for s in menu_items) + 4
      height = len(menu_items)
  
      if name:
          height += 2
      current_opt = 0
      selected_opt = -1
  
      w = curses.newwin(height+2,length,y,x)
      w.keypad(1)
      w.refresh()
  
      while selected_opt == -1:
          #reverse and bold the selected option
          display_attr=[curses.A_NORMAL]*height
          display_attr[current_opt]=curses.A_REVERSE+curses.A_BOLD
  
          line = 1
          if name:
              w.addstr(line,2,name,curses.A_BOLD+curses.A_UNDERLINE)
              line += 2
          for i,v in enumerate(menu_items):
              w.addstr(line,2,str(v),display_attr[i])
              w.clrtoeol()
              line += 1
  
          if border:
              w.border()
  
          w.refresh()
  
          x = w.getch()
          if x == curses.KEY_DOWN:
              current_opt += 1
  
          elif x == curses.KEY_UP:
              current_opt -= 1
  
          #validation can be done by CR or space bar
          elif x == ord('\n') or a == 32 :
              selected_opt = current_opt
  
          #in case key pressed is a number
          elif x in range(ord('0'),ord('0')+height):
              current_opt=a-ord('0')
              selected_opt=current_opt
  
          if current_opt > len(menu_items) - 1:
              current_opt = len(menu_items) -1
  
          elif current_opt < 0:
              current_opt = 0
  
      return selected_opt
  
subject: refactor menu, cleanup if clauses