Class: TicGitNG::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/ticgit-ng/cli.rb

Constant Summary collapse

TIOCGWINSZ_INTEL =

For an Intel processor

0x5413
TIOCGWINSZ_PPC =

For a PowerPC processor

0x40087468
STDOUT_HANDLE =

For windows

0xFFFFFFF5

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args, path = '.', out = $stdout) ⇒ CLI

Returns a new instance of CLI.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ticgit-ng/cli.rb', line 22

def initialize(args, path = '.', out = $stdout)
  @args = args.dup

  #set @init if one of the args is 'init'
  #this needs to be done because initialization of the ticgit branch must be done before
  #the branch is loaded, but because of the way commands are modularized this must be done
  #outside of and before the init.rb file itself is called (init.rb is where we would
  #normally put the code for such a command).
  args.include?( 'init' ) ? (@init=true) : (@init=false)
  #@init= ((args[0][/init/]=='init') rescue false)
  #@init= ((args[0][/init/]=='init') or (args[1][/init/]=='init') rescue false)
  
  @tic = TicGitNG.open(path, {:keep_state => true, :init => @init, :logger  => out })
  @options = OpenStruct.new
  @out = out

  @out.sync = true # so that Net::SSH prompts show up
rescue NoRepoFound
  out.puts "No repo found"
  exit
end

Class Attribute Details

.window_colsObject

Returns the value of attribute window_cols.



172
173
174
# File 'lib/ticgit-ng/cli.rb', line 172

def window_cols
  @window_cols
end

.window_linesObject

Returns the value of attribute window_lines.



172
173
174
# File 'lib/ticgit-ng/cli.rb', line 172

def window_lines
  @window_lines
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



19
20
21
# File 'lib/ticgit-ng/cli.rb', line 19

def action
  @action
end

#argsObject (readonly)

Returns the value of attribute args.



19
20
21
# File 'lib/ticgit-ng/cli.rb', line 19

def args
  @args
end

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/ticgit-ng/cli.rb', line 19

def options
  @options
end

#outObject

Returns the value of attribute out.



20
21
22
# File 'lib/ticgit-ng/cli.rb', line 20

def out
  @out
end

#ticObject (readonly)

Returns the value of attribute tic.



19
20
21
# File 'lib/ticgit-ng/cli.rb', line 19

def tic
  @tic
end

Class Method Details

.executeObject



8
9
10
# File 'lib/ticgit-ng/cli.rb', line 8

def self.execute
  parse(ARGV).execute!
end

.parse(args) ⇒ Object



12
13
14
15
16
17
# File 'lib/ticgit-ng/cli.rb', line 12

def self.parse(args)
  #new() calls initialize(...) below
  cli = new(args)
  cli.parse_options!
  cli
end

.reset_window_widthObject



178
179
180
181
182
183
# File 'lib/ticgit-ng/cli.rb', line 178

def reset_window_width
  try_using(TIOCGWINSZ_PPC) ||
  try_using(TIOCGWINSZ_INTEL) ||
    try_windows ||
    use_fallback
end

.try_using(mask) ⇒ Object

Set terminal dimensions using ioctl syscall on *nix platform TODO: find out what is raised here on windows.



187
188
189
190
191
192
193
194
195
# File 'lib/ticgit-ng/cli.rb', line 187

def try_using(mask)
  buf = [0,0,0,0].pack("S*")

  if $stdout.ioctl(mask, buf) >= 0
    self.window_lines, self.window_cols = buf.unpack("S2")
    true
  end
rescue Errno::EINVAL, Errno::ENOTTY
end

.try_windowsObject



197
198
199
200
# File 'lib/ticgit-ng/cli.rb', line 197

def try_windows
  lines, cols = windows_terminal_size
  self.window_lines, self.window_cols = lines, cols if lines and cols
end

.use_fallbackObject



219
220
221
# File 'lib/ticgit-ng/cli.rb', line 219

def use_fallback
  self.window_lines, self.window_cols = 25, 80
end

.windows_terminal_sizeObject

Determine terminal dimensions on windows platform



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/ticgit-ng/cli.rb', line 203

def windows_terminal_size
  m_GetStdHandle = Win32API.new(
    'kernel32', 'GetStdHandle', ['L'], 'L')
  m_GetConsoleScreenBufferInfo = Win32API.new(
    'kernel32', 'GetConsoleScreenBufferInfo', ['L', 'P'], 'L' )
  format = 'SSSSSssssSS'
  buf = ([0] * format.size).pack(format)
  stdout_handle = m_GetStdHandle.call(STDOUT_HANDLE)

  m_GetConsoleScreenBufferInfo.call(stdout_handle, buf)
  (bufx, bufy, curx, cury, wattr,
   left, top, right, bottom, maxx, maxy) = buf.unpack(format)
  return bottom - top + 1, right - left + 1
rescue NameError
end

Instance Method Details

#execute!Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ticgit-ng/cli.rb', line 44

