diff --git a/.gitignore b/.gitignore index 17a06cc..3a3d60f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,8 @@ temp # Python *.pyc *.pyo +dist/ +jj_menu.egg-info/ # Windows Thumbs.db diff --git a/MANIFEST.in b/MANIFEST.in new file mode 100644 index 0000000..6cd9d95 --- /dev/null +++ b/MANIFEST.in @@ -0,0 +1 @@ +include activate-jj.sh diff --git a/README.rst b/README.rst index e70c963..26c0a67 100644 --- a/README.rst +++ b/README.rst @@ -9,7 +9,7 @@ Install ------- :: - $ sudo pip install git+https://github.com/junion-org/pip_github_test.git + $ pip install git+https://github.com/ytyng/jj-menu.git Setup diff --git a/activate-jj.sh b/activate-jj.sh new file mode 100644 index 0000000..dcb4bba --- /dev/null +++ b/activate-jj.sh @@ -0,0 +1,9 @@ +#!/bin/bash +function jj(){ + RESULT_FILE=/tmp/_jj_result + jj-menu --result-file=${RESULT_FILE} + if [ $? == 0 ]; then + echo "> `cat ${RESULT_FILE}`" + source ${RESULT_FILE} + fi +} diff --git a/jj_menu/__init__.py b/jj_menu/__init__.py index b64b8b2..0110451 100644 --- a/jj_menu/__init__.py +++ b/jj_menu/__init__.py @@ -2,5 +2,5 @@ # coding: utf-8 __author__ = 'ytyng' -__version__ = '0.0.1' +__version__ = '0.0.2' __license__ = 'MIT' diff --git a/jj_menu/jj_menu.py b/jj_menu/jj_menu.py index f749972..f8ea0ba 100644 --- a/jj_menu/jj_menu.py +++ b/jj_menu/jj_menu.py @@ -2,20 +2,20 @@ # -*- coding:utf-8 -*- from __future__ import unicode_literals, print_function + +import argparse import os import sys import locale import curses import _curses +import subprocess import six -# 文字化け対応 locale.setlocale(locale.LC_ALL, '') COLOR_ACTIVE = 1 -result_filename = '/tmp/_jj_result' - class MenuFileNotFound(Exception): pass @@ -54,9 +54,6 @@ def import_menu_settings(): def get_menu(): def _get_menus(): - """ - メニュー項目1つなら同じものに展開 - """ menus = getattr(import_menu_settings(), 'menu') for m in menus: if isinstance(m, str): @@ -89,9 +86,10 @@ def window_addstr(window, y, x, message, color=None): class Launcher(object): - def __init__(self, stdscr): - stdscr.refresh() # なにより先にまず1回リフレッシュ + def __init__(self, stdscr, result_file=None): + stdscr.refresh() self.stdscr = stdscr + self.result_file = result_file self.max_y, self.max_x = stdscr.getmaxyx() self.pos_y = 0 self.menu = get_menu() @@ -121,7 +119,7 @@ def render(self): def debug(self, message): """ - 簡易デバッグ + Easy debug """ win = curses.newwin(1, 10, self.max_y - 2, self.max_x - 10) window_addstr(win, 0, 0, message[:10]) @@ -132,7 +130,7 @@ def serve(self): self.render() - # キー入力待機 + # Waiting key input c = self.stdscr.getch() if c in (14, 106, 258): # ↓ @@ -151,32 +149,43 @@ def serve(self): elif c in (113, 27): # Q, Esc raise KeyboardInterrupt() elif c == 10: - # 決定 - with open(result_filename, 'w') as fp: - fp.write(self.menu[self.pos_y][1]) - return self.menu[self.pos_y] + # choose + script = self.menu[self.pos_y][1] + if self.result_file: + with open(self.result_file, 'w') as fp: + fp.write(script) + return script else: self.debug('{}'.format(c)) def init_outfile(self): - with open(result_filename, 'w') as fp: + if not self.result_file: + return + with open(self.result_file, 'w') as fp: fp.write('') -def launch(stdscr): +def launch(stdscr, args): initialize_colors() _curses.curs_set(0) - # 画面サイズ取得 - launcher = Launcher(stdscr) + launcher = Launcher(stdscr, result_file=args.result_file) - selected = launcher.serve() - return selected + return launcher.serve() def main(): + parser = argparse.ArgumentParser(description='jj-menu core') + parser.add_argument('--result-file', dest='result_file', + help='result script file path', default=None) + + args = parser.parse_args() + try: - selected = curses.wrapper(launch) - print('$ {}'.format(selected[1])) + script = curses.wrapper(launch, args) + print('$ {}'.format(script)) + if not args.result_file: + print(subprocess.check_output(script, shell=True)) + except KeyboardInterrupt: exit(1) diff --git a/setup.py b/setup.py index 599d0bc..b21d3c6 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,11 @@ from setuptools import setup, find_packages from jj_menu import __author__, __version__, __license__ -install_requires=['curses', 'six'] +# In [2]: from setuptools.command.bdist_egg import _get_purelib +# +# In [3]: _get_purelib() +# Out[3]: '/Users/yotsuyanagi/.virtualenvs/default/lib/python2.7/site-packages' +# $ cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())" setup( name='jj-menu', @@ -15,5 +19,10 @@ url='https://github.com/jytyng/jj-menu.git', keywords='CLI Menu, Python', packages=find_packages(), - install_requires=[], + install_requires=['six'], + entry_points={ + 'console_scripts': [ + 'jj-menu = jj_menu.jj_menu:main', + ] + }, )