OILS / builtin / method_io.py View on Github | oilshell.org

43 lines, 23 significant
1"""Methods on IO type"""
2from __future__ import print_function
3
4from _devbuild.gen.value_asdl import value, value_t
5
6from core import error
7from core import vm
8from mycpp.mylib import log
9from osh import prompt
10
11from typing import cast, TYPE_CHECKING
12if TYPE_CHECKING:
13 from frontend import typed_args
14
15_ = log
16
17
18class PromptVal(vm._Callable):
19 """
20 _io->promptVal('$') is like \$
21 It expands to $ or # when root
22 """
23
24 def __init__(self):
25 # type: () -> None
26 pass
27
28 def Call(self, rd):
29 # type: (typed_args.Reader) -> value_t
30
31 # "self" param is guaranteed to succeed
32 io = rd.PosIO()
33 what = rd.PosStr()
34 rd.Done() # no more args
35
36 # Bug fix: protect against crash later in PromptVal()
37 if len(what) != 1:
38 raise error.Expr(
39 'promptVal() expected a single char, got %r' % what,
40 rd.LeftParenToken())
41
42 prompt_ev = cast(prompt.Evaluator, io.prompt_ev)
43 return value.Str(prompt_ev.PromptVal(what))