def execute!
  if mod = Command.get(action)
    extend(mod)

    if respond_to?(:parser)
      option_parser = Command.parser(action, &method(:parser))
    else
      option_parser = Command.parser(action)
    end

    option_parser.parse!(args)

    execute if respond_to?(:execute)
  else
    puts usage

    if args.empty? and !action
      exit
    else
      puts('%p is not a command' % action)
      exit 1
    end
  end
end

#get_editor_message(comments = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ticgit-ng/cli.rb', line 97

def get_editor_message(comments = nil)
  message_file = Tempfile.new('ticgitng_message').path
  File.open(message_file, 'w') { |f| f.puts comments } if comments

  editor = ENV["EDITOR"] || 'vim'
  system("#{editor} #{message_file}");
  message = File.readlines(message_file)
  message = message.select { |line| line[0, 1] != '#' } # removing comments
  if message.empty?
    return false
  else
    return message
  end
end

#just(value, size = 10, side = :left) ⇒ Object

assume 1.9



234
235
236
237
238
239
240
241
242
243
244
# File 'lib/ticgit-ng/cli.rb', line 234

def just(value, size = 10, side = :left)
  value = value.to_s

  if value.bytesize > size
    sub_value = "#{value[0, size - 1]}+"
  else
    sub_value = value[0, size]
  end

  just_common(sub_value, size, side)
end

#just_common(value, size, side) ⇒ Object



259
260
261
262
263
264
265
266
# File 'lib/ticgit-ng/cli.rb', line 259

def just_common(value, size, side)
  case side
  when :r, :right
    value.rjust(size)
  when :l, :left
    value.ljust(size)
  end
end

#parse_options!Object

:nodoc:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ticgit-ng/cli.rb', line 69

def parse_options! #:nodoc:
  if args.empty?
    puts "Please specify at least one action to execute."
    puts
    puts usage(args)
    exit 1
  end

  #FIXME
  #this is a dirty hack that needs to be fixed
  if args.include?('list') and args.include?('init')
    @action = 'list'
  else
    @action = args.shift
  end
end

#puts(*strings) ⇒ Object



268
269
270
# File 'lib/ticgit-ng/cli.rb', line 268

def puts(*strings)
  @out.puts(*strings)
end

#ticket_show(t, more = nil) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ticgit-ng/cli.rb', line 112

def ticket_show(t, more=nil)
    days_ago = ((Time.now - t.opened) / (60 * 60 * 24)).round

    data = [
        ['Title',    t.title],
        ['TicId',    t.ticket_id],
        '',
        ['Assigned', t.assigned],
        ['Opened',   "#{t.opened} (#{days_ago} days)"],
        ['State',    t.state.upcase],
        ['Points',   t.points || 'no estimate'],
        ['Tags',     t.tags.join(', ')],
        ''
    ]

    data.each do |(key, value)|
        puts(value ? "#{key}: #{value}" : key)
    end

    #FIXME display attachments inline chronologically with comments
    unless t.comments.empty? and t.attachments.empty?
        comments_and_attachments= Hash.new
        puts "Comments and attachments (#{t.comments.size + t.attachments.size}):"
        t.comments.each do |c|
            comments_and_attachments[c.added]=c
        end
        
        t.attachments.each do |a|
            comments_and_attachments[a.added]=a
        end
        comments_and_attachments.sort.each {|item|
            if item[1].class==TicGitNG::Comment
                #print comment
                puts "  * Added #{item[1].added.strftime('%m/%d %H:%M')} by #{item[1].user}"

                wrapped = item[1].comment.split("\n").map{|line|
                    line.length > 80 ? line.gsub(/(.{1,80})(\s+|$)/, "\\1\n").strip : line
                }.join("\n")

                wrapped = wrapped.split("\n").map{|line| "\t#{line}" }

                if wrapped.size > 6 and more.nil?
                    puts wrapped[0, 6].join("\n")
                    puts "\t** more... **"
                else
                    puts wrapped.join("\n")
                end
                puts
            else
                #print attachment
                puts "  * Added #{item[1].added.strftime('%m/%d %H:%M')} by #{item[1].user}"
                puts "    Attachment: #{t.attachments.index(item[1]) }"
                puts "    Filename:   #{item[1].attachment_name}"
                puts
            end
        }
    end
end

#usage(args = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/ticgit-ng/cli.rb', line 86

def usage(args = nil)
  old_args = args || [action, *self.args].compact

  if respond_to?(:parser)
    Command.parser('COMMAND', &method(:parser))
    # option_parser.parse!(args)
  else
    Command.usage(old_args.first, old_args)
  end
end

#window_colsObject



228
229
230
# File 'lib/ticgit-ng/cli.rb', line 228

def window_cols
  TicGitNG::CLI.window_cols
end

#window_linesObject



224
225
226
# File 'lib/ticgit-ng/cli.rb', line 224

def window_lines
  TicGitNG::CLI.window_lines
end