Class: Glimmer::Gladiator::Command

Inherits:
Object
  • Object
show all
Extended by:
Glimmer
Includes:
Glimmer
Defined in:
lib/models/glimmer/gladiator/command.rb

Constant Summary collapse

TIME_INTERVAL_SECONDS_NEW_COMMAND =

seconds

(ENV['UNDO_TIME_INTERVAL_SECONDS'] || 1).to_f

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, method = nil, *args) ⇒ Command

Returns a new instance of Command.



80
81
82
83
84
# File 'lib/models/glimmer/gladiator/command.rb', line 80

def initialize(file, method = nil, *args)
  @file = file
  @method = method
  @args = args
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def args
  @args
end

#fileObject

Returns the value of attribute file.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def file
  @file
end

#file_caret_positionObject

Returns the value of attribute file_caret_position.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def file_caret_position
  @file_caret_position
end

#file_dirty_contentObject

Returns the value of attribute file_dirty_content.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def file_dirty_content
  @file_dirty_content
end

#file_selection_countObject

Returns the value of attribute file_selection_count.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def file_selection_count
  @file_selection_count
end

#methodObject

Returns the value of attribute method.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def method
  @method
end

#next_commandObject

Returns the value of attribute next_command.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def next_command
  @next_command
end

#previous_commandObject

Returns the value of attribute previous_command.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def previous_command
  @previous_command
end

#previous_file_caret_positionObject

Returns the value of attribute previous_file_caret_position.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def previous_file_caret_position
  @previous_file_caret_position
end

#previous_file_dirty_contentObject

Returns the value of attribute previous_file_dirty_content.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def previous_file_dirty_content
  @previous_file_dirty_content
end

#previous_file_selection_countObject

Returns the value of attribute previous_file_selection_count.



77
78
79
# File 'lib/models/glimmer/gladiator/command.rb', line 77

def previous_file_selection_count
  @previous_file_selection_count
end

Class Method Details

.clear(file) ⇒ Object



63
64
65
# File 'lib/models/glimmer/gladiator/command.rb', line 63

def clear(file)
  command_history[file] = [Command.new(file)]
end

.command_historyObject



30
31
32
# File 'lib/models/glimmer/gladiator/command.rb', line 30

def command_history
  @command_history ||= {}
end

.command_history_for(file) ⇒ Object



34
35
36
37
# File 'lib/models/glimmer/gladiator/command.rb', line 34

def command_history_for(file)
  # keeping a first command to make redo support work by remembering next command after undoing all
  command_history[file] ||= [Command.new(file)]
end

.do(file, method = nil, *args, command: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/models/glimmer/gladiator/command.rb', line 39

def do(file, method = nil, *args, command: nil)
  if command.nil?
    command = Command.new(file, method, *args)
    command.previous_command = command_history_for(file).last
    save_new_command = command_history_for(file).last.method == :change_content! && method == :change_content! && !time_for_new_command?
    command_history_for(file).last.next_command = command unless save_new_command
    command.do
    command_history_for(file) << command unless save_new_command
  else
    command_history_for(file) << command
  end
end

.redo(file) ⇒ Object



58
59
60
61
# File 'lib/models/glimmer/gladiator/command.rb', line 58

def redo(file)
  command = command_history_for(file).last
  command&.redo
end

.time_for_new_command?Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
# File 'lib/models/glimmer/gladiator/command.rb', line 67

def time_for_new_command?
  @time ||= Time.now
  time_for_new_command = (Time.now - @time) > TIME_INTERVAL_SECONDS_NEW_COMMAND
  @time = Time.now if time_for_new_command
  time_for_new_command
end

.undo(file) ⇒ Object



52
53
54
55
56
# File 'lib/models/glimmer/gladiator/command.rb', line 52

def undo(file)
  return if command_history_for(file).size <= 1
  command = command_history_for(file).pop
  command&.undo
end

Instance Method Details

#backupObject



110
111
112
113
114
115
116
117
118
# File 'lib/models/glimmer/gladiator/command.rb', line 110

def backup
  @previous_file_dirty_content = @file.dirty_content.clone
  @previous_file_caret_position = @file.caret_position
  @previous_file_selection_count = @file.selection_count
  if @method == :change_content!
    @previous_file_caret_position = @file.last_caret_position
    @previous_file_selection_count = @file.last_selection_count
  end
end

#doObject



91
92
93
94
95
# File 'lib/models/glimmer/gladiator/command.rb', line 91

def do
  return if native?
  backup
  execute
end

#executeObject



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/models/glimmer/gladiator/command.rb', line 126

def execute
  @file.start_command
  @file.send(@method, *@args)
  @file.end_command
  @file_dirty_content = @file.dirty_content.clone
  @file_caret_position = @file.caret_position
  @file_selection_count = @file.selection_count
  if previous_command.method == :change_content! && @method == :change_content!
    previous_command.file_dirty_content = @file_dirty_content
    previous_command.file_caret_position = @file_caret_position
    previous_command.file_selection_count = @file_selection_count
  end
end

#native?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/models/glimmer/gladiator/command.rb', line 87

def native?
  @method.nil?
end

#redoObject



102
103
104
105
106
107
108
# File 'lib/models/glimmer/gladiator/command.rb', line 102

def redo
  return if next_command.nil?# || next_command.native?
  @file.dirty_content = next_command.file_dirty_content.clone
  @file.caret_position = next_command.file_caret_position
  @file.selection_count = next_command.file_selection_count
  Command.do(next_command.file, command: next_command)
end

#restoreObject



120
121
122
123
124
# File 'lib/models/glimmer/gladiator/command.rb', line 120

def restore
  @file.dirty_content = @previous_file_dirty_content.clone
  @file.caret_position = @previous_file_caret_position
  @file.selection_count = @previous_file_selection_count
end

#undoObject



97
98
99
100
# File 'lib/models/glimmer/gladiator/command.rb', line 97

def undo
  return if native?
  restore
end