File: history.rb

package info (click to toggle)
epic5 2.0.1-1
  • links: PTS
  • area: main
  • in suites: buster, sid, stretch
  • size: 4,696 kB
  • ctags: 6,357
  • sloc: ansic: 69,814; makefile: 715; ruby: 227; sh: 178; perl: 13
file content (367 lines) | stat: -rw-r--r-- 9,799 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#
# # # # # # # # # # # # # # #  # # # # # # # # # # 
# # # # # This is just a proof of concept! # # # # 
# # # # # DOES NOT WORK! DO NOT USE! # # # # # # #
# # # # # # # # # # # # # # #  # # # # # # # # # # 
return;

# Copyright 2007 EPIC Software Labs
#
# This is my attempt to port BlackJac's history script to ruby.
# Just for fun!
#
# Settings:
#	set history [0|<positive integer>]
#		Controls the maximum number of entries to be stored in
#		the client's history buffer. Setting it to 0 purges the
#		history buffer and disables command history.
#	set history_circleq [on|off|toggle]
#		Controls the behavior of history browsing when you reach
#		the beginning or end of the buffer. If set to on, when
#		you reach the beginning of the buffer and press the
#		backward_history keybinding (defaulted to the up arrow)
#		again, the history will circle back to the end of the
#		buffer. In addition, when you reach the end of the
#		buffer and press the forward_history keybinding
#		(defaulted to the down arrow) again, the history will
#		circle back to the beginning of the buffer.
#	set history_persistent [on|off|toggle]
#		Controls whether the current session's history buffer
#		will be saved on exit and read back into memory the next
#		time the script is loaded.
#	set history_remove_dupes [on|off|toggle]
#		Controls whether history buffer entries that are exact
#		duplicates of the most recent entry are removed from the
#		buffer.
#	set history_save_file <filename>
#		Default filename to use when saving the current history
#		buffer or reading a previous buffer and adding it to the
#		current buffer.
#	set history_save_position [on|off|toggle]
#		Controls whether the backward_history or forward_history
#		keybindings will start scrolling through the history
#		buffer from the spot where a previous
#		/!<indexnum|pattern> match was found, or from the most
#		recent history buffer entry.
#	set history_timestamp [on|off|toggle]
#		Controls whether /history displays the timestamp of each
#		command.
#
# Commands:
#	/history [<indexnum>]
#		Returns a list of all commands in the history buffer, or
#		all commands up to <indexnum>.
#	/!<indexnum|pattern>
#		Retrieves history buffer entries and outputs them to the
#		input line. Use /!<indexnum> to retrieve <indexnum> or
#		/!<pattern> to retrieve the most recent entry matching
#		<pattern>.
#
# Functions:
#	$historyctl(add <text>)
#		Adds <text> to the next available index number in the
#		history buffer. 
#		Returns 0 if the history buffer is disabled.
#		Returns 1 if successful.
#	$historyctl(delete <indexnum>)
#		Removes <indexnum> from the history buffer.
#		Returns -2 if the history buffer is disabled.
#		Returns -1 if <indexnum> does not exist.
#		Returns 0 if successful.
#	$historyctl(get <indexnum>)
#		Returns the history buffer item <indexnum> if it exists,
#		or nothing.
#	$historyctl(index <indexnum>)
#		Moves the history buffer scrollback pointer to item
#		<indexnum>.
#		Returns 0 if <indexnum> does not exist.
#		Returns 1 if successful.
#	$historyctl(read [<filename>])
#		Reads into the history buffer the contents of <file> or
#		HISTORY_SAVE_FILE if none is specified.
#		Returns nothing if the read failed.
#		Returns > 0 if the read succeeds.
#	$historyctl(reset)
#		Clears the entire history buffer.
#		Returns -1 if the history buffer does not exist.
#		Returns 0 if successful.
#	$historyctl(save [<filename>])
#		Writes the current history buffer to <file> or
#		HISTORY_SAVE_FILE if none is specified.
#		Returns nothing if the write failed.
#		Returns > 0 if the write succeeds.
#	$historyctl(set <buffer size>)
#		Sets the size of the history buffer. Setting it to 0
#		purges the history buffer and disables command history.
#
BEGIN {

class HistoryItem
	@@index = 0
	attr_reader :index, :timestamp, :stuff
	attr_writer :index, :timestamp, :stuff

	def initialize (stuff)
		@index = @@index++
		@timestamp = Time.new
		@stuff = stuff
	end
end

class History
  def History.cmd(args)
	EPIC.say("Command History:")
	History.items.each { |x|
		str = "#{x.index}"
		if (EPIC.expr("history_timestamp") == 'on')
			str += " [#{x.timestamp}] "
		else
			str += ": "
		end
		str += x.stuff
		EPIC.echo(str)
	}
  end

  def History.add(str)
	if str.length == 0
		return
	end

	if (EPIC.expr("history_remove_dupes") == 'on')
		History.items.reject! {|x| x.stuff == str}
		while (History.items.size >= EPIC.expr("history"))
			History.items.shift
		end

		History.items.push(str)
		History.index = nil;
	end
  end

  def History.erase
	History.index = nil;
	EPIC.cmd("parsekey reset_line")
  end

  def History.get_backward
	input = EPIC.expr("L")
	if (input.length > 0 && History.index == nil)
		History.add(input)
		History.index = History.items.length - 1
	end

	if (History.index == 0)
		if (EPIC.expr("history.circleq") == 'on')
			History.index = History.items.length - 1
		end
	else
		History.index = History.index - 1
	end

	History.show(History.index)
  end

  def History.get_forward
	input = EPIC.expr("L")
	if (input.length > 0 && History.index == nil)
		History.add(input)
	end

	if (History.index == History.items.length - 1 || History.index == nil)
		if (EPIC.expr("history.circleq") == 'on')
			History.show(0) 		# Oldest entry!
		else
			History.erase			# A blank line
		end
	elsif (History.index < History.items.length && History.index != nil)
		History.index = History.index + 1
		History.show(History.index)
	end
  end

  def History.shove
	str = EPIC.expr("L")
	History.add(str)
	EPIC.cmd("parsekey reset_line")
  end

  def History.show(index)
	if (index == nil)
		return
	end

	History.index = index
	EPIC.cmd("parsekey reset_line #{History.items[History.index].stuff}")
  end

  def History.historyctl(args)
	[action, rest] = args.split(' ', 2)
	case $action
	    when "add"
		if (History.max <= 0)
			return 0
		end
		History.add(rest)
		return 1
	    when "delete"
		index = rest.to_i
		History.items.delete_at(index)
		return 1
	    when "get"
		index = rest.to_i
		return History.items[index].stuff
	    when "index"
		index = rest.to_i
		History.index = index
		return 1
	    when "read"
		if (rest.length != 0)
			filename = rest
		else
			filename = save_file
		end
		File.foreach(filename) {|x|
			History.add(x)
		}
	    when "reset"
		History.index = nil
		History.items.clear
		return 1
	    when "save"
		if (rest.length != 0)
			filename = rest
		else
			filename = save_file
		end
		f = File.open(filename, "w")
		History.items.each {|x|
			f.puts("#{x.timestamp} #{x.stuff}")
		}
		f.close
		return 1
	    when "set"
		History.sethistory(rest)
		return 1
	    else
	end
  end

  def History.sendline(str)
	if (str.length == 0)
		return
	end
	History.add(str)
	EPIC.cmd("//sendline #{str}")
  end

  #
  # This function is called when the user does /!<something>
  #  1. If <something> is a number, it should behave as though the
  #     user had moved to the <something>th most recent history item
  #  2. Otherwise, it should behave as though the user had hit BACKWARD_HISTORY
  #     until an entry containing <something> is found.
  #  3. If <something> does not match, it should error and do nothing
  #     (ie, leave the input line unchanged
  #
  def History.banghandler(str)
>>> TODO >>>
	@ :find = after(! $0);
	if (isnumber($find)) {
		if (:found = getitem(array.history $find)) {
			xtype -l $restw(1 $found)${*1 ? [$1-] : []};
		} else {
			xecho -b -c No such history entry: $find;
		};
	} else if (:found = [$getmatches(array.history % /$find*) $getmatches(array.history % $find*)]) {
		@ :index = rightw(1 $numsort($found));
		if (history_save_position == [on]) {
			@ history.index = index;
		};
		xtype -l $restw(1 $getitem(array.history $index))${*1 ? [ $1-] : []};
	} else {
		xecho -b -c No match;
	};
<<<< TODO <<<<
  end

  def History.sethistory(numstr)
	limit = numstr.to_i
	if (limit == 0)
		History.index = nil;
		History.items.clear
		EPIC.eval("^bind ^] nothing;");
		EPIC.eval("^on #input 2 -*");
		EPIC.eval{"^on #input 2 -/!*");
	else
		while (limit > History.items.length)
			History.trim
		end
		EPIC.eval("^bind ^] shove_to_history")
		EPIC.eval("^on #-input 2 * {history.add $*}")
		EPIC.eval("^on #-input 2 /!* #")
	end
  end

  def History.setpersistent(boolstr)
	case boolstr
		when 'on'
			EPIC.eval("on #-exit 2 * {@ historyctl(save $history_save_file)}")
		when 'off'
			EPIC.eval("on #exit 2 -*")
		else
			EPIC.yell("Huh?")
	end
   end
end
	dummy = <<IRCII
}

