|
1 | 1 | """
|
2 |
| -Copyright (c) 2020, 2023, Oracle and/or its affiliates. |
| 2 | +Copyright (c) 2020, 2024, Oracle and/or its affiliates. |
3 | 3 | Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
|
4 | 4 | """
|
5 | 5 | import re
|
| 6 | +import sys |
6 | 7 |
|
7 |
| -from org.jline.terminal import TerminalBuilder |
8 |
| -from org.jline.reader import LineReaderBuilder |
9 |
| -from org.jline.reader.impl.completer import StringsCompleter |
10 | 8 | from oracle.weblogic.deploy.util import CLAException
|
11 | 9 |
|
12 |
| -# Jython tools don't require sys.path modification |
13 | 10 | from wlsdeploy.exception import exception_helper
|
14 | 11 | from wlsdeploy.tool.modelhelp.model_help_printer import ModelHelpPrinter
|
15 | 12 | from wlsdeploy.tool.modelhelp.model_help_utils import ControlOptions
|
16 | 13 | from wlsdeploy.tool.modelhelp.model_help_utils import PathOptions
|
17 |
| -from wlsdeploy.util.exit_code import ExitCode |
18 | 14 | from wlsdeploy.util import model
|
| 15 | +from wlsdeploy.util import string_utils |
| 16 | +from wlsdeploy.util.exit_code import ExitCode |
19 | 17 |
|
20 | 18 | _class_name = 'ModelHelpInteractivePrinter'
|
21 | 19 | SIMPLE_COMMAND_REGEX = re.compile(r'^top$|^(cd|ls|cat)(?:\s+(\.\.(?:/[a-zA-Z0-9#.]+)*/?))?$')
|
@@ -43,13 +41,23 @@ def interactive_help_main_loop(self):
|
43 | 41 | self._output_buffer.add_output()
|
44 | 42 | self._output_buffer.add_message('WLSDPLY-10119', model_path)
|
45 | 43 |
|
46 |
| - completer = StringsCompleter(['cat', 'cd', 'ls', 'top', 'exit']) |
47 |
| - terminal = TerminalBuilder.terminal() |
48 |
| - reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).build() |
| 44 | + # JLine requires Java 1.8.0 |
| 45 | + reader = None |
| 46 | + if string_utils.is_java_version_or_above('1.8.0'): |
| 47 | + from org.jline.terminal import TerminalBuilder |
| 48 | + from org.jline.reader import LineReaderBuilder |
| 49 | + from org.jline.reader.impl.completer import StringsCompleter |
| 50 | + |
| 51 | + completer = StringsCompleter(['cat', 'cd', 'ls', 'top', 'exit']) |
| 52 | + terminal = TerminalBuilder.terminal() |
| 53 | + reader = LineReaderBuilder.builder().terminal(terminal).completer(completer).build() |
| 54 | + |
49 | 55 | while True:
|
50 | 56 | if command_str == 'exit':
|
51 | 57 | break
|
52 |
| - reader.getHistory().add(command_str) |
| 58 | + |
| 59 | + if reader: |
| 60 | + reader.getHistory().add(command_str) |
53 | 61 |
|
54 | 62 | # the "process command" prints the help (or error) for the command_str
|
55 | 63 | # plus appends a new path to the history if the str specifies a successful directory change
|
@@ -280,13 +288,21 @@ def _add_message(self, key, *args, **kwargs):
|
280 | 288 |
|
281 | 289 | def _interactive_help_prompt(model_path, reader):
|
282 | 290 | """
|
283 |
| - Gets the next command from stdin or a file. |
| 291 | + Gets the next command from stdin or using JLine. |
284 | 292 | :param model_path: a current model path
|
285 |
| - :param reader: JLine reader |
286 |
| - :param printer: a model help printer |
| 293 | + :param reader: JLine reader, or None if JLine not supported |
287 | 294 | :return: returns when user types 'exit'
|
288 | 295 | """
|
289 |
| - command_str = reader.readLine('[%s] --> ' % model_path) |
| 296 | + prompt = '[%s] --> ' % model_path |
| 297 | + |
| 298 | + if reader: |
| 299 | + command_str = reader.readLine(prompt) |
| 300 | + else: |
| 301 | + # prompt using sys.stdout.write to avoid newline |
| 302 | + sys.stdout.write(prompt) |
| 303 | + sys.stdout.flush() |
| 304 | + command_str = raw_input('') # get command from stdin |
| 305 | + |
290 | 306 | command_str = ' '.join(command_str.split()) # remove extra white-space
|
291 | 307 | return command_str
|
292 | 308 |
|
|
0 commit comments