def test_escape_windows_cmd_string(): cases = [('', ''), ('foo', 'foo'), ('foo&bar', 'foo^&bar'), ('foo$?-/_"\\', 'foo$?-/_^"\\'), ('^&<>|', '^^^&^<^>^|'), ('this /?', 'this /.')] for st, esc in cases: obs = escape_windows_cmd_string(st) yield assert_equal, esc, obs
def test_escape_windows_cmd_string(): cases = [ ('', ''), ('foo', 'foo'), ('foo&bar', 'foo^&bar'), ('foo$?-/_"\\', 'foo$?-/_^"\\'), ('^&<>|', '^^^&^<^>^|'), ('this /?', 'this /.') ] for st, esc in cases: obs = escape_windows_cmd_string(st) yield assert_equal, esc, obs
def settitle(self): """Sets terminal title.""" _ = self env = builtins.__xonsh_env__ # pylint: disable=no-member term = env.get('TERM', None) # Shells running in emacs sets TERM to "dumb" or "eterm-color". # Do not set title for these to avoid garbled prompt. if term is None or term in ['dumb', 'eterm-color', 'linux']: return t = env.get('TITLE') if t is None: return t = format_prompt(t) if ON_WINDOWS and 'ANSICON' not in env: t = escape_windows_cmd_string(t) os.system('title {}'.format(t)) else: os.write(1, "\x1b]2;{0}\x07".format(t).encode())
def source_cmd(args, stdin=None): """Simple cmd.exe-specific wrapper around source-foreign.""" args = list(args) fpath = locate_binary(args[0]) args[0] = fpath if fpath else args[0] if not os.path.isfile(args[0]): return (None, 'xonsh: error: File not found: {}\n'.format(args[0]), 1) prevcmd = 'call ' prevcmd += ' '.join([argvquote(arg, force=True) for arg in args]) prevcmd = escape_windows_cmd_string(prevcmd) args.append('--prevcmd={}'.format(prevcmd)) args.insert(0, 'cmd') args.append('--interactive=0') args.append('--sourcer=call') args.append('--envcmd=set') args.append('--seterrpostcmd=if errorlevel 1 exit 1') args.append('--use-tmpfile=1') return source_foreign(args, stdin=stdin)
def source_cmd(args, stdin=None): """Simple cmd.exe-specific wrapper around source-foreign.""" args = list(args) fpath = locate_binary(args[0]) args[0] = fpath if fpath else args[0] if not os.path.isfile(args[0]): return (None, "xonsh: error: File not found: {}\n".format(args[0]), 1) prevcmd = "call " prevcmd += " ".join([argvquote(arg, force=True) for arg in args]) prevcmd = escape_windows_cmd_string(prevcmd) args.append("--prevcmd={}".format(prevcmd)) args.insert(0, "cmd") args.append("--interactive=0") args.append("--sourcer=call") args.append("--envcmd=set") args.append("--seterrpostcmd=if errorlevel 1 exit 1") args.append("--use-tmpfile=1") with builtins.__xonsh_env__.swap(PROMPT="$P$G"): return source_foreign(args, stdin=stdin)
def source_cmd(args, stdin=None): """Simple cmd.exe-specific wrapper around source-foreign.""" args = list(args) fpath = locate_binary(args[0]) args[0] = fpath if fpath else args[0] if not os.path.isfile(args[0]): return (None, "xonsh: error: File not found: {}\n".format(args[0]), 1) prevcmd = "call " prevcmd += " ".join([argvquote(arg, force=True) for arg in args]) prevcmd = escape_windows_cmd_string(prevcmd) args.append("--prevcmd={}".format(prevcmd)) args.insert(0, "cmd") args.append("--interactive=0") args.append("--sourcer=call") args.append("--envcmd=set") args.append("--seterrpostcmd=if errorlevel 1 exit 1") args.append("--use-tmpfile=1") with builtins.__xonsh__.env.swap(PROMPT="$P$G"): return source_foreign(args, stdin=stdin)
def settitle(self): """Sets terminal title.""" env = builtins.__xonsh_env__ # pylint: disable=no-member term = env.get('TERM', None) # Shells running in emacs sets TERM to "dumb" or "eterm-color". # Do not set title for these to avoid garbled prompt. if (term is None and not ON_WINDOWS) or term in ['dumb', 'eterm-color', 'linux']: return t = env.get('TITLE') if t is None: return t = partial_format_prompt(t) if ON_WINDOWS and 'ANSICON' not in env: t = escape_windows_cmd_string(t) os.system('title {}'.format(t)) else: with open(1, 'wb', closefd=False) as f: # prevent xonsh from answering interative questions # on the next command by writing the title f.write("\x1b]2;{0}\x07".format(t).encode()) f.flush()
def settitle(self): """Sets terminal title.""" env = builtins.__xonsh_env__ # pylint: disable=no-member term = env.get('TERM', None) # Shells running in emacs sets TERM to "dumb" or "eterm-color". # Do not set title for these to avoid garbled prompt. if (term is None and not ON_WINDOWS) or term in [ 'dumb', 'eterm-color', 'linux' ]: return t = env.get('TITLE') if t is None: return t = partial_format_prompt(t) if ON_WINDOWS and 'ANSICON' not in env: t = escape_windows_cmd_string(t) os.system('title {}'.format(t)) else: with open(1, 'wb', closefd=False) as f: # prevent xonsh from answering interative questions # on the next command by writing the title f.write("\x1b]0;{0}\x07".format(t).encode()) f.flush()
def test_escape_windows_cmd_string(st, esc): obs = escape_windows_cmd_string(st) assert esc == obs
def source_cmd_fn( files: Annotated[tp.List[str], Arg(nargs="+")], login=False, aliascmd=None, extra_args="", safe=True, postcmd="", funcscmd="", seterrprevcmd=None, overwrite_aliases=False, suppress_skip_message=False, show=False, dryrun=False, _stderr=None, ): """ Source cmd.exe files Parameters ---------- files paths to source files. login : -l, --login whether the sourced shell should be login envcmd : --envcmd command to print environment aliascmd : --aliascmd command to print aliases extra_args : --extra-args extra arguments needed to run the shell safe : -s, --safe whether the source shell should be run safely, and not raise any errors, even if they occur. postcmd : --postcmd command(s) to run after all other commands funcscmd : --funcscmd code to find locations of all native functions in the shell language. seterrprevcmd : --seterrprevcmd command(s) to set exit-on-error before any other commands. overwrite_aliases : --overwrite-aliases flag for whether or not sourced aliases should replace the current xonsh aliases. suppress_skip_message : --suppress-skip-message flag for whether or not skip messages should be suppressed. show : --show show the script output. dryrun : -d, --dry-run Will not actually source the file. """ args = list(files) fpath = locate_binary(args[0]) args[0] = fpath if fpath else args[0] if not os.path.isfile(args[0]): return (None, f"xonsh: error: File not found: {args[0]}\n", 1) prevcmd = "call " prevcmd += " ".join([argvquote(arg, force=True) for arg in args]) prevcmd = escape_windows_cmd_string(prevcmd) with XSH.env.swap(PROMPT="$P$G"): return source_foreign_fn( shell="cmd", files_or_code=args, interactive=True, sourcer="call", envcmd="set", seterrpostcmd="if errorlevel 1 exit 1", use_tmpfile=True, prevcmd=prevcmd, # from this function login=login, aliascmd=aliascmd, extra_args=extra_args, safe=safe, postcmd=postcmd, funcscmd=funcscmd, seterrprevcmd=seterrprevcmd, overwrite_aliases=overwrite_aliases, suppress_skip_message=suppress_skip_message, show=show, dryrun=dryrun, )