##########################################################
ruby {load '$word(1 $loadinfo())'}

package history
load addset

alias historyctl (action, ...) {return $ruby(History.historyctl '$*')}
alias sendline (...) {ruby {History.sendline '$*'}}
@ bindctl(function BACKWARD_HISTORY create "ruby {History.get_backward}")
@ bindctl(function ERASE_HISTORY create "ruby {History.erase}")
@ bindctl(function FORWARD_HISTORY create "ruby {History.get_forward}")
@ bindctl(function SHOVE_TO_HISTORY create "ruby {History.shove}")

fe (N [OB [[B) hh {@ bindctl(sequence ^$hh set forward_history)}
fe (P [OA [[A) hh {@ bindctl(sequence ^$hh set backward_history)}
@ bindctl(sequence ^U set erase_history)

^on ^input "/!*" {ruby {History.banghandler '$*'}}

addset history int {ruby {History.sethistory '$*'}}
set history 150

addset history_circleq bool;
set history_circleq on

addset history_persistent bool {ruby {History.setpersistent '$*'}}
set history_persistent off

addset history_save_file str
set history_save_file ~/.epic_history

addset history_remove_dupes bool
set history_remove_dupes off

addset history_save_position bool;
set history_save_position on

addset history_timestamp bool
set history_timestamp off

if (history_persistent == [on] && fexist($history_save_file) == 1) {
        @ historyctl(read $history_save_file)
}

: {
IRCII
}