In my last blog entry I explained how to send the clipboard content via
KDE Klipper to a pastebin service.
Now I wrote a quick-and-dirty script to transform a URI in the clipboard to a
shortenend version using http://ur1.ca (U R One; it is GPL).
I never figured out how to get automatic URL shortening with http://identi.ca
and Choqok. That’s a kind of universal work-around for me.
Copy the file ur1.rb to a folder which is in your $PATH and add a Klipper
action as I explained in the last post. To setup your Klipper action you will
need the following:
When you are a power IRC user, you might know the problem. You cannot copy the
whole source code, error message or log file etc. directly in the IRC channel.
You need a pastebin. I like http://pastie.org really much. It has a clean
interface and supports highlighting for many languages. But how to copy the text
to the pastebin in a handy and short way?
Do the following to copy the clipboard content to the pastebin by a simple
<Ctrl>+<Alt>+<R> (global shortcut to open Klipper actions) and a click:
Copy the file pastie.rb to a folder which is in your $PATH
Make the file executable for you
Edit the actions of Klipper and add for the .* Regexp (means: no special string)
a new action (do not activate automatic).
Add the command echo '%s' | pastie.rb, feedback should go to clipboard and
set the description to “post as plain text” for instance
You can add another command echo '%s' | pastie.rb -f ruby to paste the text with
ruby syntax highlighting
After that you should be able to send your clipboard to the pastebin with one
selection (to copy text into clipboard), one hotkey (to trigger Klipper actions)
and one click (to choose between different highlighters). You can paste the URL
with a single click on the middle button of your mouse. You don’t even have to
open the pastebin page yourself!
I like it. Just want to share this with you in the case you was locking for
something similiar. :)
pastie.rb
Copy this file to your ~/bin and make it executable.
A big thank to the unknown author of this file. I found it via google on
http://pastie.org and did only some minor modifications on it.
You can use the pastie.rb script via command line by pipe a file to it.
To set the code highlighting use the switch -f LANG. To get all supported
languages you want to try a pastie.rb -h.
#!/usr/bin/env ruby# kate: remove-trailing-space on; replace-trailing-space-save on; indent-width 2; indent-mode ruby; syntax ruby;# file: pastie.rbrequire'net/http'require'optparse'require'timeout'require'cgi'require'uri'classHashdefto_query_stringmap{|k,v|ifv.instance_of?(Hash)v.map{|sk,sv|"#{k}[#{sk}]=#{sv}"}.join('&')else"#{k}=#{v}"end}.join('&')endendmodulePastieAVAILABLE_PARSERS=%w( objective-c++ actionscript ruby ruby_on_rails diff
plain_text c++ css java javascript html html_rails shell shell-unix-generic
sql php python pascal perl yaml csharp go apache lua io lisp d erlang fortran
haskell literate_haskell makefile scala scheme smarty ini nu tex clojure
)classAPIPASTIE_URL=URI.parse"http://pastie.org/pastes"defpaste(body,format='plain_text',is_private=false)raiseInvalidParserunlessvalid_parser?formathttp=Net::HTTP.newPASTIE_URL.host,PASTIE_URL.portquery_string={:paste=>{:body=>CGI.escape(body),:parser=>format,:restricted=>is_private,:authorization=>'burger'}}.to_query_stringresponse,body=http.startdo|http|http.postPASTIE_URL.path,query_stringendraisePastie::Errorunlessresponse.code=='302'response['location']endprivatedefvalid_parser?(format)Pastie::AVAILABLE_PARSERS.include?formatendendclassError<StandardError;endclassInvalidParser<StandardError;endclassConsoleOptionsattr_reader:parser,:optionsdefinitialize@options={:format=>'plain_text',:private=>false}@parser=OptionParser.newdo|cmd|cmd.banner="Ruby Pastie CLI - takes paste input from STDIN"cmd.separator''cmd.on('-h','--help','Displays this help message')doputs@parserexitendcmd.on('-f','--format FORMAT',"The format of the text being pasted. Available parsers: #{Pastie::AVAILABLE_PARSERS.join(', ')}")do|format|@options[:format]=formatendcmd.on('-p','--private','Create a private paste')do@options[:private]=trueendendenddefrunarguments@parser.parse!(arguments)body=''Timeout.timeout(1)dobody+=STDIN.readendifbody.strip.empty?puts"Please pipe in some content to paste on STDIN."exit1endpastie=API.newputspastie.paste(body,@options[:format],@options[:private])exit0rescueInvalidParserputs"Please specify a valid format parser."exit1rescueErrorputs"An unknown error occurred"exit1rescueTimeout::Errorputs"Could not read from STDIN."exit1endendendif($0==__FILE__)app=Pastie::ConsoleOptions.newapp.run(ARGV)end
I really wonder why there are no Konqueror related idea for a GSoC 2010 project
in the KDE Community Wiki.
The last opensuse version ships Firefox as default browser, there are some other
attempts of a kde based browser (rekonq, etc.), but I think that nothing can
replace Konqueror in the next time concerning consumption of resources.
Konqueror is so fast using pdf viewer KPart Okular while Firefox almost hangs up
with proprietary plugin adobe reader. Which other browser can split its view
and show pdf viewer and webpages at the same time? KatePart is also very handy.
I mean it’s wrong to think that other Qt based browsers can replace Konqueror in
the next time. Even the inexperienced user wants to use KPart Okular.
However, I use Firefox, because it doesn’t use kthml has awesome bookmark tagging
and can be synced with Mozilla Weave. It would be nice to see these features
prospectively integrated in Konqueror, too.
What do you think? Can we bring Konqueror to the next level?
To get a shell in your GUI, you don’t have to reinvent the wheel - just use
the KonsolePart provided by KDE KParts. If you use the KDE environment
you already know the libkonsolepart, because Dolphin, Konqueror, Kate, Yakuake,
etc. take usage of it.
To get this small demo run, you need a KDE version already containing SVN
commit 1085699 (ttanks no Arno, who made the Part accessable within ruby).
The opensuse buildservice should provide a package ruby-kde4 in Factory Repo,
but I build I didn’t test it.
Just copy both files in the same directory, make the main.rb executable and run it.
The example contains a little bit more than neccessary, because it implements
a fully customizable menus/toolbars via KXmlWindow and shows how to use
actions and slots.
To get only the KonsolePart widget, you only have to look at the KonsolePart
class. The class itself bases on a short code example from Arno. Kudos to him!
For further explanation just take a look
at the source code.
Usage
You can change the working directory for the built-in shell
by a nice dialog and can execute some command by the lineedit. To send your command
to the shell, you have to press enter.
I know there are a lot of bugs. But it is just a quick and dirty demo.
After some blog posts on my old blog about KDE programming with Ruby
I decided to bring the content a little bit more to the people. Here I am.
Two days before yesterday I got a mail concerning a problem related to creating
plasmoids with Ruby. I never had the wish to create my own plasmoid. I thought
it would be difficult, but while getting a closer look, I’ve noticed how easy it
is - in the case of using Ruby.
Simple Ruby Plasmoid
This example is a modified version from the one in the KDE techbase wiki.
The plasmoid sends the content of a QLineEdit to the clipboard when pressing the
QPushButton or pressing the enter key and clears the QLineEdit afterwards.
Test It
If you want to try it yourself, you just have to extract
simple_ruby_plasmoid_clipboard.tar.gz, change the directory to
ruby-test-applet and start the plasmoid in a special viewer with
plasmoidviewer.
You have to install the Ruby KDE bindings package (on opensuse it is called
ruby-kde4), but on a lot of KDE4 systems this should be installed already.
Understand It
First you need the right directory tree for your plasmoid. It should be look
like this:
Member variables begins with an @ sign. There are different aliases for KDE and
Qt methods. You can omit brackets in a lot of cases. You don’t need any header
files. You don’t need to compile.
require'plasma_applet'moduleRubyTestAppletclassMain<PlasmaScripting::Appletslots:addTextdefinitset_minimum_size150,150@layout=Qt::GraphicsLinearLayout.newQt::Vertical,selfself.layout=@layout@label=Plasma::Label.newself@label.text='This plasmoid will copy the text you enter below to the clipboard.'@layout.add_item@label@line_edit=Plasma::LineEdit.newselfbegin@line_edit.clear_button_shown=true# not supported in early plasma versionsrescuenil# but that doesn't matterend@layout.add_item@line_edit@button=Plasma::PushButton.newself@button.text='Copy to clipboard'@layout.add_item@buttonQt::Object.connect(@button,SIGNAL(:clicked),self,SLOT(:addText))Qt::Object.connect(@line_edit,SIGNAL(:returnPressed),self,SLOT(:addText))enddefaddTextQt::Application.clipboard.text=@line_edit.text@line_edit.text=""endendend# kate: remove_trailing_space on; replace-trailing-space-save on; indent-width 2; indent-mode ruby;
The second file you need is the metadata.desktop containing all the meta data.
[Desktop Entry]Name=Simple Ruby appletComment=This is a simple applet written in RubyIcon=chronometerType=ServiceServiceTypes=Plasma/AppletX-Plasma-API=ruby-scriptX-Plasma-MainScript=code/main.rbX-KDE-PluginInfo-Author=MeX-KDE-PluginInfo-Email=me@example.comX-KDE-PluginInfo-Name=ruby-testX-KDE-PluginInfo-Version=0.1X-KDE-PluginInfo-Website=http://plasma.kde.org/X-KDE-PluginInfo-Category=ExamplesX-KDE-PluginInfo-Depends=X-KDE-PluginInfo-License=GPLX-KDE-PluginInfo-EnabledByDefault=true
metadata.desktop
What do you think? So easy, isn’t it? If you have ever played a little
bit with Ruby and Qt or KDE you should know enough to create your own plasmoid
within a quarter of an hour!