@@ -15,7 +15,7 @@ defmodule RingLogger.Viewer do
15
15
16
16
@ headers [ "#" , "Level" , "Application" , "Message" , "Timestamp" ]
17
17
@ header_lines 2
18
- @ footer_lines 2
18
+ @ footer_lines 1
19
19
@ width_of_layout_items 53
20
20
@ min_log_width 30
21
21
@ min_log_entries 10
@@ -28,6 +28,8 @@ defmodule RingLogger.Viewer do
28
28
running: true ,
29
29
last_cmd_string: nil ,
30
30
current_page: 0 ,
31
+ last_page: 0 ,
32
+ per_page: 0 ,
31
33
screen_dims: % { w: 0 , h: 0 } ,
32
34
lowest_log_level: nil ,
33
35
before_boot: true ,
@@ -52,22 +54,18 @@ defmodule RingLogger.Viewer do
52
54
53
55
IO . puts ( "Starting RingLogger Viewer..." )
54
56
55
- starting_state = @ init_state |> get_log_snapshot ( )
56
-
57
- draw ( starting_state )
58
-
59
- :ok
57
+ @ init_state |> get_log_snapshot ( ) |> loop ( )
60
58
end
61
59
62
60
#### Drawing and IO Functions
61
+ defp loop ( % { running: false } = _state ) do
62
+ :ok
63
+ end
63
64
64
- defp draw ( state ) do
65
+ defp loop ( state ) do
65
66
screen_dims = get_screen_dims ( )
66
- new_state = % { state | screen_dims: screen_dims } |> do_draw ( )
67
67
68
- if new_state . running do
69
- draw ( new_state )
70
- end
68
+ state |> update_dimensions ( screen_dims ) |> do_draw ( ) |> loop ( )
71
69
end
72
70
73
71
defp get_screen_dims ( ) do
@@ -77,10 +75,34 @@ defmodule RingLogger.Viewer do
77
75
% { w: cols , h: rows }
78
76
end
79
77
78
+ defp update_dimensions ( % { screen_dims: screen_dims } = state , screen_dims ) do
79
+ # No changes
80
+ state
81
+ end
82
+
83
+ defp update_dimensions ( state , screen_dims ) do
84
+ % { state | screen_dims: screen_dims } |> recalculate_pagination ( )
85
+ end
86
+
87
+ defp recalculate_pagination ( state ) do
88
+ index = state . per_page * state . current_page
89
+
90
+ per_page = state . screen_dims . h - ( @ header_lines + @ footer_lines )
91
+ page_count = ceil ( length ( state . raw_logs ) / per_page )
92
+ last_page = page_count - 1
93
+
94
+ current_page = div ( index , per_page )
95
+
96
+ % {
97
+ state
98
+ | per_page: per_page ,
99
+ current_page: current_page ,
100
+ last_page: last_page
101
+ }
102
+ end
103
+
80
104
defp do_draw ( state ) do
81
- filtered_logs =
82
- state . raw_logs
83
- |> paginate_logs ( state )
105
+ filtered_logs = current_page ( state )
84
106
85
107
[
86
108
reset_screen ( ) ,
@@ -108,8 +130,7 @@ defmodule RingLogger.Viewer do
108
130
end
109
131
110
132
defp compute_prompt ( state ) do
111
- per_page = state . screen_dims . h - ( @ header_lines + @ footer_lines )
112
- prefix = "[#{ state . current_page } /#{ div ( length ( state . raw_logs ) , per_page ) } ] "
133
+ prefix = "[#{ state . current_page } /#{ state . last_page } ] "
113
134
114
135
level_suffix =
115
136
if state . lowest_log_level != nil do
@@ -194,7 +215,7 @@ defmodule RingLogger.Viewer do
194
215
split_segment
195
216
end
196
217
197
- % { state | raw_logs: entries |> apply_log_filters ( state ) }
218
+ % { state | raw_logs: entries |> apply_log_filters ( state ) } |> recalculate_pagination ( )
198
219
end
199
220
200
221
defp find_starting_index ( entries ) do
@@ -211,11 +232,11 @@ defmodule RingLogger.Viewer do
211
232
end
212
233
end
213
234
214
- defp paginate_logs ( entries , state ) do
215
- per_page = state . screen_dims . h - ( @ header_lines + @ footer_lines )
216
- current_index = state . current_page * per_page
235
+ defp current_page ( state ) do
236
+ page_first_index = state . current_page * state . per_page
237
+ page_last_index = page_first_index + state . per_page - 1
217
238
218
- Enum . slice ( entries , current_index .. ( current_index + per_page ) )
239
+ Enum . slice ( state . raw_logs , page_first_index .. page_last_index )
219
240
end
220
241
221
242
defp apply_log_filters ( entries , state ) do
@@ -287,7 +308,7 @@ defmodule RingLogger.Viewer do
287
308
end
288
309
289
310
defp command ( "b" , _cmd_string , state ) do
290
- % { state | before_boot: ! state . before_boot } |> get_log_snapshot ( )
311
+ % { state | before_boot: ! state . before_boot , current_page: 0 } |> get_log_snapshot ( )
291
312
end
292
313
293
314
defp command ( "l" , cmd_string , state ) do
@@ -304,22 +325,19 @@ defmodule RingLogger.Viewer do
304
325
305
326
defp command ( _ , _cmd_string , state ) , do: state
306
327
328
+ defp next_page ( % { current_page: p , last_page: p } = state ) , do: state
307
329
defp next_page ( % { current_page: n } = state ) , do: % { state | current_page: n + 1 }
308
- defp prev_page ( % { current_page: 0 } = state ) , do: % { state | current_page: 0 }
330
+ defp prev_page ( % { current_page: 0 } = state ) , do: state
309
331
defp prev_page ( % { current_page: n } = state ) , do: % { state | current_page: n - 1 }
310
332
311
333
defp jump_to_page ( cmd_string , state ) do
312
- split = String . split ( cmd_string )
313
-
314
- case length ( split ) do
315
- 1 ->
316
- per_page = state . screen_dims . h - ( @ header_lines + @ footer_lines )
317
- last_page = div ( length ( state . raw_logs ) , per_page )
318
- % { state | current_page: last_page }
334
+ case String . split ( cmd_string ) do
335
+ [ _ ] ->
336
+ % { state | current_page: state . last_page }
319
337
320
- 2 ->
321
- { page , _ } = Integer . parse ( Enum . at ( split , 1 ) )
322
- % { state | current_page: max ( 0 , page ) }
338
+ [ _ , page_string ] ->
339
+ { page , _ } = Integer . parse ( page_string )
340
+ % { state | current_page: min ( max ( 0 , page ) , state . last_page ) }
323
341
324
342
_ ->
325
343
state
@@ -347,41 +365,40 @@ defmodule RingLogger.Viewer do
347
365
end
348
366
349
367
defp set_log_level ( cmd_string , state ) do
350
- split = String . split ( cmd_string )
351
-
352
- cond do
353
- length ( split ) <= 1 ->
368
+ case { String . split ( cmd_string ) , state . lowest_log_level } do
369
+ { [ _cmd ] , previous } when previous != nil ->
354
370
# No args, clear the log level filter
355
- % { state | lowest_log_level: nil }
371
+ % { state | lowest_log_level: nil , current_page: 0 }
356
372
357
- length ( split ) == 2 and Enum . at ( split , 1 ) in @ level_strings ->
373
+ { [ _cmd , new_level ] , level } when new_level != level and new_level in @ level_strings ->
358
374
# 2 args, 2nd arg is a valid log level string
359
- level_str = Enum . at ( split , 1 )
360
- level_atom = String . to_existing_atom ( level_str )
361
- % { state | lowest_log_level: level_atom }
375
+ level_atom = String . to_existing_atom ( new_level )
376
+ % { state | lowest_log_level: level_atom , current_page: 0 }
362
377
363
- true ->
378
+ _ ->
364
379
state
365
380
end
366
381
end
367
382
368
383
defp add_remove_app ( cmd_string , state ) do
369
- split = String . split ( cmd_string )
370
-
371
- cond do
372
- length ( split ) == 1 ->
373
- % { state | applications_filter: [ ] }
384
+ case String . split ( cmd_string ) do
385
+ [ _cmd ] ->
386
+ % { state | applications_filter: [ ] , current_page: 0 }
374
387
375
- length ( split ) == 2 ->
376
- app_atom = String . to_existing_atom ( Enum . at ( split , 1 ) )
388
+ [ _cmd , app_str ] ->
389
+ app_atom = String . to_existing_atom ( app_str )
377
390
378
391
if app_atom in state . applications_filter do
379
- % { state | applications_filter: List . delete ( state . applications_filter , app_atom ) }
392
+ % {
393
+ state
394
+ | applications_filter: List . delete ( state . applications_filter , app_atom ) ,
395
+ current_page: 0
396
+ }
380
397
else
381
- % { state | applications_filter: [ app_atom | state . applications_filter ] }
398
+ % { state | applications_filter: [ app_atom | state . applications_filter ] , current_page: 0 }
382
399
end
383
400
384
- true ->
401
+ _ ->
385
402
state
386
403
end
387
404
rescue
@@ -393,15 +410,15 @@ defmodule RingLogger.Viewer do
393
410
394
411
case Regex . compile ( str ) do
395
412
{ :ok , expression } ->
396
- % { state | grep_filter: expression }
413
+ % { state | grep_filter: expression , current_page: 0 }
397
414
398
415
_ ->
399
416
state
400
417
end
401
418
rescue
402
419
# We can't force String.split/2 with parts = 2 to not raise if they only provide 1 arg
403
420
# So treat this as the reset condition
404
- _ -> % { state | grep_filter: nil }
421
+ _ -> % { state | grep_filter: nil , current_page: 0 }
405
422
end
406
423
407
424
defp show_help ( state ) do
0 commit comments