Skip to content

Commit

Permalink
Improve (#24)
Browse files Browse the repository at this point in the history
* Add files via upload

* use ```communicate()``` instead of ```wait()```

* Update tkterm.py

revert

* Spend a lot of time to find it LOL

* remove debug

* avoid do a lot of stuff if cmd is none

* Small tweaks

* Update .editorconfig

* ..

* Update .editorconfig

* fix

* emm

* small cleanup

* Bump version to prepare for the upload

* abab

* I remember that

* Update tktermwidget/tkterm.py

* Update .editorconfig

* Add type

* Update tkterm.py

* Update tkterm.py
  • Loading branch information
littlewhitecloud authored Jun 11, 2023
1 parent ed7b231 commit 3d8e002
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 30 deletions.
10 changes: 9 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@ root = true
charset = utf-8
insert_final_newline = true

[*.py]
# Special file
[tktermwidget/term.py]
indent_style = space
indent_size = 4
end_of_line = lf
trim_trailing_whitespace = true
max_line_length = 125

[tktermwidget/__init__.py]
indent_style = space
indent_size = 4
end_of_line = lf
trim_trailing_whitespace = true
max_line_length = 88
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# `tkterm`

> A terminal emulator written in Python using tkinter
# TkTerminal
A terminal emulator written in Python using tkinter

## Views:
### Windows
Expand Down Expand Up @@ -56,6 +55,3 @@ root.mainloop()
```batch
pip install tktermwidget
```

### Coming soon

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="tktermwidget",
version="0.0.2",
version="0.0.3",
description="A terminal emulator for Tkinter",
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
49 changes: 27 additions & 22 deletions tktermwidget/tkterm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Terminal widget for tkinter"""
from __future__ import annotations

from os import getcwd
Expand All @@ -10,7 +11,6 @@
from platformdirs import user_cache_dir

# Set constants

HISTORY_PATH = Path(user_cache_dir("tkterm"))
SYSTEM = system()
CREATE_NEW_CONSOLE = 0
Expand All @@ -30,21 +30,18 @@
if not (HISTORY_PATH / "history.txt").exists():
open(HISTORY_PATH / "history.txt", "w").close()


class AutoHideScrollbar(Scrollbar):
"""Scrollbar that automatically hides when not needed"""

def __init__(self, master=None, **kwargs):
Scrollbar.__init__(self, master=master, **kwargs)

def set(self, length, height):
if float(length) <= 0.0 and float(height) >= 1.0:
def set(self, first: int, last: int):
if float(first) <= 0.0 and float(last) >= 1.0:
self.grid_remove()
else:
self.grid()

Scrollbar.set(self, length, height)

Scrollbar.set(self, first, last)

class Terminal(Frame):
"""A terminal widget for tkinter applications
Expand All @@ -60,8 +57,10 @@ class Terminal(Frame):
Methods for internal use:
up (Event) -> str: Goes up in the history
down (Event) -> str: Goes down in the history (if the user is at the bottom of the history, it clears the command)
left (Event) -> str: Goes left in the command if the index is greater than the length of the directory (so the user can't delete the directory or go left of it)
down (Event) -> str: Goes down in the history
(if the user is at the bottom of the history, it clears the command)
left (Event) -> str: Goes left in the command if the index is greater than the length of the directory
(so the user can't delete the directory or go left of it)
kill (Event) -> str: Kills the current command
loop (Event) -> str: Runs the command typed"""

Expand Down Expand Up @@ -104,21 +103,22 @@ def __init__(self, master: Misc, autohide: bool = True, *args, **kwargs):
self.index = 1
self.current_process: Popen | None = None

# Bind events
# Bind events & tags
self.text.bind("<Up>", self.up, add=True)
self.text.bind("<Down>", self.down, add=True)
self.text.bind("<Left>", self.left, add=True)
self.text.bind("<Return>", self.loop, add=True)
self.text.bind("<BackSpace>", self.left, add=True)

# TODO: Refactor the way we get output from subprocess
self.text.bind("<Control-KeyPress-c>", self.kill, add=True) # Isn't working

# History recorder
self.history = open(HISTORY_PATH / "history.txt", "r+")
self.historys = [i.strip() for i in self.history.readlines() if i.strip()]
self.hi = len(self.historys) - 1



def directory(self):
"""Insert the directory"""
self.text.insert(
Expand Down Expand Up @@ -171,15 +171,18 @@ def loop(self, _: Event) -> str:
"""Create an input loop"""
cmd = self.text.get(f"{self.index}.0", "end-1c")
# Determine command based on system
cmd = cmd.split("$")[-1] # Unix
if SYSTEM == "Windows":
cmd = cmd.split(">")[-1].strip()
cmd = cmd.split("$")[-1].strip() if not SYSTEM == "Windows" else cmd.split(">")[-1].strip()

# Record the command
if cmd != "":
self.history.write(cmd + "\n")
self.historys.append(cmd)
self.hi = len(self.historys) - 1
else:
self.text.insert("insert", "\n")
self.index += 1
self.directory()
return "break"

# Check that the insert position is at the end
if self.text.index("insert") != f"{self.index}.end":
Expand All @@ -199,23 +202,25 @@ def loop(self, _: Event) -> str:
stderr=PIPE,
stdin=PIPE,
text=True,
cwd=getcwd(), # Until a solution for changing the working directory is found, this will have to do
cwd=getcwd(), # Until a solution for changing the working directory is found, this will have to do
creationflags=CREATE_NEW_CONSOLE,
) # The following needs to be put in an after so the kill command works and the program doesn't freeze
)
# The following needs to be put in an after so the kill command works and the program doesn't freeze

# Check if the command was successful
returncode = self.current_process.wait()
process = self.current_process
returnlines, errors, = self.current_process.communicate()
returncode = self.current_process.returncode
self.current_process = None
returnlines = process.stdout.readlines()
if returncode != 0:
returnlines += process.stderr.readlines() # If the command was unsuccessful, it doesn't give stdout
returnlines += errors # If the command was unsuccessful, it doesn't give stdout
# TODO: Get the success message from the command (see #16)

self.text.insert("insert", "\n")
self.index += 1
for line in returnlines:
self.text.insert("insert", line)
self.index += 1
if line == "\n":
self.index += 1

self.directory()
return "break" # Prevent the default newline character insertion
Expand Down

0 comments on commit 3d8e002

Please sign in to comment.