-
-
Notifications
You must be signed in to change notification settings - Fork 354
/
CommandLineUIManager.class.st
451 lines (343 loc) · 11.2 KB
/
CommandLineUIManager.class.st
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
"
I am UI manager for a headless setup.
I block all UI manager API that uses Morphs and will trow an error instead.
"
Class {
#name : #CommandLineUIManager,
#superclass : #UIManager,
#instVars : [
'doNotQuitOnRestart',
'uiManager'
],
#classVars : [
'SnapshotErrorImage'
],
#category : #'UIManager-Base'
}
{ #category : #testing }
CommandLineUIManager class >> isValidForCurrentSystemConfiguration [
^ Smalltalk isHeadless and: [ Smalltalk isInteractive ]
]
{ #category : #'instance creation' }
CommandLineUIManager class >> replacing: aUIManager [
"Replace the current UI manager with instance of myself.
Keep a backup reference to old manager, and then restore it, when image will be interactive again. "
^ self new replacing: aUIManager
]
{ #category : #accessing }
CommandLineUIManager class >> snapshotErrorImage [
^ SnapshotErrorImage == true
]
{ #category : #accessing }
CommandLineUIManager class >> snapshotErrorImage: aBoolean [
SnapshotErrorImage := aBoolean
]
{ #category : #settings }
CommandLineUIManager class >> uiSettingsOn: aBuilder [
<systemsettings>
(aBuilder group: #nonInteractive)
label: 'Headless mode';
parent: #pharoSystem;
description: 'Settings related to mode without UI';
with: [
(aBuilder setting: #snapshotErrorImage)
label: 'Snapshot before quitting' ;
target: CommandLineUIManager;
description: 'On unhandled exception, save a new version of the image before quitting']
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> abort: aStringOrText [
self abort: aStringOrText title: 'Error'.
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> abort: aStringOrText title: aString [
self logTitle: aString andDescription: aStringOrText to: self stderr.
]
{ #category : #private }
CommandLineUIManager >> activate [
NonInteractiveTranscript stderr install
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> alert: aStringOrText [
self abort: aStringOrText title: 'Alert'.
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> alert: aStringOrText title: aString [
self logTitle: aString andDescription: aStringOrText to: self stdout.
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> alert: aStringOrText title: aString configure: aBlock [
"Ignore the block, because we don't create a dialog, so there is nothing to configure."
self alert: aStringOrText title: aString.
]
{ #category : #'ui requests' }
CommandLineUIManager >> choose: questionsAnswerDict title: queryString [
]
{ #category : #'ui requests' }
CommandLineUIManager >> chooseFrom: aList lines: linesArray title: aString [
| maxPad |
maxPad := aList size asString size.
self stdout
nextPutAll: aString; nextPut: $:; cr.
aList withIndexDo: [ :item :index |
self stdout
nextPutAll: ' ['; nextPutAll: (index asString padLeftTo: maxPad); nextPutAll: '] ';
print: item; cr].
self stdout nextPutAll: '> '.
^ aList at: (self stdin upToAnyOf: String crlf do: [ :chr| ]) asInteger.
]
{ #category : #'ui requests' }
CommandLineUIManager >> confirm: queryString label: title trueChoice: trueLabel falseChoice: falseLabel cancelChoice: cancelLabel default: trueFalseNil [
| questions |
questions := Dictionary new.
trueLabel ifNotNil: [ questions at: 'y' put: trueLabel ].
falseLabel ifNotNil: [ questions at: 'n' put: falseLabel ].
cancelLabel ifNotNil: [ questions at: 'c' put: cancelLabel ].
^ self choose: questions title: title, ' ', queryString
]
{ #category : #'ui requests' }
CommandLineUIManager >> confirm: queryString trueChoice: trueLabel falseChoice: falseLabel [
^self
confirm: queryString
trueChoice: trueLabel
falseChoice: falseLabel
cancelChoice: nil
default: nil
]
{ #category : #'ui requests' }
CommandLineUIManager >> confirm: queryString trueChoice: trueLabel falseChoice: falseLabel cancelChoice: cancelLabel default: trueFalseNil [
| questions |
questions := Dictionary new.
trueLabel ifNotNil: [ questions at: 'y' put: trueLabel ].
falseLabel ifNotNil: [ questions at: 'n' put: falseLabel ].
cancelLabel ifNotNil: [ questions at: 'c' put: cancelLabel ].
^ self choose: questions title: queryString
]
{ #category : #debug }
CommandLineUIManager >> debugProcess: process context: context label: title fullView: bool notification: notificationString [
| description |
description := String streamContents: [ :stream |
stream
nextPutAll: 'debugging process: ';
nextPutAll: process name;
cr;
nextPutAll: 'context:';
cr;
print: context.
notificationString ifNotNil: [ stream nextPutAll: notificationString ] ].
self
logTitle: title
andDescription: description
to: self stderr
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> deny: aStringOrText [
"Open a denial dialog."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> deny: aStringOrText title: aString [
"Open a denial dialog."
]
{ #category : #utilities }
CommandLineUIManager >> exitFailure [
[
self class snapshotErrorImage ifTrue: [
doNotQuitOnRestart := true.
"make a new image version snapshot before leaving"
Smalltalk saveAsNewVersion ].
] ensure: [
doNotQuitOnRestart ifFalse: [ Smalltalk exitFailure ].
doNotQuitOnRestart := false.
].
]
{ #category : #'error handling' }
CommandLineUIManager >> handleError: anError log: shouldLog [
shouldLog ifTrue: [ self logError: anError ].
]
{ #category : #accessing }
CommandLineUIManager >> headlessManager [
self class == CommandLineUIManager
ifFalse: [ ^ self ].
^ CommandLineUIManager replacing: uiManager
]
{ #category : #'ui requests' }
CommandLineUIManager >> inform: aString [
| logBlock |
"Just log notifications"
(ProvideAnswerNotification signal: aString) ifNotNil: [:answer | ^true].
logBlock := [:logger |
logger cr;
nextPutAll: (String new: 79 withAll: $= ); cr;
nextPutAll: 'Notice: ';
nextPutAll: aString; cr;
nextPutAll: (String new: 79 withAll: $= ); cr].
Smalltalk logDuring: logBlock.
]
{ #category : #initialization }
CommandLineUIManager >> initialize [
doNotQuitOnRestart := false.
]
{ #category : #utilities }
CommandLineUIManager >> logColored: anAnsiiColorCode during: aBlock [
Smalltalk logStdErrorDuring: [:stderr |
stderr
nextPut: Character escape;
nextPut: $[; nextPutAll: anAnsiiColorCode; nextPut: $m.
aBlock value: stderr.
stderr nextPut: Character escape; nextPutAll: '[0m'
]
]
{ #category : #utilities }
CommandLineUIManager >> logDuring: aBlock [
Smalltalk logStdErrorDuring: [ :stderr | aBlock value: stderr ]
]
{ #category : #utilities }
CommandLineUIManager >> logGreenDuring: aBlock [
^ self logColored: '32' during: aBlock
]
{ #category : #utilities }
CommandLineUIManager >> logRedDuring: aBlock [
^ self logColored: '31' during: aBlock
]
{ #category : #private }
CommandLineUIManager >> logTitle: aString andDescription: aStringOrText to: aStream [
aStream
nextPutAll: aString;
nextPutAll: ': ';
nextPutAll: aStringOrText asString;
cr.
]
{ #category : #utilities }
CommandLineUIManager >> logYellowDuring: aBlock [
^ self logColored: '33' during: aBlock
]
{ #category : #display }
CommandLineUIManager >> newDisplayDepthNoRestore: pixelSize [
"do nothing"
]
{ #category : #'non-interactive' }
CommandLineUIManager >> nonInteractiveManager [
" Answer an instance of non-interactive manager, which will be used when image runs headless.
We put it here, so subclasses can override it. "
^ NonInteractiveUIManager replacing: uiManager
]
{ #category : #events }
CommandLineUIManager >> onPrimitiveError: aString [
" log error and quit "
^ self quitFrom: thisContext sender withMessage: aString
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> proceed: aStringOrText [
"Open a proceed dialog."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> proceed: aStringOrText title: aString [
"Open a proceed dialog and answer true if not cancelled, false otherwise."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> question: aStringOrText [
"Open a question dialog."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> question: aStringOrText title: aString [
"Open a question dialog and answer true if yes,
false if no and nil if cancelled."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> questionWithoutCancel: aStringOrText [
"Open a question dialog."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> questionWithoutCancel: aStringOrText title: aString [
"Open a question dialog and answer true if yes,
false if no and nil if cancelled."
]
{ #category : #utilities }
CommandLineUIManager >> quitFrom: aContext withMessage: aString [
" log error and quit "
[ Smalltalk
logError: aString
inContext: aContext.
" Print stacks of all current processes "
Smalltalk logDuring: [:logger |
logger nextPutAll: 'Processes and their stacks: ';cr.
Process allInstances do: [:each | | ctx |
logger nextPutAll: 'Process: '; print: each; cr; nextPutAll: ' stack:'; cr; cr.
ctx := each isActiveProcess ifTrue: [ thisContext sender ] ifFalse: [ each suspendedContext ].
ctx ifNotNil: [
(ctx stackOfSize: 20) do: [:s | logger print: s; cr ]].
logger nextPutAll: '------------------------------'; cr; cr.
]].
] ensure: [ self exitFailure ]
]
{ #category : #private }
CommandLineUIManager >> replacing: aUIManager [
" save the aUIManager to restore it later, when we become interactive again "
uiManager := aUIManager.
self beDefault.
]
{ #category : #display }
CommandLineUIManager >> restoreDisplay [
"do nothing"
]
{ #category : #display }
CommandLineUIManager >> restoreDisplayAfter: aBlock [
"do nothing"
]
{ #category : #accessing }
CommandLineUIManager >> stderr [
^ Stdio stderr
]
{ #category : #accessing }
CommandLineUIManager >> stdin [
^ Stdio stdin
]
{ #category : #accessing }
CommandLineUIManager >> stdout [
^ Stdio stdout
]
{ #category : #'ui requests' }
CommandLineUIManager >> systemNotificationDefaultAction: aNotification [
Smalltalk logDuring: [:logger |
logger cr; nextPutAll: aNotification class name, ': ', aNotification messageText; cr ].
aNotification resume.
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> textEntry: aStringOrText [
"Open a text entry dialog."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> textEntry: aStringOrText title: aString [
"Open a text entry dialog."
]
{ #category : #'ui TEasilyThemed' }
CommandLineUIManager >> textEntry: aStringOrText title: aString entryText: defaultEntryText [
"Open a text entry dialog."
]
{ #category : #'ui requests' }
CommandLineUIManager >> uiProcess [
" receiver don't have a ui process, associated with it,
client should check explicitly if #uiProcess answers nil or not"
^ nil
]
{ #category : #'default actions' }
CommandLineUIManager >> unhandledErrorDefaultAction: anException [
self quitFrom: anException signalerContext withMessage: anException description.
UIManager default == self ifFalse: [
^ UIManager default unhandledErrorDefaultAction: anException
]
]
{ #category : #'ui requests' }
CommandLineUIManager >> warningDefaultAction: aWarning [
| logBlock |
"Pass all warnings, but log them"
logBlock := [:logger |
logger
cr;
nextPutAll: '*** Warning: ';
nextPutAll: aWarning description;
cr ].
Smalltalk logDuring: logBlock.
self logYellowDuring: logBlock.
aWarning resume.
]