Skip to content

Commit 78ef191

Browse files
Feature/colorful (#73)
* colorful messages in envgen * Add colors to tester and submit
1 parent 78c6f8c commit 78ef191

File tree

4 files changed

+33
-12
lines changed

4 files changed

+33
-12
lines changed

atcodertools/tools/envgen.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from time import sleep
1010
from typing import Tuple, Optional
1111

12+
from colorama import Fore
13+
1214
from atcodertools.client.atcoder import AtCoderClient, Contest, LoginError
1315
from atcodertools.client.models.problem import Problem
1416
from atcodertools.client.models.problem_content import InputFormatDetectionError, SampleDetectionError
@@ -21,6 +23,7 @@
2123
from atcodertools.fmtprediction.predict_format import NoPredictionResultError, \
2224
MultiplePredictionResultsError, predict_format
2325
from atcodertools.tools.models.metadata import Metadata
26+
from atcodertools.tools.utils import with_color
2427

2528
script_dir_path = os.path.dirname(os.path.abspath(__file__))
2629

@@ -76,7 +79,7 @@ def prepare_procedure(atcoder_client: AtCoderClient,
7679
pid)
7780

7881
def emit_error(text):
79-
logging.error("Problem {}: {}".format(pid, text))
82+
logging.error(with_color("Problem {}: {}".format(pid, text), Fore.RED))
8083

8184
def emit_warning(text):
8285
logging.warning("Problem {}: {}".format(pid, text))
@@ -143,7 +146,9 @@ def emit_info(text):
143146
code_file_path
144147
)
145148
emit_info(
146-
"Prediction succeeded -- Saved auto-generated code to '{}'".format(code_file_path))
149+
"{} -- Saved auto-generated code to '{}'".format(
150+
with_color("Prediction succeeded", Fore.LIGHTGREEN_EX),
151+
code_file_path))
147152
except (NoPredictionResultError, MultiplePredictionResultsError) as e:
148153
if isinstance(e, NoPredictionResultError):
149154
msg = "No prediction -- Failed to understand the input format"
@@ -154,7 +159,7 @@ def emit_info(text):
154159
shutil.copy(replacement_code_path, code_file_path)
155160
emit_warning(
156161
"{} -- Copied {} to {}".format(
157-
msg,
162+
with_color(msg, Fore.LIGHTRED_EX),
158163
replacement_code_path,
159164
code_file_path))
160165

atcodertools/tools/submit.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import sys
55
import os
66

7+
from colorama import Fore
8+
9+
from atcodertools.tools.utils import with_color
10+
711
from atcodertools.client.atcoder import AtCoderClient, LoginError
812
from atcodertools.tools import tester
913

@@ -90,9 +94,9 @@ def main(prog, args, credential_supplier=None, use_local_session_cache=True) ->
9094
if not args.unlock_safety:
9195
for submission in submissions:
9296
if submission.problem_id == metadata.problem.problem_id:
93-
logging.error("Cancel submitting because you already sent some code to the problem. Please "
94-
"specify -u to send the code. {}".format(
95-
metadata.problem.contest.get_submissions_url(submission)))
97+
logging.error(with_color("Cancel submitting because you already sent some code to the problem. Please "
98+
"specify -u to send the code. {}".format(
99+
metadata.problem.contest.get_submissions_url(submission)), Fore.LIGHTRED_EX))
96100
return False
97101

98102
code_path = os.path.join(args.dir, metadata.code_filename)
@@ -102,7 +106,8 @@ def main(prog, args, credential_supplier=None, use_local_session_cache=True) ->
102106
logging.info("Submitting {} as {}".format(code_path, detailed_lang))
103107
submission = client.submit_source_code(
104108
metadata.problem.contest, metadata.problem, detailed_lang, source)
105-
logging.info("Done! {}".format(
109+
logging.info("{} {}".format(
110+
with_color("Done!", Fore.LIGHTGREEN_EX),
106111
metadata.problem.contest.get_submissions_url(submission)))
107112

108113

atcodertools/tools/tester.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010
from pathlib import Path
1111
from typing import List, Tuple
1212

13+
from colorama import Fore
14+
1315
from atcodertools.tools.models.metadata import Metadata
16+
from atcodertools.tools.utils import with_color
1417

1518

1619
class NoExecutableFileError(Exception):
@@ -121,13 +124,15 @@ def run_for_samples(exec_file: str, sample_pair_list: List[Tuple[str, str]], tim
121124

122125
is_correct = exec_res.is_correct_output(answer_text)
123126
if is_correct:
124-
message = "PASSED {elapsed} ms".format(elapsed=exec_res.elapsed_ms)
127+
message = "{} {elapsed} ms".format(
128+
with_color("PASSED", Fore.LIGHTGREEN_EX),
129+
elapsed=exec_res.elapsed_ms)
125130
success_count += 1
126131
else:
127132
if exec_res.status == ExecStatus.NORMAL:
128-
message = "WA"
133+
message = with_color("WA", Fore.LIGHTRED_EX)
129134
else:
130-
message = exec_res.status.name
135+
message = with_color(exec_res.status.name, Fore.LIGHTYELLOW_EX)
131136

132137
print("# {case_name} ... {message}".format(
133138
case_name=os.path.basename(in_sample_file),
@@ -198,13 +203,14 @@ def run_all_tests(exec_file, in_sample_file_list, out_sample_file_list, timeout_
198203
print("No test cases")
199204
return False
200205
elif success_count != len(samples):
201-
print("Some cases FAILED (passed {success_count} of {total})".format(
206+
print("{msg} (passed {success_count} of {total})".format(
207+
msg=with_color("Some cases FAILED", Fore.LIGHTRED_EX),
202208
success_count=success_count,
203209
total=len(samples),
204210
))
205211
return False
206212
else:
207-
print("Passed all test cases!!!")
213+
print(with_color("Passed all test cases!!!", Fore.LIGHTGREEN_EX))
208214
return True
209215

210216

atcodertools/tools/utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from colorama import Fore
2+
3+
4+
def with_color(msg, color):
5+
return "{}{}{}".format(color, msg, Fore.RESET)

0 commit comments

Comments
 (0)