-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathprofanity.rb
More file actions
executable file
·289 lines (248 loc) · 9.8 KB
/
profanity.rb
File metadata and controls
executable file
·289 lines (248 loc) · 9.8 KB
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
#!/usr/bin/env ruby
# frozen_string_literal: true
# encoding: US-ASCII # rubocop:disable Lint/OrderedMagicComments
# vim: set sts=2 noet ts=2:
#
# ProfanityFE v0.4
# Copyright (C) 2013 Matthew Lowe
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# matt@lichproject.org
#
require 'socket'
require 'rexml/document'
BOOT_PROFILE = ARGV.include?('--profile')
if BOOT_PROFILE
BOOT_T0 = Process.clock_gettime(Process::CLOCK_MONOTONIC)
BOOT_TIMINGS = []
def boot_mark(label)
elapsed = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - BOOT_T0) * 1000).round(1)
BOOT_TIMINGS << [label, elapsed]
end
boot_mark('stdlib loaded')
end
# Load version and initialize curses
require_relative 'lib/version'
require_relative 'lib/curses_setup'
boot_mark('curses init') if BOOT_PROFILE
# Load global constants (HIGHLIGHT, PRESET, LAYOUT, etc.)
require_relative 'lib/constants'
# Thread-safe curses rendering (must be loaded before any doupdate calls)
require_relative 'lib/curses_renderer'
# Centralized highlight processing (must be loaded before windows)
require_relative 'lib/highlight_processor'
# Window classes loaded from lib/windows/
require_relative 'lib/windows/base_window'
require_relative 'lib/windows/skill'
require_relative 'lib/windows/exp_window'
require_relative 'lib/windows/perc_window'
require_relative 'lib/windows/text_window'
require_relative 'lib/windows/tabbed_text_window'
require_relative 'lib/windows/progress_window'
require_relative 'lib/windows/countdown_window'
require_relative 'lib/windows/indicator_window'
require_relative 'lib/windows/sink_window'
require_relative 'lib/windows/room_window'
require_relative 'lib/key_codes'
require_relative 'lib/color_manager'
require_relative 'lib/selection_manager'
require_relative 'lib/gag_patterns'
# Extracted modules (SRP decomposition)
require_relative 'lib/shared_state'
require_relative 'lib/kill_ring'
require_relative 'lib/string_classification'
require_relative 'lib/profanity_log'
require_relative 'lib/command_buffer'
require_relative 'lib/window_manager'
require_relative 'lib/settings_loader'
require_relative 'lib/games/dragonrealms'
require_relative 'lib/games/gemstone'
require_relative 'lib/room_data_processor'
require_relative 'lib/familiar_notifier'
require_relative 'lib/game_text_processor'
require_relative 'lib/profanity_settings'
require_relative 'lib/autocomplete'
require_relative 'lib/mouse_scroll'
require_relative 'lib/application'
# Initialize gag patterns with defaults (can be extended via XML config)
GagPatterns.load_defaults
boot_mark('requires + gag defaults') if BOOT_PROFILE
# Ensure terminal is restored on any exit path (graceful shutdown)
at_exit do
Curses.close_screen
rescue StandardError
nil
end
# @deprecated Use {BaseWindow.parse_color_attrs} instead.
def parse_color_attrs(element, attr_name)
return unless element.attributes[attr_name]
element.attributes[attr_name].split(',').collect do |val|
val == 'nil' ? nil : val
end
end
# @deprecated Use RoomDataProcessor#parse_player_names instead.
def parse_player_names(text)
text.sub(/^Also here:\s*/, '')
.sub(/ and (?<rest>.*)$/) { ", #{Regexp.last_match[:rest]}" }
.split(', ')
.map { |obj| obj.sub(/ (who|whose body)? ?(has|is|appears|glows) .+/, '').sub(/ \(.+\)/, '') }
.map { |obj| obj.strip.scan(/\w+$/).first }
.compact
end
# @deprecated Use {WindowManager#add_prompt} instead.
# Kept for backward compatibility with spec_helper and external callers.
def add_prompt(window, prompt_text, cmd = '')
return if cmd.empty? && window.respond_to?(:duplicate_prompt?) && window.duplicate_prompt?(prompt_text)
prompt_colors = [{ start: 0, end: (prompt_text.length + cmd.length), fg: '555555' }]
window.route_string("#{prompt_text}#{cmd}", prompt_colors, MAIN_STREAM)
end
# Safe arithmetic expression evaluator for layout dimensions.
# Parses integers, +, -, *, /, and parentheses without using eval.
# Uses a simple recursive descent parser (expression → term → factor).
#
# @param expr [String] arithmetic expression (e.g., "lines-2", "cols/3+1")
# @return [Integer] computed result, or 0 on error
def safe_eval_arithmetic(expr)
tokens = expr.gsub(/\s+/, '').scan(%r{(\d+|[+\-*/()]|.)})
.flatten.reject(&:empty?)
# Reject tokens that aren't valid arithmetic
unless tokens.all? { |t| t.match?(%r{\A(\d+|[+\-*/()])\z}) }
warn "Invalid layout expression (unsafe characters): #{expr}"
return 0
end
pos = [0] # mutable position index
# Recursive descent: expr → term ((+|-) term)*
parse_expr = nil
parse_term = nil
parse_factor = nil
parse_factor = lambda {
if tokens[pos[0]] == '('
pos[0] += 1
result = parse_expr.call
pos[0] += 1 if tokens[pos[0]] == ')' # consume ')'
result
elsif tokens[pos[0]] == '-'
pos[0] += 1
-parse_factor.call
elsif tokens[pos[0]]&.match?(/\A\d+\z/)
val = tokens[pos[0]].to_i
pos[0] += 1
val
else
0
end
}
parse_term = lambda {
result = parse_factor.call
while pos[0] < tokens.length && %w[* /].include?(tokens[pos[0]])
op = tokens[pos[0]]
pos[0] += 1
right = parse_factor.call
if op == '*'
result *= right
elsif right != 0
result /= right
else
result = 0 # division by zero → 0
end
end
result
}
parse_expr = lambda {
result = parse_term.call
while pos[0] < tokens.length && %w[+ -].include?(tokens[pos[0]])
op = tokens[pos[0]]
pos[0] += 1
right = parse_term.call
result = op == '+' ? result + right : result - right
end
result
}
parse_expr.call.to_i
rescue StandardError => e
warn "Layout expression error: #{e.message} in '#{expr}'"
0
end
# ========== CLI CONFIGURATION ==========
require 'optparse'
cli_options = {
port: 8000,
default_color_id: 7,
default_background_color_id: 0,
use_default_colors: false,
custom_colors: nil,
settings_file: nil,
log_dir: nil,
log_file: nil,
char: nil,
config: nil,
template: nil,
no_status: false,
links: false,
speech_ts: false,
room_window_only: false,
remote_url: false,
}
OptionParser.new do |opts|
opts.banner = "\nProfanity FrontEnd v#{VERSION}\n\n"
opts.on('--char=NAME', 'Character name (for log file & process title)') { |v| cli_options[:char] = v }
opts.on('--config=NAME', 'Config name to load (default: same as --char)') { |v| cli_options[:config] = v }
opts.on('--template=FILE', 'Template file name (from templates/)') { |v| cli_options[:template] = v }
opts.on('--port=PORT', Integer, 'Game server port (default: 8000)') { |v| cli_options[:port] = v }
opts.on('--default-color-id=ID', Integer, 'Default foreground color (default: 7)') { |v| cli_options[:default_color_id] = v }
opts.on('--default-background-color-id=ID', Integer, 'Default background color (default: 0)') { |v| cli_options[:default_background_color_id] = v }
opts.on('--custom-colors=MODE', %w[on off yes no], 'Force custom color mode (on/off/yes/no)') do |v|
cli_options[:custom_colors] = %w[on yes].include?(v)
end
opts.on('--use-default-colors', 'Use terminal default colors') { cli_options[:use_default_colors] = true }
opts.on('--no-status', 'Disable process title updates') { cli_options[:no_status] = true }
opts.on('--links', 'Enable in-game link highlighting') { cli_options[:links] = true }
opts.on('--speech-ts', 'Add timestamps to speech, familiar, and thought windows') { cli_options[:speech_ts] = true }
opts.on('--room-window-only', 'Do not echo room data to the story window') { cli_options[:room_window_only] = true }
opts.on('--remote-url', 'Display LaunchURLs on screen instead of opening browser') { cli_options[:remote_url] = true }
opts.on('--log-file=PATH', 'Log file path (default: profanity.log)') { |v| cli_options[:log_file] = v }
opts.on('--log-dir=DIR', 'Log directory (default: current directory)') { |v| cli_options[:log_dir] = v }
opts.on('--settings-file=FILE', 'Settings XML file path (overrides --char/--config lookup)') { |v| cli_options[:settings_file] = v }
opts.on('--profile', 'Log boot timing to log file') {} # handled early via BOOT_PROFILE
end.parse!
# ========== GLOBAL CONSTANTS ==========
PORT = cli_options[:port]
CHAR_NAME = cli_options[:char]
SETTINGS_FILENAME = ProfanitySettings.resolve_template(
char: cli_options[:config] || cli_options[:char],
template: cli_options[:template],
settings_file: cli_options[:settings_file],
app_dir: File.dirname(__FILE__)
)
LOG_FILE = ProfanitySettings.resolve_log(
char: cli_options[:char],
log_file: cli_options[:log_file],
log_dir: cli_options[:log_dir]
)
DEFAULT_COLOR_ID = cli_options[:default_color_id]
DEFAULT_BACKGROUND_COLOR_ID = cli_options[:default_background_color_id]
Curses.use_default_colors if cli_options[:use_default_colors]
CUSTOM_COLORS = cli_options[:custom_colors].nil? ? Curses.can_change_color? : cli_options[:custom_colors]
NO_STATUS = cli_options[:no_status]
SPEECH_TS = cli_options[:speech_ts]
ColorManager.configure(
default_color_id: DEFAULT_COLOR_ID,
default_background_color_id: DEFAULT_BACKGROUND_COLOR_ID,
custom_colors: CUSTOM_COLORS
)
# ========== RUN ==========
boot_mark('constants + color config') if BOOT_PROFILE
Application.new(cli_options).run