Skip to content

Commit

Permalink
feat: New terminals are opened at active directory (or $HOME if none)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomlin7 committed Aug 7, 2023
1 parent 7b10577 commit 054c155
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 16 deletions.
6 changes: 6 additions & 0 deletions biscuit/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ def setup_references(self):
self.statusbar = self.root.statusbar
self.editorsmanager = self.root.baseframe.contentpane.editorspane
self.panel = self.root.baseframe.contentpane.panel
self.terminalmanager = self.panel.terminal
self.contentpane = self.root.baseframe.contentpane
self.sidebar = self.root.baseframe.sidebar
self.explorer = self.sidebar.explorer
Expand Down Expand Up @@ -163,6 +164,11 @@ def open_directory(self, dir):
self.active_directory = dir
self.explorer.directory.change_path(dir)
self.set_title(os.path.basename(self.active_directory))

self.editorsmanager.delete_all_editors()
self.terminalmanager.delete_all_terminals()
self.terminalmanager.open_terminal()

self.git.check_git()
self.update_git()

Expand Down
36 changes: 24 additions & 12 deletions biscuit/core/components/views/panel/terminal/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import tkinter as tk

from ..panelview import PanelView
Expand All @@ -6,6 +7,13 @@
from .tabs import Tabs


def get_home_directory():
if os.name == 'nt':
return os.path.expandvars("%USERPROFILE%")
if os.name == 'posix':
return os.path.expanduser("~")
return '.'
print(get_home_directory())
class Terminal(PanelView):
def __init__(self, master, *args, **kwargs):
super().__init__(master, *args, **kwargs)
Expand All @@ -25,12 +33,12 @@ def __init__(self, master, *args, **kwargs):

self.terminals = []

self.default_terminals = Default(self)
self.add_terminal(self.default_terminals)
self.default_terminal = Default(self, cwd=self.base.active_directory or get_home_directory())
self.add_terminal(self.default_terminal)

def add_current_terminal(self, *_):
"Spawns an instance of currently active terminal"
self.add_terminal(self.active_terminal_type(self))
self.add_terminal(self.active_terminal_type(self, cwd=self.base.active_directory or get_home_directory()))

def add_terminals(self, terminals):
"Append terminals to list. Create tabs for them."
Expand All @@ -41,12 +49,16 @@ def add_terminal(self, terminal):
"Appends a terminal to list. Create a tab."
self.terminals.append(terminal)
self.tabs.add_tab(terminal)

def open_terminal(self, path=None):
self.add_terminal(self.active_terminal_type(self, cwd=path or self.base.active_directory or get_home_directory()))

def delete_all_terminals(self):
"Permanently delete all terminals."
for terminal in self.terminals:
terminal.destroy()

self.tabs.clear_all_tabs()
self.terminals.clear()

def delete_terminal(self, terminal):
Expand All @@ -68,17 +80,17 @@ def clear_terminal(self, *_):
if active := self.active_terminal:
active.clear()

@property
def pwsh(self):
return self.default_terminals[0]
# @property
# def pwsh(self):
# return self.default_terminals[0]

@property
def cmd(self):
return self.default_terminals[1]
# @property
# def cmd(self):
# return self.default_terminals[1]

@property
def python(self):
return self.default_terminals[1]
# @property
# def python(self):
# return self.default_terminals[1]

@property
def active_terminal_type(self):
Expand Down
3 changes: 2 additions & 1 deletion biscuit/core/components/views/panel/terminal/tabs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import tkinter as tk

from .tab import Tab
from biscuit.core.components.utils import Frame

from .tab import Tab


class Tabs(Frame):
def __init__(self, master, width=150, *args, **kwargs):
Expand Down
8 changes: 5 additions & 3 deletions biscuit/core/components/views/panel/terminal/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Terminal(PanelView):
args:
shell - the shell executable
cwd - current directory
methods:
start_service - start the terminal service
Expand All @@ -26,7 +27,7 @@ class Terminal(PanelView):
name: str
icon: str

def __init__(self, master, shell=None, *args, **kwargs):
def __init__(self, master, shell=None, cwd=".", *args, **kwargs):
super().__init__(master, *args, **kwargs)
self.__buttons__ = (('add',),('trash', self.destroy))

Expand All @@ -35,7 +36,8 @@ def __init__(self, master, shell=None, *args, **kwargs):

self.alive = False
self.shell = shell

self.cwd = cwd

self.terminal = TerminalText(self, relief=tk.FLAT, padx=10, pady=10, font=("Consolas", 11))
self.terminal.grid(row=0, column=0, sticky=tk.NSEW)
self.terminal.bind("<Return>", self.enter)
Expand All @@ -54,7 +56,7 @@ def start_service(self, *_):
self.last_command = None

self.p = subprocess.Popen(
self.shell, stdout=subprocess.PIPE,
self.shell, stdout=subprocess.PIPE, cwd=self.cwd,
stdin=subprocess.PIPE, stderr=subprocess.PIPE)

self.out_queue = queue.Queue()
Expand Down

0 comments on commit 054c155

Please sign in to comment.