From 8f78f4bb893738bfc7180c790576c8ac4fcbae54 Mon Sep 17 00:00:00 2001
From: Qinxuan Chen <koushiro.cqx@gmail.com>
Date: Wed, 18 May 2022 00:38:51 +0800
Subject: [PATCH] Migrate abandoned `prettytable-rs` to `comfy-table` (#11430)

* Migrate to comfy-table

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* disable the default features

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
---
 substrate/Cargo.lock                          | 109 +++---------------
 .../utils/frame/benchmarking-cli/Cargo.toml   |   2 +-
 .../frame/benchmarking-cli/src/machine/mod.rs |  25 ++--
 3 files changed, 34 insertions(+), 102 deletions(-)

diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock
index 201feb7e06d..14196e4f853 100644
--- a/substrate/Cargo.lock
+++ b/substrate/Cargo.lock
@@ -615,17 +615,6 @@ dependencies = [
  "constant_time_eq",
 ]
 
-[[package]]
-name = "blake2b_simd"
-version = "0.5.11"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587"
-dependencies = [
- "arrayref",
- "arrayvec 0.5.2",
- "constant_time_eq",
-]
-
 [[package]]
 name = "blake2b_simd"
 version = "1.0.0"
@@ -1048,6 +1037,17 @@ dependencies = [
  "bitflags",
 ]
 
+[[package]]
+name = "comfy-table"
+version = "5.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b103d85ca6e209388771bfb7aa6b68a7aeec4afbf6f0a0264bfbf50360e5212e"
+dependencies = [
+ "strum",
+ "strum_macros",
+ "unicode-width",
+]
+
 [[package]]
 name = "concurrent-queue"
 version = "1.2.2"
@@ -1665,17 +1665,6 @@ dependencies = [
  "dirs-sys-next",
 ]
 
-[[package]]
-name = "dirs"
-version = "1.0.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "3fd78930633bd1c6e35c4b42b1df7b0cbc6bc191146e512bb3bedf243fcc3901"
-dependencies = [
- "libc",
- "redox_users 0.3.5",
- "winapi",
-]
-
 [[package]]
 name = "dirs-sys"
 version = "0.3.6"
@@ -1683,7 +1672,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
 dependencies = [
  "libc",
- "redox_users 0.4.0",
+ "redox_users",
  "winapi",
 ]
 
@@ -1694,7 +1683,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
 dependencies = [
  "libc",
- "redox_users 0.4.0",
+ "redox_users",
  "winapi",
 ]
 
@@ -1844,12 +1833,6 @@ dependencies = [
  "zeroize",
 ]
 
-[[package]]
-name = "encode_unicode"
-version = "0.3.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
-
 [[package]]
 name = "enum-as-inner"
 version = "0.4.0"
@@ -2152,6 +2135,7 @@ dependencies = [
  "Inflector",
  "chrono",
  "clap 3.1.18",
+ "comfy-table",
  "frame-benchmarking",
  "frame-support",
  "frame-system",
@@ -2165,7 +2149,6 @@ dependencies = [
  "log",
  "memory-db",
  "parity-scale-codec",
- "prettytable-rs",
  "rand 0.8.4",
  "rand_pcg 0.3.1",
  "sc-block-builder",
@@ -4365,7 +4348,7 @@ version = "0.16.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "e3db354f401db558759dfc1e568d010a5d4146f4d3f637be1275ec4a3cf09689"
 dependencies = [
- "blake2b_simd 1.0.0",
+ "blake2b_simd",
  "blake2s_simd",
  "blake3",
  "core2",
@@ -6588,7 +6571,7 @@ dependencies = [
  "cfg-if 1.0.0",
  "instant",
  "libc",
- "redox_syscall 0.2.10",
+ "redox_syscall",
  "smallvec",
  "winapi",
 ]
@@ -6601,7 +6584,7 @@ checksum = "28141e0cc4143da2443301914478dc976a61ffdb3f043058310c70df2fed8954"
 dependencies = [
  "cfg-if 1.0.0",
  "libc",
- "redox_syscall 0.2.10",
+ "redox_syscall",
  "smallvec",
  "windows-sys",
 ]
@@ -6893,20 +6876,6 @@ dependencies = [
  "output_vt100",
 ]
 
-[[package]]
-name = "prettytable-rs"
-version = "0.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "0fd04b170004fa2daccf418a7f8253aaf033c27760b5f225889024cf66d7ac2e"
-dependencies = [
- "atty",
- "csv",
- "encode_unicode",
- "lazy_static",
- "term",
- "unicode-width",
-]
-
 [[package]]
 name = "primitive-types"
 version = "0.11.1"
@@ -7416,12 +7385,6 @@ dependencies = [
  "rand_core 0.3.1",
 ]
 
-[[package]]
-name = "redox_syscall"
-version = "0.1.57"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
-
 [[package]]
 name = "redox_syscall"
 version = "0.2.10"
@@ -7431,17 +7394,6 @@ dependencies = [
  "bitflags",
 ]
 
-[[package]]
-name = "redox_users"
-version = "0.3.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "de0737333e7a9502c789a36d7c7fa6092a49895d4faa31ca5df163857ded2e9d"
-dependencies = [
- "getrandom 0.1.16",
- "redox_syscall 0.1.57",
- "rust-argon2",
-]
-
 [[package]]
 name = "redox_users"
 version = "0.4.0"
@@ -7449,7 +7401,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
 dependencies = [
  "getrandom 0.2.3",
- "redox_syscall 0.2.10",
+ "redox_syscall",
 ]
 
 [[package]]
@@ -7684,18 +7636,6 @@ dependencies = [
  "thiserror",
 ]
 
-[[package]]
-name = "rust-argon2"
-version = "0.8.3"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb"
-dependencies = [
- "base64",
- "blake2b_simd 0.5.11",
- "constant_time_eq",
- "crossbeam-utils",
-]
-
 [[package]]
 name = "rustc-demangle"
 version = "0.1.18"
@@ -10791,22 +10731,11 @@ dependencies = [
  "cfg-if 1.0.0",
  "fastrand",
  "libc",
- "redox_syscall 0.2.10",
+ "redox_syscall",
  "remove_dir_all",
  "winapi",
 ]
 
-[[package]]
-name = "term"
-version = "0.5.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "edd106a334b7657c10b7c540a0106114feadeb4dc314513e97df481d5d966f42"
-dependencies = [
- "byteorder",
- "dirs",
- "winapi",
-]
-
 [[package]]
 name = "termcolor"
 version = "1.1.2"
diff --git a/substrate/utils/frame/benchmarking-cli/Cargo.toml b/substrate/utils/frame/benchmarking-cli/Cargo.toml
index 68be768012d..5f30e21b298 100644
--- a/substrate/utils/frame/benchmarking-cli/Cargo.toml
+++ b/substrate/utils/frame/benchmarking-cli/Cargo.toml
@@ -16,6 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
 chrono = "0.4"
 clap = { version = "3.1.18", features = ["derive"] }
 codec = { package = "parity-scale-codec", version = "3.0.0" }
+comfy-table = { version = "5.0.1", default-features = false }
 handlebars = "4.2.2"
 hash-db = "0.15.2"
 hex = "0.4.3"
@@ -26,7 +27,6 @@ lazy_static = "1.4.0"
 linked-hash-map = "0.5.4"
 log = "0.4.17"
 memory-db = "0.29.0"
-prettytable-rs = "0.8.0"
 rand = { version = "0.8.4", features = ["small_rng"] }
 rand_pcg = "0.3.1"
 serde = "1.0.136"
diff --git a/substrate/utils/frame/benchmarking-cli/src/machine/mod.rs b/substrate/utils/frame/benchmarking-cli/src/machine/mod.rs
index d5cd2420a38..5f27c719839 100644
--- a/substrate/utils/frame/benchmarking-cli/src/machine/mod.rs
+++ b/substrate/utils/frame/benchmarking-cli/src/machine/mod.rs
@@ -20,6 +20,12 @@
 
 pub mod hardware;
 
+use std::{boxed::Box, fs, path::Path};
+
+use clap::Parser;
+use comfy_table::{Row, Table};
+use log::{error, info, warn};
+
 use sc_cli::{CliConfiguration, Result, SharedParams};
 use sc_service::Configuration;
 use sc_sysinfo::{
@@ -27,11 +33,6 @@ use sc_sysinfo::{
 	benchmark_memory, benchmark_sr25519_verify, ExecutionLimit,
 };
 
-use clap::Parser;
-use log::{error, info, warn};
-use prettytable::{cell, row, table};
-use std::{boxed::Box, fmt::Debug, fs, path::Path};
-
 use crate::shared::check_build_profile;
 pub use hardware::{Metric, Requirement, Requirements, Throughput, SUBSTRATE_REFERENCE_HARDWARE};
 
@@ -160,7 +161,8 @@ impl MachineCmd {
 	/// Prints a human-readable summary.
 	fn print_summary(&self, requirements: Requirements, results: Vec<BenchResult>) -> Result<()> {
 		// Use a table for nicer console output.
-		let mut table = table!(["Category", "Function", "Score", "Minimum", "Result"]);
+		let mut table = Table::new();
+		table.set_header(["Category", "Function", "Score", "Minimum", "Result"]);
 		// Count how many passed and how many failed.
 		let (mut passed, mut failed) = (0, 0);
 		for (requirement, result) in requirements.0.iter().zip(results.iter()) {
@@ -217,15 +219,16 @@ impl MachineCmd {
 
 impl BenchResult {
 	/// Format [`Self`] as row that can be printed in a table.
-	fn to_row(&self, req: &Requirement) -> prettytable::Row {
+	fn to_row(&self, req: &Requirement) -> Row {
 		let passed = if self.passed { "✅ Pass" } else { "❌ Fail" };
-		row![
-			req.metric.category(),
-			req.metric.name(),
+		vec![
+			req.metric.category().into(),
+			req.metric.name().into(),
 			format!("{}", self.score),
 			format!("{}", req.minimum),
-			format!("{} ({: >5.1?} %)", passed, self.rel_score * 100.0)
+			format!("{} ({: >5.1?} %)", passed, self.rel_score * 100.0),
 		]
+		.into()
 	}
 }
 
-- 
GitLab