Class: Twterm::Tweetbox

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

Instance Method Summary collapse

Constructor Details

#initializeTweetbox

Returns a new instance of Tweetbox.



7
8
9
# File 'lib/twterm/tweetbox.rb', line 7

def initialize
  @status = ''
end

Instance Method Details

#clearObject



83
84
85
# File 'lib/twterm/tweetbox.rb', line 83

def clear
  @status = ''
end

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

def compose(in_reply_to = nil)
  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

    Readline.completion_append_character = ' '
    Readline.basic_word_break_characters = " \t\n\"\\'`$><=;|&{("
    Readline.completion_proc = proc do |str|
      if str.start_with?('#')
        History::Hashtag.instance.history
          .map { |tag| "##{tag}" }
          .select { |tag| tag.downcase.start_with?(str.downcase) }
      elsif str.start_with?('@')
        History::ScreenName.instance.history
          .map { |name| "@#{name}" }
          .select! { |name| name.downcase.start_with?(str.downcase) }
      else
        []
      end
    end

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

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

    resetter.call
    post
  end

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

  thread.join
end

#postObject



76
77
78
79
80
81
# File 'lib/twterm/tweetbox.rb', line 76

def post
  return if @status.nil? || @status.empty?

  Client.current.post(@status, @in_reply_to)
  clear
end