Class: Twterm::Tweetbox

Inherits:
Object
  • Object
show all
Includes:
Curses, Readline, Singleton
Defined in:
lib/twterm/tweetbox.rb

Defined Under Namespace

Classes: EmptyTextError, InvalidCharactersError, TextTooLongError

Instance Method Summary collapse

Instance Method Details

#compose(in_reply_to = nil) ⇒ Object



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
# File 'lib/twterm/tweetbox.rb', line 11

def compose(in_reply_to = nil)
  @text = ''

  if in_reply_to.is_a? Status
    @in_reply_to = in_reply_to
  else
    @in_reply_to = nil
  end

  resetter = proc do
    reset_prog_mode
    sleep 0.1
    Screen.instance.refresh
  end

  thread = Thread.new do
    close_screen

    if in_reply_to.nil?
      puts "\nCompose new tweet:"
    else
      puts "\nReply to @#{in_reply_to.user.screen_name}'s tweet: \"#{in_reply_to.text}\""
    end

    CompletionManager.instance.set_default_mode!

    loop do
      loop do
        msg = in_reply_to.nil? || !text.empty? ? '> ' : "> @#{in_reply_to.user.screen_name} "
        line = (readline(msg, true) || '').strip
        break if line.empty?

        if line.end_with?('\\')
          @text << line.chop.rstrip + "\n"
        else
          @text << line
          break
        end
      end

      puts "\n"

      begin
        validate_text!
        break
      rescue EmptyTextError
        break
      rescue InvalidCharactersError
        puts 'Text contains invalid characters'
      rescue TextTooLongError
        puts "Text is too long (#{text_length} / 140 characters)"
      end

      puts "\n"
      clear
    end

    resetter.call
    post
  end

  App.instance.register_interruption_handler do
    thread.kill
    clear
    puts "\nCanceled"
    resetter.call
  end

  thread.join
end