Class: Twit::CLI

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/twit/cli.rb

Overview

Automatically-built command-line interface (using Thor).

Instance Method Summary collapse

Instance Method Details

#discardObject



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

def discard
  begin
    say "All changes since last save will be reverted."
    if yes? "Would you like to copy the changes in a different branch instead?"
      oldbranch = Twit.current_branch
      newbranch = ask "Enter name for the copy branch:"
      Twit.saveas newbranch
      Twit.open oldbranch
      return
    elsif no? "These changes will be lost forever! Are you sure?"
      return
    end
    Twit.discard
  rescue Error => e
    say "Error: #{e.message}"
  end
end

#guiObject

See GUI.



156
157
158
# File 'lib/twit/cli.rb', line 156

def gui
  Twit::GUI.main
end

#include(other_branch = nil) ⇒ Object

Deprecated: use git merge instead.



100
101
102
103
# File 'lib/twit/cli.rb', line 100

def include other_branch = nil
  say "This function has been deprecated."
  say "Use git merge instead!"
end

#include_into(other_branch = nil) ⇒ Object

Deprecated: use git merge instead.



107
108
109
110
# File 'lib/twit/cli.rb', line 107

def include_into other_branch = nil
  say "This function has been deprecated."
  say "Use git merge instead!"
end

#initObject

See init



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/twit/cli.rb', line 13

def init
  begin
    if Twit.is_repo?
      say "You are already in a repository! (Root directory: #{Twit.repo.root})"
      return
    end
    Twit.init
  rescue Error => e
    say "Error: #{e.message}"
  end
end

#listObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/twit/cli.rb', line 83

def list
  begin
    @current = Twit.current_branch
    @others = Twit.list.reject { |b| b == @current }
  rescue Error => e
    say "Error: #{e.message}"
    return
  end
  say "Current branch: #{@current}"
  say "Other branches:"
  @others.each do |branch|
    say "- #{branch}"
  end
end

#open(branch = nil) ⇒ Object

See Repo#open.



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/twit/cli.rb', line 68

def open branch = nil
  while branch.nil? || branch.strip == ""
    branch = ask "Please enter the name of the branch to open:"
  end
  begin
    Twit.open branch
  rescue Error => e
    say "Error: #{e.message}"
  else
    say "Opened #{branch}."
  end
end

#rewind(amount) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/twit/cli.rb', line 114

def rewind amount
  begin
    unless Twit.nothing_to_commit?
      return if no? "You have unsaved changes to your branch. Proceed?"
    end
    say "This branch will be rewound by #{amount} save points."
    if yes? "Would you like to save a copy of these last #{amount} points?"
      oldbranch = Twit.current_branch
      newbranch = ask "Enter name for the copy branch:"
      Twit.saveas newbranch
      Twit.open oldbranch
    elsif no? "These #{amount} changes may be lost forever! Are you sure?"
      return
    end
    Twit.rewind amount.to_i
  rescue Error => e
    say "Error: #{e.message}"
  end
end

#save(message = nil) ⇒ Object

See Repo#save.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/twit/cli.rb', line 27

def save message = nil
  begin
    if Twit.nothing_to_commit?
      say "No new edits to save"
      return
    end
    while message.nil? || message.strip == ""
      message = ask "Please supply a message describing your changes:"
    end
    Twit.save message
  rescue Error => e
    say "Error: #{e.message}"
  end
end

#saveas(branch = nil, message = nil) ⇒ Object



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

def saveas branch = nil, message = nil
  while branch.nil? || branch.strip == ""
    branch = ask "Please supply the name for your new branch:"
  end
  begin
    if (not Twit.nothing_to_commit?) && message.nil?
      message = ask "Please supply a message describing your changes:"
    end
    begin
      Twit.saveas branch, message
    rescue InvalidParameter => e
      if /already exists/.match e.message
        say "Cannot saveas to existing branch. Try a git merge!"
      else
        say "Error: #{e.message}"
      end
    end
  rescue Error => e
    say "Error: #{e.message}"
  end
end