def main(basedir=None, query=None): from calibre import prints from calibre.utils.terminal import ColoredStream if basedir is None: try: basedir = input_unicode('Enter directory to scan [%s]: ' % os.getcwd()).strip() or os.getcwd() except (EOFError, KeyboardInterrupt): return m = FilesystemMatcher(basedir) emph = ColoredStream(sys.stdout, fg='red', bold=True) while True: if query is None: try: query = input_unicode('Enter query: ') except (EOFError, KeyboardInterrupt): break if not query: break for path, positions in islice(iteritems(m(query)), 0, 10): positions = list(positions) p = 0 while positions: pos = positions.pop(0) if pos == -1: continue prints(path[p:pos], end='') ch = get_char(path, pos) with emph: prints(ch, end='') p = pos + len(ch) prints(path[p:]) query = None
def do_list(fields, data, opts): from calibre.utils.terminal import geometry, ColoredStream separator = ' ' widths = list(map(lambda x: 0, fields)) for i in data: for j, field in enumerate(fields): widths[j] = max(widths[j], max(len(field), len(unicode_type(i[field])))) screen_width = geometry()[0] if not screen_width: screen_width = 80 field_width = screen_width // len(fields) base_widths = list(map(lambda x: min(x + 1, field_width), widths)) while sum(base_widths) < screen_width: adjusted = False for i in range(len(widths)): if base_widths[i] < widths[i]: base_widths[i] += min( screen_width - sum(base_widths), widths[i] - base_widths[i] ) adjusted = True break if not adjusted: break widths = list(base_widths) titles = map( lambda x, y: '%-*s%s' % (x - len(separator), y, separator), widths, fields ) with ColoredStream(sys.stdout, fg='green'): prints(''.join(titles)) wrappers = list(map(lambda x: TextWrapper(x - 1), widths)) for record in data: text = [ wrappers[i].wrap(unicode_type(record[field])) for i, field in enumerate(fields) ] lines = max(map(len, text)) for l in range(lines): for i, field in enumerate(text): ft = text[i][l] if l < len(text[i]) else '' filler = '%*s' % (widths[i] - len(ft) - 1, '') print(ft.encode('utf-8') + filler.encode('utf-8'), end=separator) print()
def do_list(dbctx, fields, afields, sort_by, ascending, search_text, line_width, separator, prefix, limit, for_machine=False): if sort_by is None: ascending = True ans = dbctx.run('list', fields, sort_by, ascending, search_text, limit) try: book_ids, data, metadata = ans['book_ids'], ans['data'], ans[ 'metadata'] except TypeError: raise SystemExit(ans) fields = list(ans['fields']) try: fields.remove('id') except ValueError: pass fields = ['id'] + fields stringify(data, metadata, for_machine) if for_machine: raw = json.dumps(list(as_machine_data(book_ids, data, metadata)), indent=2, sort_keys=True) if not isinstance(raw, bytes): raw = raw.encode('utf-8') getattr(sys.stdout, 'buffer', sys.stdout).write(raw) return from calibre.utils.terminal import ColoredStream, geometry output_table = prepare_output_table(fields, book_ids, data, metadata) widths = list(map(lambda x: 0, fields)) for record in output_table: for j in range(len(fields)): widths[j] = max(widths[j], str_width(record[j])) screen_width = geometry()[0] if line_width < 0 else line_width if not screen_width: screen_width = 80 field_width = screen_width // len(fields) base_widths = list(map(lambda x: min(x + 1, field_width), widths)) while sum(base_widths) < screen_width: adjusted = False for i in range(len(widths)): if base_widths[i] < widths[i]: base_widths[i] += min(screen_width - sum(base_widths), widths[i] - base_widths[i]) adjusted = True break if not adjusted: break widths = list(base_widths) titles = map(lambda x, y: '%-*s%s' % (x - len(separator), y, separator), widths, fields) with ColoredStream(sys.stdout, fg='green'): prints(''.join(titles)) wrappers = [ TextWrapper(x - 1).wrap if x > 1 else lambda y: y for x in widths ] for record in output_table: text = [wrappers[i](record[i]) for i, field in enumerate(fields)] lines = max(map(len, text)) for l in range(lines): for i, field in enumerate(text): ft = text[i][l] if l < len(text[i]) else u'' sys.stdout.write(ft.encode('utf-8')) if i < len(text) - 1: filler = (u'%*s' % (widths[i] - str_width(ft) - 1, u'')) sys.stdout.write((filler + separator).encode('utf-8')) print()
def prints(self, level, *args, **kwargs): from calibre.utils.terminal import ColoredStream with ColoredStream(self.stream, self.color[level]): self._prints(*args, **kwargs)