3 ft`f9@sxddlmZddlmZddlZddlZddlZddlZddlZddl Z ddl Z d ddZ d ddZ Gd d d e ZdS) )absolute_import)unicode_literalsNc CsBy(d}tj|tj|}tjd|d}|Stk r<dSXdS)z Get the real terminal width ZabcdefghshhhhrN)fcntlZioctltermiosZ TIOCGWINSZstructunpackIOError)fdZbufretr /usr/lib/python3.6/term.py_real_term_widthsrcCs&tdd}|sdS|dkrdS|SdS)z@ Compute terminal width falling to default 80 in case of troubler)r PN)r)r Ztwr r r _term_width)s  rc @seZdZdZdZeddZeddZdddd Zd d d d dddddZ d d d d dddddZ dddddddZ dddddd d!d"dZ d#d$d%d&d'd(d)d*dZ d+d,ZdFd/d0ZdGd1d2Zd3d4Zd5d6Zd7d8ZdHd:d;Zdd?Zd@dAZdBdCZdDdEZd-S)ITermz>A class to provide some terminal "UI" helpers based on curses.TcCstS)N)r)selfr r r @sz Term.cCstS)N)r)rr r r rAsZsmulZrevZsgr0) underlinereversenormalrr)blackbluegreencyanredmagentayellowwhite)rr"r r$rr#r!r%zzzzz(B)boldblinkdimrrrzzzzzzzzzzzzzzzzcCs|j|_|j|_|j|_dS)N)_Term__ansi_forced_MODEMODE_Term__ansi_forced_FG_COLORFG_COLOR_Term__ansi_forced_BG_COLORBG_COLOR)rr r r Z __forced_initzszTerm.__forced_initNautoc CsLd|_d|_|dkr |jdSddddddd|_ddddddddd|_ddddddddd|_|dkrvd |_dS|stj}|jsd |_dSyt j |j d Wnt k rd |_dSXt j |_t jd |_x8|jD].}|}||jkr|j|}|j||j|<qW|jd jd }|r\x4|jjD]&\}}t j||jpNd|j|<q2W|jdjd }|rx8|jjD]*\}}t j||jpd}||j|<q~W|jdjd } | rx4|jjD]&\}}t j| |jpd|j|<qW|jdjd } | rHx8|jjD]*\}}t j| |jp6d} | |j|<qWdS)a Reinitializes the :class:`Term`. :param term_stream: the terminal stream that the :class:`Term` should be initialized to use. If *term_stream* is not given, :attr:`sys.stdout` is used. :param color: when to colorize output. Valid values are 'always', 'auto', and 'never'. 'always' will use ANSI codes to always colorize output, 'auto' will decide whether do colorize depending on the terminal, and 'never' will never colorize. TalwaysNr&)r'r(r)rrr)rrr r!r"r#r$r%ZneverF)r linesZsetfzutf-8ZsetafZsetbZsetab)_Term__enabledr3_Term__forced_initr+r-r/sysstdoutisattycursesZ setuptermfileno ExceptionZtigetstr _ctigetstrZtigetnum_Term__cap_names _tigetstrencode _Term__colorsitemsZtparmdecode_Term__ansi_colors) r term_streamcolorcap_namemodeZset_fgvalZ set_fg_ansiZfg_colorZset_bgZ set_bg_ansiZbg_colorr r r reinits        ""z Term.reinitcCs|j||dS)N)rI)rrDrEr r r __init__sz Term.__init__cCs0|j|p d}tjj|r"|j}tjdd|S)Nr&z \$<\d+>[/*]?)r<dnfZpycompZ is_py3bytesrBresub)rrFZcapr r r r>s zTerm._tigetstrcCs|j|t||jdS)zColorize string with colorr)r+str)rrEsr r r rEsz Term.colorcCs |jd|S)zMake string bold.r')rE)rrOr r r r'sz Term.boldFc s\|js |S|stj}fdd}x4|D],}||} |rFtj| tj} tj| ||}q(W|S)aSearch the string *haystack* for all occurrences of any string in the list *needles*. Prefix each occurrence with *beg*, and postfix each occurrence with *end*, then return the modified string. For example:: >>> yt = Term() >>> yt.sub('spam and eggs', 'x', 'z', ['and']) 'spam xandz eggs' This is particularly useful for emphasizing certain words in output: for example, calling :func:`sub` with *beg* = MODE['bold'] and *end* = MODE['normal'] will return a string that when printed to the terminal will appear to be *haystack* with each occurrence of the strings in *needles* in bold face. Note, however, that the :func:`sub_mode`, :func:`sub_bold`, :func:`sub_fg`, and :func:`sub_bg` methods provide convenient ways to access this same emphasizing functionality. :param haystack: the string to be modified :param beg: the string to be prefixed onto matches :param end: the string to be postfixed onto matches :param needles: a list of strings to add the prefixes and postfixes to :param escape: a function that accepts a string and returns the same string with problematic characters escaped. By default, :func:`re.escape` is used. :param ignore_case: whether case should be ignored when searching for matches :return: *haystack* with *beg* prefixing, and *end* postfixing, occurrences of the strings in *needles* cs|jS)N)group)match)begendr r rszTerm.sub..)r4rLescapetemplateIrM) rhaystackrRrSneedlesrTZ ignore_caseZrenderZneedleZpatr )rRrSr rMs  zTerm.subcKs|j|||jd|f|S)aOSearch the string *haystack* for all occurrences of any string in the list *needles*. Prefix each occurrence with *beg*, and postfix each occurrence with self.MODE['normal'], then return the modified string. If *beg* is an ANSI escape code, such as given by self.MODE['bold'], this method will return *haystack* with the formatting given by the code only applied to the strings in *needles*. :param haystack: the string to be modified :param beg: the string to be prefixed onto matches :param end: the string to be postfixed onto matches :param needles: a list of strings to add the prefixes and postfixes to :return: *haystack* with *beg* prefixing, and self.MODE['normal'] postfixing, occurrences of the strings in *needles* r)rMr+)rrWrRrXkwdsr r r sub_norm&sz Term.sub_normcKs|j||j||f|S)aTSearch the string *haystack* for all occurrences of any string in the list *needles*. Prefix each occurrence with self.MODE[*mode*], and postfix each occurrence with self.MODE['normal'], then return the modified string. This will return a string that when printed to the terminal will appear to be *haystack* with each occurrence of the strings in *needles* in the given *mode*. :param haystack: the string to be modified :param mode: the mode to set the matches to be in. Valid values are given by self.MODE.keys(). :param needles: a list of strings to add the prefixes and postfixes to :return: *haystack* with self.MODE[*mode*] prefixing, and self.MODE['normal'] postfixing, occurrences of the strings in *needles* )rZr+)rrWrGrXrYr r r sub_mode9sz Term.sub_modecKs|j|d|f|S)aSearch the string *haystack* for all occurrences of any string in the list *needles*. Prefix each occurrence with self.MODE['bold'], and postfix each occurrence with self.MODE['normal'], then return the modified string. This will return a string that when printed to the terminal will appear to be *haystack* with each occurrence of the strings in *needles* in bold face. :param haystack: the string to be modified :param needles: a list of strings to add the prefixes and postfixes to :return: *haystack* with self.MODE['bold'] prefixing, and self.MODE['normal'] postfixing, occurrences of the strings in *needles* r')r[)rrWrXrYr r r sub_boldMsz Term.sub_boldcKs|j||j||f|S)acSearch the string *haystack* for all occurrences of any string in the list *needles*. Prefix each occurrence with self.FG_COLOR[*color*], and postfix each occurrence with self.MODE['normal'], then return the modified string. This will return a string that when printed to the terminal will appear to be *haystack* with each occurrence of the strings in *needles* in the given color. :param haystack: the string to be modified :param color: the color to set the matches to be in. Valid values are given by self.FG_COLOR.keys(). :param needles: a list of strings to add the prefixes and postfixes to :return: *haystack* with self.FG_COLOR[*color*] prefixing, and self.MODE['normal'] postfixing, occurrences of the strings in *needles* )rZr-)rrWrErXrYr r r sub_fg_sz Term.sub_fgcKs|j||j||f|S)aSearch the string *haystack* for all occurrences of any string in the list *needles*. Prefix each occurrence with self.BG_COLOR[*color*], and postfix each occurrence with self.MODE['normal'], then return the modified string. This will return a string that when printed to the terminal will appear to be *haystack* with each occurrence of the strings in *needles* highlighted in the given background color. :param haystack: the string to be modified :param color: the background color to set the matches to be in. Valid values are given by self.BG_COLOR.keys(). :param needles: a list of strings to add the prefixes and postfixes to :return: *haystack* with self.BG_COLOR[*color*] prefixing, and self.MODE['normal'] postfixing, occurrences of the strings in *needles* )rZr/)rrWrErXrYr r r sub_bgssz Term.sub_bg)Nr0)Nr0)NF)__name__ __module__ __qualname____doc__r4propertyZ real_columnscolumnsr=r@rCr*r,r.r5rIrJr>rEr'rMrZr[r\r]r^r r r r r4sr    f   -r)r)r)Z __future__rrr9Z dnf.pycomprKrrLrr6rrrobjectrr r r r s