Completer function for cd, which only returns directories.
(self, event)
| 324 | |
| 325 | |
| 326 | def cd_completer(self, event): |
| 327 | """Completer function for cd, which only returns directories.""" |
| 328 | ip = get_ipython() |
| 329 | relpath = event.symbol |
| 330 | |
| 331 | #print(event) # dbg |
| 332 | if event.line.endswith('-b') or ' -b ' in event.line: |
| 333 | # return only bookmark completions |
| 334 | bkms = self.db.get('bookmarks', None) |
| 335 | if bkms: |
| 336 | return bkms.keys() |
| 337 | else: |
| 338 | return [] |
| 339 | |
| 340 | if event.symbol == '-': |
| 341 | width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) |
| 342 | # jump in directory history by number |
| 343 | fmt = '-%0' + width_dh +'d [%s]' |
| 344 | ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] |
| 345 | if len(ents) > 1: |
| 346 | return ents |
| 347 | return [] |
| 348 | |
| 349 | if event.symbol.startswith('--'): |
| 350 | return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']] |
| 351 | |
| 352 | # Expand ~ in path and normalize directory separators. |
| 353 | relpath, tilde_expand, tilde_val = expand_user(relpath) |
| 354 | relpath = relpath.replace('\\','/') |
| 355 | |
| 356 | found = [] |
| 357 | for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') |
| 358 | if os.path.isdir(f)]: |
| 359 | if ' ' in d: |
| 360 | # we don't want to deal with any of that, complex code |
| 361 | # for this is elsewhere |
| 362 | raise TryNext |
| 363 | |
| 364 | found.append(d) |
| 365 | |
| 366 | if not found: |
| 367 | if os.path.isdir(relpath): |
| 368 | return [compress_user(relpath, tilde_expand, tilde_val)] |
| 369 | |
| 370 | # if no completions so far, try bookmarks |
| 371 | bks = self.db.get('bookmarks',{}) |
| 372 | bkmatches = [s for s in bks if s.startswith(event.symbol)] |
| 373 | if bkmatches: |
| 374 | return bkmatches |
| 375 | |
| 376 | raise TryNext |
| 377 | |
| 378 | return [compress_user(p, tilde_expand, tilde_val) for p in found] |
| 379 | |
| 380 | def reset_completer(self, event): |
| 381 | "A completer for %reset magic" |
nothing calls this directly
no test coverage detected
searching dependent graphs…