Class: Termtter::StdOut

Inherits:
Hook
  • Object
show all
Defined in:
lib/plugins/stdout.rb

Instance Attribute Summary

Attributes inherited from Hook

#exec_proc, #name, #points

Instance Method Summary collapse

Methods inherited from Hook

#match?

Constructor Details

#initializeStdOut



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/plugins/stdout.rb', line 91

def initialize
  super(:name => :stdout, :points => [:output])
  @input_thread = nil
  Client.register_hook(
    :name => :stdout_exit,
    :points => [:exit],
    :exec_proc => lambda { @input_thread.kill if @input_thread }
  )
  Client.register_hook(
      :name => :stdout_readline_yield_thread,
      :points => [:before_task_thread_run],
      :exec_proc => lambda { start_input_thread() unless @input_thread }
  )
end

Instance Method Details

#call(statuses, event) ⇒ Object



106
107
108
# File 'lib/plugins/stdout.rb', line 106

def call(statuses, event)
  print_statuses(statuses)
end


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/plugins/stdout.rb', line 154

def print_statuses(statuses, sort = true, time_format = nil)
  return unless statuses and statuses.first
  unless time_format
    t0 = Time.now
    t1 = Time.parse(statuses.first[:created_at])
    t2 = Time.parse(statuses.last[:created_at])
    time_format =
      if [t0.year, t0.month, t0.day] == [t1.year, t1.month, t1.day] \
        and [t1.year, t1.month, t1.day] == [t2.year, t2.month, t2.day]
        '%H:%M:%S'
      else
        '%y/%m/%d %H:%M'
      end
  end

  output_text = ''
  output_text += "\e[1K\e[0G" unless win?
  Curses::init_screen;
  cols = Curses.cols;
  Curses::close_screen
  statuses.each do |s|
    text = s.text
    status_color = config.plugins.stdout.colors[s.user.id.hash % config.plugins.stdout.colors.size]
    status = "#{s.user.screen_name}: #{TermColor.escape(text)}"
    if s.in_reply_to_status_id
      status += " (reply to #{s.in_reply_to_status_id})"
    end

    time = "(#{Time.parse(s.created_at).strftime(time_format)})"
    id = s.id
    len =  id.to_s.length + 2
    if cols > 0 then
      status = status.truncate_column(cols-len-1).join( "\n" )
      status = status.gsub( /\n/, "\n".ljust(len) )
    end
    source =
      case s.source
      when />(.*?)</ then $1
      when 'web' then 'web'
      end

    erbed_text = ERB.new(config.plugins.stdout.timeline_format).result(binding)
    output_text << TermColor.parse(erbed_text) + "\n"
  end

  if config.plugins.stdout.enable_pager && ENV['LINES'] && statuses.size > ENV['LINES'].to_i
    file = Tempfile.new('termtter')
    file.print output_text
    file.close
    system "#{config.plugins.stdout.pager} #{file.path}"
    file.close(true)
  else
    output_text << TermColor.parse("<90>-----Fetched on " + Time.now.strftime(time_format) + "</90>\n")
    print output_text
  end
  Readline.refresh_line
end

#setup_readlineObject



124
125
126
127
128
129
130
131
132
133
# File 'lib/plugins/stdout.rb', line 124

def setup_readline
  if Readline.respond_to?(:basic_word_break_characters=)
    Readline.basic_word_break_characters= "\t\n\"\\'`><=;|&{("
  end
  Readline.completion_proc = Client.get_command_completion_proc()
  vi_or_emacs = config.editing_mod
  unless vi_or_emacs.empty?
    Readline.__send__("#{vi_or_emacs}_editing_mode")
  end
end

#start_input_threadObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/plugins/stdout.rb', line 135

def start_input_thread
  setup_readline()
  trap_setting()
  @input_thread = Thread.new do
    while buf = Readline.readline(ERB.new(config.prompt).result(API.twitter.__send__(:binding)), true)
      Readline::HISTORY.pop if buf.empty?
      begin
        Client.call_commands(buf)
      rescue CommandNotFound => e
        warn "Unknown command \"#{e}\""
        warn 'Enter "help" for instructions'
      rescue => e
        Client.handle_error e
      end
    end
  end
  @input_thread.join
end

#trap_settingObject



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/plugins/stdout.rb', line 110

def trap_setting()
  begin
    stty_save = `stty -g`.chomp
    trap("INT") do
      begin
        system "stty", stty_save
      ensure
        exit
      end
    end
  rescue Errno::ENOENT
  end
end