From edb0b2c9459a41bfa016e060a452066a10524faa Mon Sep 17 00:00:00 2001 From: Hourier <66951241+Hourier@users.noreply.github.com> Date: Sat, 1 Jul 2023 23:35:32 +0900 Subject: [PATCH] =?utf8?q?[Refactor]=20#3499=20input=5Fquantity()=20?= =?utf8?q?=E3=81=AE=E5=85=A5=E5=8A=9B=E9=83=A8=E3=82=92input=5Fstring()=20?= =?utf8?q?=E3=81=AB=E5=B7=AE=E3=81=97=E6=9B=BF=E3=81=88=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 数値以外を入力すると最大値になる仕様は維持する --- src/core/asking-player.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/core/asking-player.cpp b/src/core/asking-player.cpp index 94777d440..c6596c781 100644 --- a/src/core/asking-player.cpp +++ b/src/core/asking-player.cpp @@ -285,8 +285,8 @@ bool input_check_strict(PlayerType *player_ptr, std::string_view prompt, EnumCla } else { ss << "[y/n]"; } - const auto buf = ss.str(); + const auto buf = ss.str(); auto &rfu = RedrawingFlagsUpdater::get_instance(); if (auto_more) { rfu.set_flag(SubWindowRedrawingFlag::MESSAGE); @@ -372,9 +372,10 @@ std::optional input_command(std::string_view prompt, bool z_escape) } /* - * Request a "quantity" from the user - * - * Hack -- allow "command_arg" to specify a quantity + * @brief 数量をユーザ入力する + * @param max 最大値 (売出し商品の個数等) + * @param initial_prompt 初期値 + * @details 数値でない値 ('a'等)を入力したら最大数を選択したとみなす. */ int input_quantity(int max, std::string_view initial_prompt) { @@ -394,11 +395,11 @@ int input_quantity(int max, std::string_view initial_prompt) amt = code; if ((max != 1) && result) { if (amt > max) { - amt = max; + return max; } if (amt < 0) { - amt = 0; + return 0; } return amt; @@ -412,23 +413,22 @@ int input_quantity(int max, std::string_view initial_prompt) } msg_print(nullptr); - prt(prompt, 0, 0); - amt = 1; - char buf[80]{}; - strnfmt(buf, sizeof(buf), "%d", amt); - const auto res = askfor(buf, 6, false); - prt("", 0, 0); - if (!res) { + const auto input_amount = input_string(prompt, 6, "1"); + if (!input_amount) { return 0; } - if (isalpha(buf[0])) { + if (isalpha((*input_amount)[0])) { amt = max; } else { - amt = std::clamp(atoi(buf), 0, max); + try { + amt = std::clamp(std::stoi(*input_amount), 0, max); + } catch (const std::exception &) { + amt = 0; + } } - if (amt) { + if (amt > 0) { repeat_push(static_cast(amt)); } -- 2.11.0