Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save Code Only #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions dreampielib/data/dreampie.glade
Original file line number Diff line number Diff line change
Expand Up @@ -1527,6 +1527,16 @@ You can also set the __expects_str__ attribute of a function to True to achieve
<signal name="activate" handler="on_copy_commands_only"/>
</widget>
</child>
<child>
<widget class="GtkMenuItem" id="save_commands_only_popmenu">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">Save Code Only</property>
<property name="use_underline">True</property>
<signal name="activate" handler="on_save_commands_only"/>
</widget>
</child>
</widget>
<widget class="GtkWindow" id="window_main">
<property name="can_focus">False</property>
Expand Down Expand Up @@ -1798,6 +1808,19 @@ You can also set the __expects_str__ attribute of a function to True to achieve
<accelerator key="c" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</widget>
</child>
<child>
<widget class="GtkMenuItem" id="menuitem_save_commands_only">
<property name="visible">True</property>
<property name="sensitive">False</property>
<property name="can_focus">False</property>
<property name="tooltip" translatable="yes">Save only the code, without output or leading dots.</property>
<property name="use_action_appearance">False</property>
<property name="label" translatable="yes">Save Code Only</property>
<property name="use_underline">True</property>
<signal name="activate" handler="on_save_commands_only"/>
<accelerator key="c" signal="activate" modifiers="GDK_SHIFT_MASK | GDK_CONTROL_MASK"/>
</widget>
</child>
<child>
<widget class="GtkImageMenuItem" id="menuitem_paste">
<property name="label">gtk-paste</property>
Expand Down
11 changes: 11 additions & 0 deletions dreampielib/gui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,24 @@ def on_copy(self, _widget):
def on_copy_commands_only(self, _widget):
return self.selection.copy_commands_only()

def on_save_commands_only(self, _widget):
"""Prompts for a filename and saves only the selected commands
to the file"""

def save_code(filename):
self.selection.save_commands_only(filename)

save_dialog(save_code, _("Choose where to save the code"),
self.main_widget, _("Python Files"), "*.py", None)

def on_paste(self, _widget):
return self.selection.paste()

def on_is_something_selected_changed(self, is_something_selected):
self.menuitem_cut.props.sensitive = is_something_selected
self.menuitem_copy.props.sensitive = is_something_selected
self.menuitem_copy_commands_only.props.sensitive = is_something_selected
self.menuitem_save_commands_only.props.sensitive = is_something_selected
self.menuitem_interrupt.props.sensitive = not is_something_selected

# Source buffer, Text buffer
Expand Down
17 changes: 16 additions & 1 deletion dreampielib/gui/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def copy(self):
else:
beep()

def copy_commands_only(self):
def commands_only(self):
if self.sourcebuffer.get_has_selection():
self.sourcebuffer.copy_clipboard(self.clipboard)
return
Expand All @@ -109,11 +109,26 @@ def copy_commands_only(self):
r.append(get_text(tb, it, it2))
it = it2
r = ''.join(r)
return r
def copy_commands_only(self):
r = self.commands_only()
if not r:
beep()
else:
self.clipboard.set_text(r)

def save_commands_only(self, filename):
"""Save only the selected commands to the specified filename;
if no commands are selected, beep and forget it"""

r = self.commands_only()

if not r:
beep()
else:
with open(filename, "w") as f:
f.write(r)

def paste(self):
if self.sourceview.is_focus():
self.sourcebuffer.paste_clipboard(self.clipboard, None, True)
Expand Down