The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
gedit external tools: Ruby helpers, git integration and more

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jan Lelis

Posts: 136
Nickname: rbjl
Registered: Aug, 2009

Jan Lelis is an IT student from Dresden/Germany
gedit external tools: Ruby helpers, git integration and more Posted: May 26, 2010 12:41 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Jan Lelis.
Original Post: gedit external tools: Ruby helpers, git integration and more
Feed Title: rbJ*_*L.net
Feed URL: http://feeds.feedburner.com/rbJL
Feed Description: Hi, I am a fan of Ruby and like to explore it and the world around ;). So I started this blog, where I am publishing code snippets, tutorials for beginners as well as general thoughts about Ruby, the web or programming in general.
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Jan Lelis
Latest Posts From rbJ*_*L.net

Advertisement

With the easy-to-use (and pre-installed) command line utility zenity, it is pretty simple to create useful external tools for gedit!

To use them, just check if the “External Tools” plugin is activated and copy & paste the tools you want into the Tools → Manage External Tools… menu. Alternatively, you can download all files, copy them to ~/.gnome2/gedit/tools and they are automatically recognised by the plugin. Make sure to adjust the shortcuts if you choose the second approach.

As I said, zenity is a wonderful easy tool, so don’t hesitate to edit the scripts, for example, if the width of a zenity popup does not suit your screen settings.

Ruby helpers

[ruby] check syntax

Let’s begin with a simple syntax checker. Hit the shortcut and a little popup will tell you the truth.

#!/usr/bin/env ruby

# Checks the Ruby syntax of the current file
#  (depends on ruby, zenity)
#
# Save:   Nothing
# Input:  Current document
# Output: Nothing

if "Syntax OK" == syntax = `ruby -c 2>&1`.chomp
  type = 'info'
else
  type = 'error'
  syntax = "Syntax not OK\n\nLine #{syntax[2..-1]}"
end

`zenity --#{type} --title='ruby -c' --text='#{syntax}'`

[ruby] run

Next one is the simplest, yet useful tool: run ruby ;)

#!/bin/sh

# Executes the current Ruby file and shows it in the bottom pane
#  (depends on ruby)
# Save:   Nothing
# Input:  Current document
# Output: Display in bottom pane

ruby

[ruby] load into irb

Often you want to further interact with the results of the ran code. You can do it by requiring the ruby file in an irb session. This tool does this for you.

#!/usr/bin/env ruby

# Saves the current file, checks its syntax and loads it into an irb shell
#  (depends on ruby, zenity)
#
# Save:   Current document
# Input:  Nothing
# Output: Nothing

if "Syntax OK" == syntax = `ruby -c "$GEDIT_CURRENT_DOCUMENT_PATH" 2>&1`.chomp
  `gnome-terminal --command="irb -r$GEDIT_CURRENT_DOCUMENT_NAME" --working-directory="$GEDIT_CURRENT_DOCUMENT_DIR" &`
else
  `zenity --error --title='ruby -c' --text='Syntax not OK\n\nLine #{syntax[/rb:\d+:.*$/][3..-1]}'`
end

[ruby] run rake task

This shows a selection dialog with all available rake tasks in the current file’s directory. It runs the selected task in the bottom pane.

#!/bin/sh

# Shows a list with available rake tasks and executes the selected one in the bottom pane
#  (depends on ruby, zenity)
#
# Save:   Nothing
# Input:  Nothing
# Output: Display in bottom pane

rake=`rake -sT`

if [ -z "$rake" ]; then
  zenity --error --text='No Rakefile found.'
else
	cmd=`echo "$rake" | ruby -ne 'puts $1 if ~/^rake (\S*).*$/' | zenity --list --text='Select the task to run' --title='rake' --column='rake --tasks'`

if [ ! -z "$cmd" ]; then
    echo "rake $cmd"
    rake $cmd
  fi
fi

[ruby] run capistrano task

Same again, for capistrano.

#!/bin/sh

# Shows a list with available capistrano tasks and executes the selected one in the bottom pane
#  (depends on ruby, capistrano gem, zenity)
#
# Save:   Nothing
# Input:  Nothing
# Output: Display in bottom pane

