# Path: conjureup/ui/views/bundle_readme_view.py
# class BundleReadmeView(WidgetWrap):
# 
#     def __init__(self, spell_name, spell_dir,
#                  done_callback, initial_height):
#         self.spell_name = spell_name
#         self.spell_dir = spell_dir
#         self.done_callback = done_callback
#         self.initial_height = initial_height
# 
#         w = self.build_widgets()
#         super().__init__(w)
# 
#         self.pile.focus_position = 1
# 
#     def selectable(self):
#         return True
# 
#     def keypress(self, size, key):
#         if key == 'tab':
#             cur = self.pile.focus_position
#             self.pile.focus_position = 3 if cur == 1 else 1
#         else:
#             return super(BundleReadmeView, self).keypress(size, key)
# 
#     def build_widgets(self):
#         readme_files = glob(os.path.join(self.spell_dir, 'README.*'))
#         if len(readme_files) == 0:
#             self.readme_w = Text("No README found for bundle.")
#         else:
#             readme_file = readme_files[0]
#             if len(readme_files) != 1:
#                 utils.warning("Unexpected: {} files matching README.*"
#                               "- using {}".format(len(readme_files),
#                                                   readme_file))
#             with open(readme_file) as rf:
#                 rlines = [Text(l) for l in rf.readlines()]
#                 self.readme_w = BoxAdapter(ListBox(rlines),
#                                            self.initial_height)
# 
#         ws = [Text("About {}:".format(self.spell_name)),
#               Padding.right_50(Color.button_primary(
#                   PlainButton("Continue",
#                               self.do_continue),
#                   focus_map='button_primary focus')),
#               Padding.center(HR()),
#               Padding.center(self.readme_w, left=2),
#               Padding.center(HR()),
#               Padding.center(Text("Use arrow keys to scroll text "
#                                   "and TAB to select the button."))]
# 
#         self.pile = Pile(ws)
#         return Padding.center_90(Filler(self.pile, valign="top"))
# 
#     def handle_readme_updated(self, readme_text_f):
#         EventLoop.loop.event_loop._loop.call_soon_threadsafe(
#             self._update_readme_on_main_thread,
#             readme_text_f)
# 
#     def _update_readme_on_main_thread(self, readme_text_f):
#         self.readme_w.set_text(readme_text_f.result().splitlines())
#         self._invalidate()
# 
#     def do_continue(self, arg):
#         self.done_callback()
# 
# Path: conjureup/ui/widgets/selectors.py
# class MenuSelectButtonList(SelectList):
#     option_type = ValuedMenuSelectButton
#     wrapable = True
# 
#     def __init__(self, opts=None, default=None, *args, **kwargs):
#         super().__init__(opts, *args, **kwargs)
#         if default:
#             self.select_item_by_value(default)
#         else:
#             self.select_first()


from ubuntui.ev import EventLoop
from ubuntui.utils import Color
from urwid import Text
from conjureup.ui.views.base import BaseView
from conjureup.ui.views.bundle_readme_view import BundleReadmeView
from conjureup.ui.widgets.selectors import MenuSelectButtonList



class SpellPickerView(BaseView):
    title = "Spell Selection"
    subtitle = "Choose from this list of recommended spells"
    show_back_button = False

    def __init__(self, app, spells, cb):
        self.app = app
        self.cb = cb
        self.spells = spells
        self.config = self.app.config
        super().__init__()
        self.extend_command_map({
            'r': self.show_readme,
        })
        self.update_spell_description()

    def show_readme(self):
        _, rows = EventLoop.screen_size()
        cur_spell = self.selected_spell
        spellname = cur_spell['name']
        spelldir = cur_spell['spell-dir']
        brmv = BundleReadmeView(spellname, spelldir,

# Path: conjureup/ui/views/bundle_readme_view.py
# class BundleReadmeView(WidgetWrap):
# 
#     def __init__(self, spell_name, spell_dir,
#                  done_callback, initial_height):
#         self.spell_name = spell_name
#         self.spell_dir = spell_dir
#         self.done_callback = done_callback
#         self.initial_height = initial_height
# 
#         w = self.build_widgets()
#         super().__init__(w)
# 
#         self.pile.focus_position = 1
# 
#     def selectable(self):
#         return True
# 
#     def keypress(self, size, key):
#         if key == 'tab':
#             cur = self.pile.focus_position
#             self.pile.focus_position = 3 if cur == 1 else 1
#         else:
#             return super(BundleReadmeView, self).keypress(size, key)
# 
#     def build_widgets(self):
#         readme_files = glob(os.path.join(self.spell_dir, 'README.*'))
#         if len(readme_files) == 0:
#             self.readme_w = Text("No README found for bundle.")
#         else:
#             readme_file = readme_files[0]
#             if len(readme_files) != 1:
#                 utils.warning("Unexpected: {} files matching README.*"
#                               "- using {}".format(len(readme_files),
#                                                   readme_file))
#             with open(readme_file) as rf:
#                 rlines = [Text(l) for l in rf.readlines()]
#                 self.readme_w = BoxAdapter(ListBox(rlines),
#                                            self.initial_height)
# 
#         ws = [Text("About {}:".format(self.spell_name)),
#               Padding.right_50(Color.button_primary(
#                   PlainButton("Continue",
#                               self.do_continue),
#                   focus_map='button_primary focus')),
#               Padding.center(HR()),
#               Padding.center(self.readme_w, left=2),
#               Padding.center(HR()),
#               Padding.center(Text("Use arrow keys to scroll text "
#                                   "and TAB to select the button."))]
# 
#         self.pile = Pile(ws)
#         return Padding.center_90(Filler(self.pile, valign="top"))
# 
#     def handle_readme_updated(self, readme_text_f):
#         EventLoop.loop.event_loop._loop.call_soon_threadsafe(
#             self._update_readme_on_main_thread,
#             readme_text_f)
# 
#     def _update_readme_on_main_thread(self, readme_text_f):
#         self.readme_w.set_text(readme_text_f.result().splitlines())
#         self._invalidate()
# 
#     def do_continue(self, arg):
#         self.done_callback()
# 
# Path: conjureup/ui/widgets/selectors.py
# class MenuSelectButtonList(SelectList):
#     option_type = ValuedMenuSelectButton
#     wrapable = True
# 
#     def __init__(self, opts=None, default=None, *args, **kwargs):
#         super().__init__(opts, *args, **kwargs)
#         if default:
#             self.select_item_by_value(default)
#         else:
#             self.select_first()


# Path: conjureup/ui/views/spellpicker.py
from ubuntui.ev import EventLoop
from ubuntui.utils import Color
from urwid import Text
from conjureup.ui.views.base import BaseView
from conjureup.ui.views.bundle_readme_view import BundleReadmeView
from conjureup.ui.widgets.selectors import MenuSelectButtonList




class SpellPickerView(BaseView):
    title = "Spell Selection"
    subtitle = "Choose from this list of recommended spells"
    show_back_button = False

    def __init__(self, app, spells, cb):
        self.app = app
        self.cb = cb
        self.spells = spells
        self.config = self.app.config
        super().__init__()
        self.extend_command_map({
            'r': self.show_readme,
        })
        self.update_spell_description()

    def show_readme(self):
        _, rows = EventLoop.screen_size()
        cur_spell = self.selected_spell
        spellname = cur_spell['name']
        spelldir = cur_spell['spell-dir']
        brmv = BundleReadmeView(spellname, spelldir,

                                self.hide_readme,