"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/http_prompt/utils.py" (5 Mar 2021, 2104 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "utils.py" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.0.0_vs_2.1.0.

    1 from __future__ import unicode_literals
    2 
    3 import math
    4 import re
    5 import shlex
    6 
    7 from prompt_toolkit.shortcuts import create_output
    8 
    9 
   10 RE_ANSI_ESCAPE = re.compile(r'\x1b[^m]*m')
   11 
   12 
   13 def smart_quote(s):
   14     return shlex.quote(s)
   15 
   16 
   17 def unquote(s):
   18     quotes = ["'", '"']
   19     quote_str = None
   20     if s[0] in quotes:
   21         quote_str = s[0]
   22 
   23     if quote_str and s[-1] == quote_str:
   24         return s[1: -1]
   25     return s
   26 
   27 
   28 def unescape(s, exclude=None):
   29     if exclude:
   30         char = '[^%s]' % exclude
   31     else:
   32         char = '.'
   33     return re.sub(r'\\(%s)' % char, r'\1', s)
   34 
   35 
   36 def get_terminal_size():
   37     return create_output().get_size()
   38 
   39 
   40 def strip_ansi_escapes(text):
   41     return RE_ANSI_ESCAPE.sub('', text)
   42 
   43 
   44 def colformat(strings, num_sep_spaces=1, terminal_width=None):
   45     """Format a list of strings like ls does multi-column output."""
   46     if terminal_width is None:
   47         terminal_width = get_terminal_size().columns
   48 
   49     if not strings:
   50         return
   51 
   52     num_items = len(strings)
   53     max_len = max([len(strip_ansi_escapes(s)) for s in strings])
   54 
   55     num_columns = min(
   56         int((terminal_width + num_sep_spaces) / (max_len + num_sep_spaces)),
   57         num_items)
   58     num_columns = max(1, num_columns)
   59 
   60     num_lines = int(math.ceil(float(num_items) / num_columns))
   61     num_columns = int(math.ceil(float(num_items) / num_lines))
   62 
   63     num_elements_last_column = num_items % num_lines
   64     if num_elements_last_column == 0:
   65         num_elements_last_column = num_lines
   66 
   67     lines = []
   68     for i in range(num_lines):
   69         line_size = num_columns
   70         if i >= num_elements_last_column:
   71             line_size -= 1
   72         lines.append([None] * line_size)
   73 
   74     for i, line in enumerate(lines):
   75         line_size = len(line)
   76         for j in range(line_size):
   77             k = i + num_lines * j
   78             item = strings[k]
   79             if j % line_size != line_size - 1:
   80                 item_len = len(strip_ansi_escapes(item))
   81                 item = item + ' ' * (max_len - item_len)
   82             line[j] = item
   83 
   84     sep = ' ' * num_sep_spaces
   85     for line in lines:
   86         yield sep.join(line)