cmd=`cap -qvT | ruby -ne 'puts $1 if ~/^cap (\S*).*$/' | zenity --list --text='Select the task to run' --title='capistrano' --column='cap --verbose --tasks'`
if [ ! -z "$cmd" ]; then
  echo "cap $cmd"
  cap $cmd
fi

Simple git integration

Here are some tools for common git actions. They do not aim to cover everything, replacing git command line action. They just try to simplify often done workflow tasks.

[git] status

Hit the shortcut to get the git status of the current file’s git repository.

#!/bin/sh

# Shows git status
#  (depends on git, ruby, zenity)
#
# Save:   Nothing
# Input:  Nothing
# Output: Nothing

if [ ! -z `git rev-parse --git-dir` ]; then
  git status | ruby -pe '$_.slice!(0)' | zenity --text-info --width=600 --height=600 --title="git status"
else
  zenity --error --title='git status' --text='Sorry, no git repository'
fi

git [diff]

See what has changed in the current file.

#!/bin/sh

# Saves the current file and shows it differences from current HEAD
#  (depends on git, zenity)
#
# Save:   Current document
# Input:  Nothing
# Output: Nothing

if [ ! -z `git rev-parse --git-dir` ]; then
  diff=`git diff $GEDIT_CURRENT_DOCUMENT_PATH` &&
  if [ -z "$diff" ]; then
    zenity --info --title="git diff" --text='File has not changed.'
  else
    echo "$diff" | zenity --text-info --title="git diff $GEDIT_CURRENT_DOCUMENT_PATH" --width=1000 --height=700 
  fi
else
  zenity --error --title='git diff' --text='Sorry, no git repository'
fi

[git] add/commit

This displays a menu with common add/commit tasks. For more complex purposes, you need to do it manually. The number column allows fast shortcuts.

#!/bin/sh

# Saves all documents, then shows a menu with common git tasks
#  (depends on git, zenity)
#
# Save:   All documents
# Input:  Nothing
# Output: Nothing

if [ ! -z `git rev-parse --git-dir` ]; then

msg() {
  zenity --entry --title="git commit" --text="message:" --width=500
}

sel=`echo "1
add 
file
2
add
file's directory
3
add
repository
4
commit
index
5
commit
file (ignore index)
6
commit
file's directory (ignore index)
7
add & commit
repository
8
add & commit
repository (don't add new files)
9
push
(only fast forward)" | zenity --list --title="git" --text="choose action" --column="#" --column="action" --column="files" --height=320 --width=400`

case $sel in
1)
  git add $GEDIT_CURRENT_DOCUMENT_PATH &&
  zenity --info --title='git add' --text="Added\n$GEDIT_CURRENT_DOCUMENT_PATH"
  ;;
2)
  git add $GEDIT_CURRENT_DOCUMENT_DIR &&
  zenity --info --title='git add' --text="Added\n$GEDIT_CURRENT_DOCUMENT_DIR"
  ;;
3)
  dir=`dirname \`git rev-parse --git-dir\``
  git add "$dir" &&
  zenity --info --title='git add' --text="Added\n$dir"
  ;;
4)
  res=`git commit -m \'\`msg\`\'` &&
  zenity --info --title='git commit' --text="$res"
  ;;
5)
  res=`git commit $GEDIT_CURRENT_DOCUMENT_PATH -m \'\`msg\`\'` &&
  zenity --info --title="git commit $GEDIT_CURRENT_DOCUMENT_PATH" --text="$res"
  ;;
6)
  res=`git commit $GEDIT_CURRENT_DOCUMENT_DIR -m \'\`msg\`\'` &&
  zenity --info --title="git commit $GEDIT_CURRENT_DOCUMENT_DIR" --text="$res"
  ;;
7)
  dir=`dirname \`git rev-parse --git-dir\``
  git add "$dir" &&
  res=`git commit -m \'\`msg\`\'` &&
  zenity --info --title="git add $dir && git commit" --text="$res"
  ;;
8)
  res=`git commit -a -m \'\`msg\`\'` &&
  zenity --info --title="git commit -a" --text="$res"
  ;;
9)
  params=`zenity --entry --title='git push' --text='params (cancel for none)' --entry-text='origin master'`
  res=`git push $params` &&
  zenity --info --title="git push $params" --text="$res"
  ;;
