Skip to content

Commit

Permalink
supported line commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Lifailon committed Jul 25, 2024
1 parent 700d621 commit b9d3d1c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ I created this little project to make working with the console easier and faster
- [X] Integration with [cheat.sh](https://cheat.sh). Auto-complete search for executable commands using the `!` or output cheat sheets for the last command entered in the line.
- [ ] Interactive [grep](https://www.gnu.org/software/grep). Performs a filtered search based on the output of the last command executed when the `@` character is used at the beginning of the input line. This can be used as an alternative to grep, which needs to be called every time the text in the filter query changes, or if you have previously used a soft terminal search but the output may have gone beyond it.

#### 💡 To do:
### 💡 To do:

- [ ] Processing all commands in **one bash process**.
- [ ] Automatic addition of options and keys for commands after the `-` character at the end of a line.

#### ⌨️ Hotkeys:
### ⌨️ Hotkeys:

- `right` – select a command without executing it, which is convenient for continuing recording or moving to the next directory to quickly display its contents.
- `backspace` - updates history to reflect changes.
Expand Down
55 changes: 42 additions & 13 deletions src/insh.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ def get_completions(self, document, complete_event):
# Фиксируем текущий текст в поле ввода
text = document.text

# Ничего не делаем, если текст пустой
if not text:
# Ничего не делаем, если текст пустой или содержит только пробелы
if not text or text.isspace():
return

# Логика автодополнения для команды cd
if text.startswith('cd '):
# Извлекаем запрос (удаляем команду cd и лишние пробелы по краям)
Expand Down Expand Up @@ -378,25 +378,54 @@ def get_completions(self, document, complete_event):
display_meta = 'Variable'
)

# Логика вывода подсказок, если в начале строки идет "!" проверяем строку целиком
elif text.startswith('!'):
cur_text = text[1:].strip().lower().replace(" ","-")
for command in command_cheat_list:
command_replace = command.replace("-"," ")
if command.lower().startswith(cur_text.lower()):
yield Completion(f'{command_replace}',
start_position = -len(text),
display = HTML(f'<cyan>{command_replace}</cyan>'),
display_meta = 'Command'
)

# Логика вывода подсказок, если в конце строки идет "!"
elif text.endswith('!'):
# Удаляем символ ! в конце строки, обрезаем пробелы, разбиваем на массив, забираем последнюю команду и опускаем регистр
last_command = text[:-1].strip().split(" ")[-1].lower()
# Проверяем, что последняя команда присутствует в массиве доступных команд и выводим для нее примеры
if last_command in command_cheat_list:
examples = get_command_examples(last_command)
for example in examples:
if example.lower().startswith(last_command):
# Проверяем всю строку
line = text[:-1].strip().lower().replace(" ","-")
# Проверяем последнюю команду
last_command = text[:-1].strip().lower().split(" ")[-1]
# Проверяем, есть ли строка в массиве доступных команд
if line in command_cheat_list:
line = line.replace("-"," ")
examples = get_command_examples(line)
for example in examples:
if example.lower().startswith(line):
if text[-2] == ' ':
start_pos = -len(last_command)-2
start_pos = -len(line)-2
else:
start_pos = -len(last_command)-1
start_pos = -len(line)-1
yield Completion(
text = example,
start_position = start_pos,
display_meta = 'Example'
)
# Если нет, пытаемся по ней выполнить поиск команд для автоматического завершения
# Проверяем, что последняя команда присутствует в массиве команд
elif last_command in command_cheat_list:
examples = get_command_examples(last_command)
for example in examples:
if example.lower().startswith(last_command):
if text[-2] == ' ':
start_pos = -len(last_command)-2
else:
start_pos = -len(last_command)-1
yield Completion(
text = example,
start_position = start_pos,
display_meta = 'Example'
)
# Если примеры не найдены, пытаемся по последней команде выполнить поиск для автоматического завершения
else:
cur_text = text.split()[-1][:-1]
old_text = text.split()[-1]
Expand Down

0 comments on commit b9d3d1c

Please sign in to comment.