Comments (10)
Hi @sphinxc0re
First, you are not adding item_cell
to the table, but item.name
instead.
But even doing this won't be enough to display the colour correctly. In fact, you should not use the row!
macro to create a new row from cells, since the macro will call to_string()
on the cell in order to create a new cell. The macros are use to create tables/rows/cells from elements which can be converted to strings. Maybe I should consider refactoring this.
Heres what you should fo instead :
for .. in .. {
let mut index_cell = Cell::from(&index);
index_cell.align(Alignment::RIGHT);
let mut item_cell = Cell::from(&item.name);
item_cell.style(Attr::ForegroundColor(0x00AAFF));
table.add_row(Row::new(vec![index_cell, item_cell, cell!(amount)]));
}
from prettytable-rs.
By the way, I'm not 100% sure you can use 0x00AAFF
as a foreground color. Sorry I can't check this right now.
This depends on the term
crate and the colors should be one of those.
from prettytable-rs.
Thank you for your fast response @phsym
I will try to refactor it as shown above
from prettytable-rs.
you're welcome
from prettytable-rs.
@sphinxc0re any update ?
from prettytable-rs.
@phsym I removed the dependency from my project entirely but only to make it a crate. When I write the first implementation, I'll definitely reconsider using prettytable-rs
from prettytable-rs.
I'm closing issue for now. Feel free to reopen it later
from prettytable-rs.
So i'm not sure the best solution here, I noticed that the style spec isn't showing up for name_cell
below, and I believe it's due to what you said above about to_string
being called.
what would be the best way to mix and match style strings in the case below?
let ph_name_table = |phdr: &elf::ProgramHeader| {
let typ = phdr.p_type;
let name = program_header::pt_to_str(typ);
match typ {
program_header::PT_LOAD => Cell::new(name).style_spec("Fr"),
program_header::PT_INTERP => Cell::new(name).style_spec("Fy"),
program_header::PT_DYNAMIC => Cell::new(name).style_spec("Fc"),
_ => Cell::new(name),
}
};
for (i, phdr) in phdrs.into_iter().enumerate() {
let name_cell = ph_name_table(&phdr);
let flags = ph_flag(&phdr);
table.add_row(row![
format!("{} ", i),
name_cell,
format!("{:>4} ", flags),
Fy->format!("{:<#x} ", phdr.p_offset),
Fr->format!("{:<#x} ", phdr.p_vaddr),
bFr->format!("{:<#x} ", phdr.p_paddr),
Fg->format!("{:<#x} ", phdr.p_filesz),
bFg->format!("{:<#x} ", phdr.p_memsz),
format!("{:#x}", phdr.p_align)
]);
}
table.print_tty(self.opt.color);
from prettytable-rs.
Well, I would write
table.add_row(Row::new(vec![
cell!(format!("{} ", i)),
name_cell,
cell!(format!("{:>4} ", flags)),
cell!(Fy->format!("{:<#x} ", phdr.p_offset)),
cell!(Fr->format!("{:<#x} ", phdr.p_vaddr)),
cell!(bFr->format!("{:<#x} ", phdr.p_paddr)),
cell!(Fg->format!("{:<#x} ", phdr.p_filesz)),
cell!(bFg->format!("{:<#x} ", phdr.p_memsz)),
cell!(format!("{:#x}", phdr.p_align))
]));
I'm agree this could probably made better by adding other macros and / or refactoring the existing ones.
from prettytable-rs.
Maybe
cell!(Fy->format!("{:<#x} ", phdr.p_offset))
could be made into
format_cell!("Fy", "{:<#x} ", phdr.p_offset)
or maybe we could refacor the cell!
macro so that
cell!(Fg -> "foobar")
could be written
cell!("Fg":"foobar")
(But need to find another symbol than :
)
Then you could store the style string into a variable and pass it later during table contruction
from prettytable-rs.
Related Issues (20)
- Using more than one character as separator
- term has security warning, please update HOT 1
- Question: Slice Usage With Row Macro
- Support styling options / colors for borders and separators HOT 2
- Crash due to subtract with overflow HOT 4
- Allow skipping line separators HOT 2
- Mark crate as `deprecated` on crates.io HOT 1
- TableSlice::print_tty() should not panic on I/O errors HOT 1
- output of a table to a string with styles -- is it possible? HOT 5
- Some suggestions to simplify the code
- Replace atty dependency in prettytable-rs due to RUSTSEC-2021-0145 HOT 2
- force casting Vec<T> to &[T] in Table::AsRef is causing trouble HOT 12
- Release 0.10.0 HOT 2
- Make Color optional HOT 1
- Release x.y.z
- Apply dynamic styles to nested table HOT 4
- Could we get some form of Format Macro? HOT 1
- Display Train over Table Object
- .printhtml() method doesnt insert table headers (titles) correctly HOT 2
- Unexpected `FromIterator<Utf8Char> for Vec<u8>` import by just importing prettytable-rs
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from prettytable-rs.