Skip to content

Commit

Permalink
Add a new tabular layout for the contract storage data (#1485)
Browse files Browse the repository at this point in the history
  • Loading branch information
smiasojed authored Feb 1, 2024
1 parent 6ef21d3 commit f5f7201
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 99 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed
- Mandatory dylint-based lints - [#1412](https://github.com/paritytech/cargo-contract/pull/1412)
- Add a new tabular layout for the contract storage data - [#1485](https://github.com/paritytech/cargo-contract/pull/1485)

## [4.0.0-rc.1]

Expand Down
33 changes: 32 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/cargo-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ semver = "1.0"
jsonschema = "0.17"
schemars = "0.8"
ink_metadata = "5.0.0-rc"
crossterm = "0.27.0"
comfy-table = "7.1.0"

# dependencies for extrinsics (deploying and calling a contract)
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
Expand Down
130 changes: 33 additions & 97 deletions crates/cargo-contract/src/cmd/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
// along with cargo-contract. If not, see <http://www.gnu.org/licenses/>.

use super::DefaultConfig;
use anyhow::{
anyhow,
Result,
};
use anyhow::Result;
use colored::Colorize;
use comfy_table::{
ContentArrangement,
Table,
};
use contract_extrinsics::{
ContractArtifacts,
ContractStorage,
ContractStorageLayout,
ContractStorageRpc,
ErrorVariant,
};
use crossterm::terminal;
use std::{
cmp,
path::PathBuf,
};
use std::path::PathBuf;
use subxt::Config;

#[derive(Debug, clap::Args)]
Expand Down Expand Up @@ -96,8 +93,8 @@ impl StorageCommand {
json = serde_json::to_string_pretty(&contract_storage)?
);
} else {
let table = StorageDisplayTable::new(&contract_storage)?;
table.display()?;
let table = StorageDisplayTable::new(&contract_storage);
table.display();
}
}
Err(_) => {
Expand All @@ -120,110 +117,49 @@ impl StorageCommand {
}
}

struct StorageDisplayTable<'a> {
storage_layout: &'a ContractStorageLayout,
parent_width: usize,
value_width: usize,
}
struct StorageDisplayTable(Table);

impl<'a> StorageDisplayTable<'a> {
const KEY_WIDTH: usize = 8;
const INDEX_WIDTH: usize = 5;
impl StorageDisplayTable {
const INDEX_LABEL: &'static str = "Index";
const KEY_LABEL: &'static str = "Root Key";
const PARENT_LABEL: &'static str = "Parent";
const VALUE_LABEL: &'static str = "Value";

fn new(storage_layout: &'a ContractStorageLayout) -> Result<Self> {
let parent_len = storage_layout
.iter()
.map(|c| c.parent().len())
.max()
.unwrap_or_default();
let parent_width = cmp::max(parent_len, Self::PARENT_LABEL.len());
let terminal_width = terminal::size().unwrap_or((80, 24)).0 as usize;

// There are tree separators in the table ' | '
let value_width = terminal_width
.checked_sub(Self::KEY_WIDTH + Self::INDEX_WIDTH + 3 * 3 + parent_width)
.filter(|&w| w > Self::VALUE_LABEL.len())
.ok_or(anyhow!(
"Terminal width to small to display the storage layout correctly"
))?;

Ok(Self {
storage_layout,
parent_width,
value_width,
})
fn new(storage_layout: &ContractStorageLayout) -> Self {
let mut table = Table::new();
Self::table_add_header(&mut table);
Self::table_add_rows(&mut table, storage_layout);
Self(table)
}

fn table_row_println(&self, index: usize, key: &str, parent: &str, value: &str) {
let mut result = value.split_whitespace().fold(
(Vec::new(), String::new()),
|(mut result, mut current_line), word| {
if current_line.len() + word.len() + 1 > self.value_width {
if !current_line.is_empty() {
result.push(current_line.clone());
current_line.clear();
}
current_line.push_str(word);
(result, current_line)
} else {
if !current_line.is_empty() {
current_line.push(' ');
}
current_line.push_str(word);
(result, current_line)
}
},
);

if !result.1.is_empty() {
result.0.push(result.1);
}
fn table_add_header(table: &mut Table) {
table.set_content_arrangement(ContentArrangement::Dynamic);

for (i, value) in result.0.iter().enumerate() {
println!(
"{:<index_width$} | {:<key_width$} | {:<parent_width$} | {:<value_width$.value_width$}",
if i == 0 { index.to_string() } else { String::new() },
if i == 0 { key } else { "" },
if i == 0 { parent } else { "" },
value,
index_width = Self::INDEX_WIDTH,
key_width = Self::KEY_WIDTH,
parent_width = self.parent_width,
value_width = self.value_width,
);
}
let header = vec![
Self::INDEX_LABEL,
Self::KEY_LABEL,
Self::PARENT_LABEL,
Self::VALUE_LABEL,
];
table.set_header(header);
}

fn display(&self) -> Result<()> {
// Print the table header
println!(
"{:<index_width$} | {:<key_width$} | {:<parent_width$} | {:<value_width$.value_width$}",
Self::INDEX_LABEL.bright_purple().bold(),
Self::KEY_LABEL.bright_purple().bold(),
Self::PARENT_LABEL.bright_purple().bold(),
Self::VALUE_LABEL.bright_purple().bold(),
index_width = Self::INDEX_WIDTH,
key_width = Self::KEY_WIDTH,
parent_width = self.parent_width,
value_width = self.value_width,
);

for (index, cell) in self.storage_layout.iter().enumerate() {
fn table_add_rows(table: &mut Table, storage_layout: &ContractStorageLayout) {
for (index, cell) in storage_layout.iter().enumerate() {
let formatted_cell = format!("{cell}");
let values = formatted_cell.split('\n');
for (i, v) in values.enumerate() {
self.table_row_println(
index + i,
table.add_row(vec![
(index + i).to_string().as_str(),
cell.root_key().as_str(),
cell.parent().as_str(),
v,
);
]);
}
}
Ok(())
}

fn display(&self) {
println!("{}", self.0);
}
}

0 comments on commit f5f7201

Please sign in to comment.