esac

else
  zenity --error --title='git' --text='Sorry, no git repository'
fi

General utilities

Some more tool snippets I find useful.

[selection] crypto hash

Select some text and replace it with a hash value. You can also edit the selected menu entry to choose a different key length for SHA2 or to append salt to the string.

#!/usr/bin/env ruby

# Replaces selected text with a sha/md5 hash value of it
#  (depends on: ruby, zenity)
#
# Save:   Nothing
# Input:  Current selection
# Output: Replace current selection

require 'digest'

list =
"SHA2\n256\n
SHA2\n384\n
SHA2\n512\n
SHA1\n(160)\n
MD5\n(128)\n"

params = `echo "#{list}" | zenity --list --title='create cryptographic hash' --text='What kind of hash do you want to create? (editable, but press enter after edit!)' --height=210 --column="algorithm" --column="length" --column='salt' --editable --print-column='ALL'`.chop

gets

if params =~ /^(SHA2|SHA1|MD5)\|(.*?)\|(.*)$/ # calculate hash if possible
  print eval "(Digest::#$1.new(#{$2.to_i if $1 == 'SHA2'}) << '#$_' + '#$3').to_s"
else # or don't change input
  print
end

[create] save tabs as tgz

A relaxed way to create a flat .tgz archiv: open all documents which should go in there and start this tool.

#!/usr/bin/env ruby

# Saves all tabs, then creates a tgz archive of them
#  (depends on tar, ruby, zenity)
#
# Save:   All documents
# Input:  Nothing
# Output: Nothing

save_at = `zenity --file-selection --save --title='Please select the location where the archive should be stored' --filename="$GEDIT_CURRENT_DOCUMENT_DIR/archive.tgz"`.chomp

if save_at
  cmd = 'tar czf ' + save_at + ENV['GEDIT_DOCUMENTS_PATH'].split.map{ |doc|
    " -C #{File.dirname doc} #{File.basename doc}"
  }.join

`#{cmd} && zenity --info --text='#{save_at}\n...has been successfully created.'`
end

[create] export to syntax highlighted html

Get code into a nice html format using coderay ;)

#!/usr/bin/env ruby

# Exports the current file to syntax highlighted html using coderay
#  (depends on ruby, coderay gem, zenity)
#
# Save:   Nothing
# Input:  Current document
# Output: Nothing

require 'rubygems'
require 'coderay'

supported = {
'application/x-ruby' => 'ruby',
'text/html'          => 'html',
'text/rhtml'         => 'rhtml',
'text/css'           => 'css',
'text/javascript'    => 'java_script',
'application/xml'    => 'xml',
'text/x-csrc'        => 'c',
'text/x-chdr'        => 'cplusplus',
'text/x-c++src'      => 'cplusplus',
'text/x-java'        => 'java',
'text/x-php'         => 'php',
'text/x-python'      => 'python',
'text/x-scheme'      => 'scheme',
'text/x-sql'         => 'sql'
 # ...
}

if lang = supported[ENV['GEDIT_CURRENT_DOCUMENT_TYPE']]
  html = CodeRay::Duo[lang, :page].highlight gets(nil)
  path = `zenity --file-selection --save --title='Please select the location where the syntax html file should be stored' --filename="$GEDIT_CURRENT_DOCUMENT_PATH.html"`
  unless path.empty?
    File.open(path, 'w').puts html
  end
else
  `zenity --error --text='Sorry, language not supported'`
end

[internet] download url (wget)

Quick access to the internet ;)

#!/bin/sh

# Creates a new document and downloads the entered URL (if valid)
#  (depends on wget, zenity)
#
# Save:   Nothing
# Input:  Nothing
# Output: Create new document

url=`zenity --entry --title='Download' --text='URL:'`

if [ ! -z "$url" ]; then
  wget -qO- "$url"
fi

More ;)

All these tools can be found on github, where you can also download them all at once. Feel free to fork and add new ones ;). There will be another article with more tools in about a month. If I think a contributed tool is very useful, I will feature it in the article.

CC-BY (DE)

Read: gedit external tools: Ruby helpers, git integration and more

Topic: On the backchannel, and civility Previous Topic   Next Topic Topic: Video demonstrations in presentations

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use