public
Fork of drnic/ruby-on-rails-tmbundle
Description: Ruby on Rails TextMate bundle [master branch is svn trunk; patches to drnicwilliams@gmail.com]
Homepage: http://macromates.com
Clone URL: git://github.com/Infininight/ruby-on-rails-tmbundle.git
Search Repo:
commit  c1160ccd8fd575a698a44d7b4a7094a1a7c973d8
tree    920c6eeed6657a0f348871e7186beeef8429dc7a
parent  4e2fe3ade86e6bbd8b4c47482a6e81dc8d85ff59
ruby-on-rails-tmbundle / Support / lib / rails / text_mate.rb
100644 124 lines (107 sloc) 4.048 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright:
# (c) 2006 syncPEOPLE, LLC.
# Visit us at http://syncpeople.com/
# Author: Duane Johnson (duane.johnson@gmail.com)
# Description:
# Helper module for accesing TextMate facilities such as environment variables.
 
module TextMate
  class <<self
    def open_url(url)
      `open "#{url}"`
    end
 
    # Open a file in textmate using the txmt:// protocol. Uses 0-based line and column indices.
    def open(filename, line_number = nil, column_number = nil)
      filename = filename.filepath if filename.is_a? RailsPath
      options = []
      options << "url=file://#{filename}"
      options << "line=#{line_number + 1}" if line_number
      options << "column=#{column_number + 1}" if column_number
      open_url "txmt://open?" + options.join("&")
    end
 
    # Always return something, or nil, for selected_text
    def selected_text
      env(:selected_text)
    end
 
    # Make line_number 0-base index
    def line_number
      env(:line_number).to_i - 1
    end
 
    # Make column_number 0-base as well
    def column_number
      env(:column_number).to_i - 1
    end
 
    def project_directory
      env(:project_directory)
    end
 
    def env(var)
      ENV['TM_' + var.to_s.upcase]
    end
 
    # Forward to the TM_* environment variables if method is missing. Some useful variables include:
    # selected_text, current_line, column_number, line_number, support_path
    def method_missing(method, *args)
      if value = env(method)
        return value
      else
        super(method, *args)
      end
    end
 
    # TODO: Move cocoa dialog stuff to its own class or module
 
    def cocoa_dialog_command
      "#{support_path}/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog"
    end
 
    # See http://cocoadialog.sourceforge.net/documentation.html for documentation
    def cocoa_dialog(command, options = {})
      options_list = []
      options.each_pair do |k, v|
        k = k.to_s.gsub('_', '-')
        value = v.is_a?(Array) ? %Q{"#{v.join('" "')}"} : "\"#{v}\""
        if v
          if v.is_a? TrueClass
            options_list << "--#{k}"
          else
            options_list << "--#{k} #{value}"
          end
        end
      end
      dialog_command = "\"#{cocoa_dialog_command}\" #{command} #{options_list.join(' ')}"
      # $logger.debug "Dialog command: #{dialog_command}"
      `#{dialog_command}`.to_a.map { |v| v.strip }
    end
 
    # Shows an information bubble with a nice gradient background
    #
    # def message(text, options = {})
    # options = {:title => "Message", :informative_text => text, :button1 => "Ok"}.update(options)
    # return cocoa_dialog('msgbox', options)[0] == "1"
    # end
 
    # def textbox(informative_text, text, options = {})
    # options = {:title => "Message", :informative_text => informative_text, :text => text, :button1 => "Ok"}.update(options)
    # return cocoa_dialog('textbox', options)[0] == "1"
    # end
 
    # def message_yes_no_cancel(text, options = {})
    # options = {:title => "Message", :text => text}.update(options)
    # return cocoa_dialog('yesno-msgbox', options)[0] == "1"
    # end
 
    # def message_ok_cancel(text, informative_text = nil, options = {})
    # options = {:title => "Message", :text => text, :informative_text => informative_text}.update(options)
    # return cocoa_dialog('ok-msgbox', options)[0] == "1"
    # end
 
    def input(text, default_text = "", options = {})
      options = {:title => "Input", :informative_text => text, :text => default_text}.update(options)
      button, text = cocoa_dialog('standard-inputbox', options)
      if button == '1'
        return text.strip
      else
        return nil
      end
    end
 
    def choose(text, choices = ["none"], options = {})
      options = {:title => "Choose", :text => text, :items => choices, :button1 => 'Ok', :button2 => 'Cancel'}.update(options)
      button, choice = cocoa_dialog('dropdown', options)
      if button == '1'
        return choice.strip.to_i
      else
        return nil
      end
    end
  end
end