Skip to content

Commit

Permalink
Merge pull request #40 from andfoy/cwd_update
Browse files Browse the repository at this point in the history
New terminals inherit Spyder current working directory
  • Loading branch information
andfoy authored Apr 11, 2017
2 parents f5a5bf3 + 54ba744 commit 51433e1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
20 changes: 19 additions & 1 deletion spyder_terminal/server/static/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var term,
socket,
pid,
charWidth,
charHeight;
charHeight,
path;

var terminalContainer = document.getElementById('terminal-container');

Expand Down Expand Up @@ -98,6 +99,20 @@ function fitFont(font) {
setFont(font);
}

function setcwd(cwd) {
console.log(cwd);
path = cwd;
}

function chdir(path) {
term.send('cd '+path+'\n');
}

function clearTerm()
{
term.send('clear\n');
}

function closeTerm() {
console.log("Closed via server");
term.writeln("Pipe closed")
Expand All @@ -107,4 +122,7 @@ function runRealTerminal() {
term.attach(socket);
console.log("Am I Alive?");
term._initialized = true;
term.writeln("Loading...");
chdir(path);
clearTerm();
}
5 changes: 4 additions & 1 deletion spyder_terminal/terminalplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
from spyder.py3compat import is_text_string, to_text_string
from spyder.utils.misc import select_port

from spyder.py3compat import getcwd

LOCATION = osp.realpath(osp.join(os.getcwd(),
osp.dirname(__file__)))

Expand Down Expand Up @@ -185,7 +187,8 @@ def get_current_term(self):
def create_new_term(self, name=None, give_focus=True):
"""Add a new terminal tab."""
font = self.get_plugin_font()
term = TerminalWidget(self, self.port, font=font.family())
term = TerminalWidget(self, self.port, path=getcwd(),
font=font.family())
self.add_tab(term)

def close_term(self, index=None, term=None):
Expand Down
2 changes: 1 addition & 1 deletion spyder_terminal/tests/test_terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
LOCATION = os.path.realpath(os.path.join(os.getcwd(),
os.path.dirname(__file__)))

TERM_UP = 10000
TERM_UP = 15000


def check_pwd(termwidget):
Expand Down
8 changes: 7 additions & 1 deletion spyder_terminal/widgets/terminalgui.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
class TerminalWidget(QFrame):
"""Terminal widget."""

def __init__(self, parent, port, font=None):
def __init__(self, parent, port, path='~', font=None):
"""Frame main constructor."""
QWidget.__init__(self, parent)
url = 'http://127.0.0.1:{0}'.format(port)
self.view = TermView(self, term_url=url)
self.font = font
self.initial_path = path

layout = QVBoxLayout()
layout.addWidget(self.view)
Expand Down Expand Up @@ -61,6 +62,7 @@ def setup_term(self, finished):
# This forces to display the black background
print("\0", end='')
self.set_font(self.font)
self.set_dir(self.initial_path)

def eval_javascript(self, script):
"""Evaluate Javascript instructions inside view."""
Expand All @@ -69,6 +71,10 @@ def eval_javascript(self, script):
else:
return self.body.evaluateJavaScript("{}".format(script))

def set_dir(self, path):
"""Set terminal initial current working directory."""
self.eval_javascript('setcwd("{0}")'.format(path))

def set_font(self, font):
"""Set terminal font via CSS."""
self.font = font
Expand Down

0 comments on commit 51433e1

Please sign in to comment.