Skip to content

Commit 81592ab

Browse files
Fix: table formatting and adjust print spacing (#72)
* style: Adjust print spacing in core game loop * fix: Resolve a table formatting issue on load menu fixes O-81
1 parent e5aeaf0 commit 81592ab

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

lib/console/console.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,7 @@ def handle_command(cmd, opt_arg, cmds, is_valid, cmd_err: D_MSG[:cmd_err])
164164
def table_formatter(data_arr)
165165
row_counts = data_arr.size
166166
prefix_size = ol_prefix(row_counts).size
167-
auto_pad = row_counts * 2 + D_MSG[:v_sep].size
168-
max_length = data_arr.map { |arr| arr.max_by(&:size) }.sum(&:size) + auto_pad + prefix_size
169-
max_length = 80 if max_length > 80
167+
max_length = 80
170168
[prefix_size, max_length]
171169
end
172170

lib/console_game/chess/board.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Board
1111
include Console
1212
include Display
1313

14-
attr_accessor :board_size, :board_side, :board_padding, :flip_board, :highlight, :live_board
14+
attr_accessor :board_size, :board_side, :board_padding, :flip_board, :highlight
1515
attr_reader :level, :type_hl, :alg_pos_hl
1616

1717
# @param level [Level] chess Level object
@@ -42,16 +42,16 @@ def print_turn(event_msgs = [""])
4242
system("clear")
4343
# print "\e[2J\e[H"
4444

45-
print_msg(*event_msgs, pre: "⠗ ") unless event_msgs.empty?
45+
print_msg(*event_msgs, pre: "\n⠗ ") unless event_msgs.empty?
4646
print_chessboard
4747
level.event_msgs.clear
48-
# self.live_board = 0
4948
end
5049

5150
# Print the chessboard
5251
def print_chessboard
52+
puts "\n"
5353
print_msg(*build_chessboard, pre: "".ljust(board_padding), clear: false)
54-
# self.live_board += 1
54+
puts "\n"
5555
end
5656

5757
# Enable & disable board flipping
@@ -79,7 +79,6 @@ def display_configs
7979
@board_side = :white
8080
@highlight = THEME[:classic].slice(:icon, :highlight)
8181
@type_hl, @alg_pos_hl = MSG_HIGHLIGHT[:std].values_at(:type, :alg_pos)
82-
@live_board = 0
8382
end
8483

8584
# Pre-process turn data before sending it to display module

lib/console_game/chess/level.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Level
3434
attr_reader :controller, :w_player, :b_player, :session, :board, :kings, :castling_states, :threats_map,
3535
:usable_pieces, :opponent
3636

37-
# @param input [ChessInput]
37+
# @param input [ChessInput]``
3838
# @param sides [Hash]
3939
# @option sides [ChessPlayer, ChessComputer] :white Player who plays as White
4040
# @option sides [ChessPlayer, ChessComputer] :black Player who plays as Black
@@ -67,7 +67,7 @@ def refresh(print_turn: true)
6767
update_board_state
6868
game_end_check
6969
add_check_marker
70-
board.print_turn(event_msgs) if print_turn
70+
board.print_turn(event_msgs) if print_turn || game_ended
7171
end
7272

7373
# Board state refresher
@@ -154,7 +154,9 @@ def init_level
154154
# greet player on load, message should change depending on load state
155155
def greet_player
156156
keypath = full_move == 1 ? "session.new" : "session.load"
157-
event_msgs.push(board.s(keypath, { event: session[:event], p1: player.name }))
157+
event_msgs.push(board.s(keypath, {
158+
event: [session[:event].sub("Status", "| Status:"), "gold"], p1: player.name
159+
}))
158160
end
159161

160162
# Pre-turn flow
@@ -204,8 +206,7 @@ def handle_result(type:, side: nil)
204206
save_turn
205207
winner = session[opposite_of(side)]
206208
kings[side].color = "#CC0000" if type == "checkmate"
207-
event_msgs << board.s("level.endgame.#{type}", { win_player: winner })
208-
board.print_turn(event_msgs[-1])
209+
event_msgs << board.s("level.endgame.#{type}", { win_player: [winner, "gold"] })
209210
@game_ended = true
210211
end
211212

lib/console_game/chess/utilities/session_builder.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def build_session
3333
session = { mode: mode,
3434
white: wp_name,
3535
black: bp_name,
36-
event: "#{wp_name} vs #{bp_name} status-#{ongoing_txt}",
36+
event: "#{wp_name} vs #{bp_name} Status #{ongoing_txt}",
3737
site: site_txt,
3838
date: Time.new.ceil.strftime(STR_TIME) }
3939
[id, session]

spec/console/console_spec.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919
end
2020

2121
let(:expected_output) do
22-
[
23-
"List of Sessions",
24-
"---------------------------------------------------------------------------",
25-
"Event | Date",
26-
"---------------------------------------------------------------------------",
27-
"* [1] - Q vs Picard 07/10/2025 12:00 AM",
28-
"* [2] - Spock vs Data 07/15/2025 12:00 AM",
29-
"* [3] - Voyager vs Enterprise 07/18/2025 12:00 AM"
30-
]
22+
["List of Sessions",
23+
"--------------------------------------------------------------------------------",
24+
"Event | Date",
25+
"--------------------------------------------------------------------------------",
26+
"* [1] - Q vs Picard 07/10/2025 12:00 AM",
27+
"* [2] - Spock vs Data 07/15/2025 12:00 AM",
28+
"* [3] - Voyager vs Enterprise 07/18/2025 12:00 AM"]
3129
end
3230

3331
it "returns a table in a form of a string" do

0 commit comments

Comments
 (0)