From 3fc206b0c69e1b973695a3e4846ef7299c72abf5 Mon Sep 17 00:00:00 2001 From: Victor Mylle Date: Fri, 2 Dec 2022 12:19:28 +0100 Subject: [PATCH] Fixed the edit option --- copilot.py | 38 +++++++++++++++++++++++++++++++++++++- token.json | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/copilot.py b/copilot.py index e0e59a3..6abf201 100755 --- a/copilot.py +++ b/copilot.py @@ -5,6 +5,18 @@ import sys import time from os.path import expanduser import click +from rich import print +from rich.panel import Panel +from rich.text import Text +import signal +import subprocess +from threading import Thread +from pynput.keyboard import Key, Controller +try: + import gnureadline as readline +except ImportError: + import readline + # get a new github copilot token @@ -117,4 +129,28 @@ def generate_suggestion(prompt): prompt = " ".join(prompt) prompt = "!/bin/bash\n\n" + prompt + ":\n" token = get_token() - print(get_suggestion(prompt, token)) \ No newline at end of file + + suggestion = get_suggestion(prompt, token) + panel = Panel(Text(suggestion, justify="center"), title_align="center", title="Accept? \[y/n/e]", expand=False) + print(panel) + # read a character from stdin without displaying it + c = click.getchar() + if c == "y": + # execute the suggestion + os.system(suggestion) + elif c == "e": + # prompt the suggestion by using redisplay + def pre_input_hook(): + readline.insert_text(suggestion) + readline.redisplay() + + + readline.set_pre_input_hook(pre_input_hook) + line = input() + + # execute line + os.system(line) + # remove pre input hook + readline.set_pre_input_hook() + + diff --git a/token.json b/token.json index bc94338..66720aa 100644 --- a/token.json +++ b/token.json @@ -1 +1 @@ -{"expires_at": 1669917293, "public_suggestions": "disabled", "refresh_in": 1500, "sku": "free_educational", "telemetry": "disabled", "token": "tid=d47460691ef58366f713e508d59d807b;exp=1669917293;sku=free_educational:f9553e702e2a159aa3459e09a3b0b2c0ca83a11c4042ea928c8af5cf8732063b", "tracking_id": "d47460691ef58366f713e508d59d807b"} \ No newline at end of file +{"expires_at": 1669980922, "public_suggestions": "disabled", "refresh_in": 1500, "sku": "free_educational", "telemetry": "disabled", "token": "tid=d47460691ef58366f713e508d59d807b;exp=1669980922;sku=free_educational:fb44337ee996917b43d51aa8bd1553ecce5ef1d7880333106ee25689648ca133", "tracking_id": "d47460691ef58366f713e508d59d807b"} \ No newline at end of file