123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- #!/usr/bin/python
- # PYTHON_ARGCOMPLETE_OK
- import sys
- import osex
- import curses
- import pprint
- import locale
- import argparse
- import argcomplete
- import ioex, ioex.cursesex, ioex.selector
- class LocateRootNode(ioex.selector.Node):
- def __init__(
- self,
- window,
- patterns,
- match_all,
- ignore_case,
- database_path,
- update_database,
- update_require_visibility
- ):
- super(LocateRootNode, self).__init__()
- self._window = window
- self._patterns = [p for p in patterns if len(p) > 0]
- self._match_all = match_all
- self._ignore_case = ignore_case
- self._database_path = database_path
- self._update_database = update_database
- self._update_require_visibility = update_require_visibility
- self.refresh_children()
- def get_window_height(self):
- return self._window.getmaxyx()[0]
- def get_window_width(self):
- return self._window.getmaxyx()[1]
- def get_header_height(self):
- return self._header_height
- def show_patterns(self):
- self._window.addstr(0, 0, 'locate-select', curses.A_BOLD)
- self._window.refresh()
- self._header_height = 1
- def show_header(self):
- self.show_patterns()
- def refresh_children(self):
- self._window.clear()
- self.show_header()
- self._clear_children()
- if self._update_database:
- self._window.addstr(self.get_header_height(), 0, 'updating database... ')
- self._window.refresh()
- osex.update_locate_database(
- database_path = self._database_path,
- require_visibility = self._update_require_visibility
- )
- self._window.addstr(self.get_header_height(), 0, 'searching database... ')
- self._window.clrtoeol()
- self._window.refresh()
- paths = osex.locate(
- self._patterns,
- match_all = self._match_all,
- ignore_case = self._ignore_case,
- database_path = self._database_path
- )
- if len(paths) == 0:
- self._window.addstr(self.get_header_height(), 0, 'nothing found')
- self._window.clrtoeol()
- self._window.refresh()
- else:
- for path in paths:
- self._append_child(ioex.selector.StaticNode(path))
- def locate_select(stdscr, patterns, match_all, ignore_case, update_database, multiple, database_path, update_require_visibility):
- curses.curs_set(0)
- root = LocateRootNode(
- stdscr,
- patterns,
- match_all,
- ignore_case,
- database_path,
- update_database,
- update_require_visibility
- )
- selection = ioex.selector.select(
- stdscr,
- root,
- multiple = multiple
- )
- if selection is None:
- return None
- else:
- return [n.get_label() for n in selection]
- def _init_argparser():
- argparser = argparse.ArgumentParser(description = None)
- argparser.add_argument('--database-path')
- argparser.add_argument('--ignore-case', action='store_true')
- argparser.add_argument('--match-all', action='store_true')
- argparser.add_argument('--multiple', action='store_true')
- argparser.add_argument('--update-database', action='store_true')
- argparser.add_argument('--update-require-visibility', choices = ['yes', 'no'])
- argparser.add_argument('patterns', metavar = 'pattern', nargs = '+')
- return argparser
- def main(argv):
- argparser = _init_argparser()
- argcomplete.autocomplete(argparser)
- args = argparser.parse_args(argv)
- params = vars(args)
- if args.update_require_visibility == 'yes':
- args.update_require_visibility = True
- elif args.update_require_visibility == 'no':
- args.update_require_visibility = False
- paths = ioex.cursesex.tty_wrapper(locate_select, **vars(args))
- if paths is None:
- return 1
- else:
- print('\n'.join(paths).encode(locale.getpreferredencoding()))
- return 0
- if __name__ == "__main__":
- sys.exit(main(sys.argv[1:]))
|