Run dialog

David Majà davidmaja at gmail.com
Mon Jun 12 17:35:20 EDT 2006


Hello,
   I have my own dialog to run applications, I make it with python and
pyQt, this is the program, if anybody want to try: (execute: ./pyqtrun
or python pyqtrun)

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Filename : pyqtrun
# Author : David Majà Martínez <davidmaja at gmail.com>

import sys
import os
import string
from qt import *

class Run(QWidget):
	__history = None
	__leng = 0
	def __init__(self, *args):
		"""Initialize the QWidget"""
		QWidget.__init__(self, *args)
		self.setCaption("PyQtRun - 0.30")
		self.setMinimumWidth(350)
		self.layout = QGridLayout(self, 1, 1, 5, 5)
		# Initialize history class #
		self.__history = History()
		# Add QLineEdit txtCmd #
		self.txtCmd = QLineEdit(self)
		self.layout.addMultiCellWidget(self.txtCmd, 0, 0, 0, 1, 0)
		self.connect(self.txtCmd, SIGNAL("textChanged( const QString & )"),
self.autoComplete)
		self.connect(self.txtCmd, SIGNAL("returnPressed()"), self.runCommand)
	def runCommand(self):
		"""Run command especified on the text box and store in the
		history file."""
		text = str(self.txtCmd.text()).strip()
		cmd = text
		if cmd != "":
			if string.rfind(cmd, "&") != (len(cmd) - 1):
				cmd = cmd + '&'
			os.system(cmd)
			self.__history.append(text)
		self.close(True)
	def keyPressEvent(self, event):
		"""Over-ride event keyPressEvent.
		Close application when Esc key is pressed."""
		if event.key() == Qt.Key_Escape:
			self.close(True)
	def autoComplete(self, text):
		"""Complete de command with the commands stored in the
		history file."""
		text = str(text)
		if text != "" and len(text) > self.__leng:
			cmd = self.__history.findCommand(text)
			if cmd != None:
				self.txtCmd.setText(cmd)
				self.txtCmd.setSelection(len(text), len(cmd))
		self.__leng = len(text)
	def cleanAll(self):
		"""Remove all the text in textCmd"""
		self.txtCmd.setText("")

class History:
	__hfile = None
	def __init__(self):
		"""Initialize the History class. Sets the name of the history
		file."""
		self.__hfile = os.path.expanduser("~") + os.sep + ".pyqtrun"
	def findCommand(self, cmd):
		equal = None
		if cmd != '' and os.path.exists(self.__hfile):
			histfile = open(self.__hfile, 'r')
			for line in histfile:
				if string.find(line, cmd) == 0:
					line = string.replace(line, "\n", "")
					equal = line
					break
			histfile.close()
		return equal
	def findEqual(self, cmd):
		equal = None
		if cmd != '' and os.path.exists(self.__hfile):
			histfile = open(self.__hfile, 'r')
			for line in histfile:
				line = string.replace(line, "\n", "")
				if line == cmd:
					equal = line
					break
			histfile.close()
		return equal
	def append(self, cmd):
		"""Append a new command at the end of history file."""
		if os.path.exists(self.__hfile):
			if self.findEqual(cmd) == None:
				histfile = open(self.__hfile, 'a')
				cmd = cmd + '\n'
				histfile.writelines(cmd)
				histfile.flush()
				histfile.close()
		else:
			histfile = open(self.__hfile, 'w')
			cmd = cmd + '\n'
			histfile.writelines(cmd)
			histfile.flush()
			histfile.close()

if __name__ == '__main__' :
	if "--help" in sys.argv:
		print "syntax: pyqtrun"
		sys.exit(1)

	app = QApplication(sys.argv)
	app.connect(app, SIGNAL('lastWindowClosed()'), app, SLOT('quit()'))
	run = Run()
	run.show()
	app.setMainWidget(run)
	app.exec_loop()


On 6/12/06, Mikael Magnusson <mangosoft at comhem.se> wrote:
> On Mon, 12 Jun 2006, Rafal Muzylo wrote:
>
> > On Mon, Jun 12, 2006 at 01:37:18AM +0200, Mikael Magnusson wrote:
> >> The other week, I wrote a little shellcode and made a keybinding to turn
> >> my terminal and zsh into the fastest and bestest run dialog ever.
> >> rc.xml:
> >>   <keybind key="W-r">
> >>     <action name="execute"><execute>sh -c 'ZSHRUN=1 urxvt -geometry
> >>     100x5+335+446'</execute></action>
> >>   </keybind>
> >>
> >> .zshrc:
> >> #change the part where you assign HISTFILE to this
> >> if [[ -n "$ZSHRUN" ]]
> >> then
> >>   HISTFILE=~/.zrunhistory
> >> else
> >>   HISTFILE=~/.history
> >> fi
> >>
> >> #put this somewhere
> >> if [[ -n "$ZSHRUN" ]]; then
> >>   unset ZSHRUN
> >>   function _accept_and_quit() {
> >>     zsh -c "${BUFFER}" &|
> >>     exit
> >>   }
> >>   zle -N _accept_and_quit
> >>   bindkey "^M" _accept_and_quit
> >> fi
> >> #if you want to run a command, like cd, without closing the dialog, you
> >> #can press ctrl-j instead.
> >>
> >> #if you have any time consuming commands or ones that print info about
> new
> >> #mail etc, wrap them like this:
> >> if [[ ! -n "$ZSHRUN" ]]; then
> >>   echo hi
> >>   PS1=elaborate\ prompt
> >> else
> >>   PS1="zshrun %~> "
> >> fi
> >>
> > Are there any major changes needed, if you want to use that with bash,
> > cause I'm using gmrun too, and though the slow part I can handle, it's
> > something else that is a problem. I often need to run a program with
> > some shell variable set (most often it's LD_LIBRARY_PATH), and gmrum
> > does not allow it. BTW. I'm using urxvt too (like it, too).
>
> I suppose the bindkey part needs replacing, but not sure how to do that in
> bash.
>
> --
> Mikael Magnusson
>


-- 
David Majà Martínez
davidmaja at gmail.com



More information about the openbox mailing list