Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get multiple, different colorized output for a list element #58

Closed
listx opened this issue May 25, 2016 · 10 comments
Closed

Get multiple, different colorized output for a list element #58

listx opened this issue May 25, 2016 · 10 comments

Comments

@listx
Copy link

listx commented May 25, 2016

Not sure if this is a question or a feature request, but please bear with me.

From what I can tell, there is no way to assign multiple color attributes to text within a single List widget element.

What I have is basically a List of multiple strings, with each string being a particular "type", much like a spreadsheet. E.g.,

foo bar quux
foo bar quux
foo bar quux

and I want all the foo strings to be a certain color distinct from all the bar strings, and so on.

As I write this I realize that I could probably just have multiple List widgets, one for each "column" (one for foo, one for bar, etc.) and assign a different color attribute for each widget (and have them aligned horizontally, right?), but is there a way to just embed different colors into a single List widget item?
I feel that having multiple List widgets seems hacky and overly complicated.

@jtdaugherty
Copy link
Owner

This is possible using the attribute management combinators; you can find them in the "Attribute management" section of Brick.Widgets.Core:

http://hackage.haskell.org/package/brick-0.6.4/docs/Brick-Widgets-Core.html#g:5

But the easiest way is to use withDefAttr, e.g.,

listWidget =
  hBox [ withDefAttr fooAttr $ str "foo"
       , str " "
       , withDefAttr barAttr $ str "bar"
       , str " "
       , withDefAttr quuxAttr $ str "quux"
       ]

and then be sure to assign those attribute map entries in your attribute map, e.g.,

App { appAttrMap = const $ attrMap defAttr mapping
    }
mapping =
  [ (fooAttr,  fg white)
  , (barAttr,  fg red)
  , (quuxAttr, blue `on` green)
  ]

You can also see some examples of the attribute API at work in programs/AttrDemo.hs.

If this helps, please feel free to close the issue; otherwise let me know what I can do. Thanks!

@listx
Copy link
Author

listx commented May 26, 2016

Oh cool, I will have a look at this and report back my findings.

On Wed, May 25, 2016 at 4:50 PM, Jonathan Daugherty <
notifications@github.com> wrote:

This is possible using the attribute management combinators; you can find
them in the "Attribute management" section of Brick.Widgets.Core:

http://hackage.haskell.org/package/brick-0.6.4/docs/Brick-Widgets-Core.html#g:5

But the easiest way is to use withDefAttr, e.g.,

listWidget =
hBox [ withDefAttr fooAttr $ str "foo"
, str " "
, withDefAttr barAttr $ str "bar"
, str " "
, withDefAttr quuxAttr $ str "quux"
]

and then be sure to assign those attribute map entries in your attribute
map, e.g.,

App { appAttrMap = const $ attrMap defAttr mapping
}
mapping =
[ (fooAttr, fg white)
, (barAttr, fg red)
, (quuxAttr, blue on green)
]

You can also see some examples of the attribute API at work in
programs/AttrDemo.hs.

If this helps, please feel free to close the issue; otherwise let me know
what I can do. Thanks!


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
#58 (comment)

@listx
Copy link
Author

listx commented May 26, 2016

Thanks, I got it working. I don't understand why I was thinking so narrowly before. The key is that the List widget comes with the wonderful renderList function, which allows one to supply a custom rendering function for the arbitrary element type in the List widget. My List elements are of a custom type and I used your hBox [<widgets>] approach to populate a list of str widgets for each element's subparts ("foo", "bar", and "quux" in my original post).

Thanks again!

@listx listx closed this as completed May 26, 2016
@jtdaugherty
Copy link
Owner

Great! I'm glad it's working out for you. Keep in mind that if you think of something about the docs that could have made this clearer, I'm interested to know what that would be.

@listx
Copy link
Author

listx commented May 26, 2016

Hm, maybe the docs could say that we could simulate a spreadsheet by having each element in a List widget be composed of multiple 'column' subparts, optionally colored differently? Or just implement it in a new ListDemo2.hs program?

@jtdaugherty
Copy link
Owner

Actually, the existing ListDemo already does this using withAttr (which is similar to withDefAttr).

@listx
Copy link
Author

listx commented May 26, 2016

Yes it does, although I think it would be clearer if visually it had something more like a spreadsheet. Shrug but then again it could be just me. Your call!

@jtdaugherty
Copy link
Owner

I only hesitate to program a spreadsheet as a demo because it's such a specific application idea, and I'm concerned that having a demo app for every way to use brick would get unmanageable because it's intended to let you do whatever you want. :)

With that said, I can see how it wouldn't be obvious to do what you want to do from the outset, and it will require learning the library well enough to imagine ways to get the layout you desire. My intention is to provide enough basic building blocks that just about anything can be built from them, if they are well understood.

@listx
Copy link
Author

listx commented May 26, 2016

I agree with your concerns. Well, in the coming month(s) I will be
releasing a project that uses all of this functionality so maybe a
screenshot of that project would be good enough. We could have a "Brick
usage examples in the wild" section either in the Github wiki or directly
in the README and have links to screenshots and such.

On Thu, May 26, 2016 at 3:22 PM, Jonathan Daugherty <
notifications@github.com> wrote:

I only hesitate to program a spreadsheet as a demo because it's such a
specific application idea, and I'm concerned that having a demo app for
every way to use brick would get unmanageable because it's intended to let
you do whatever you want. :)

With that said, I can see how it wouldn't be obvious to do what you want
to do from the outset, and it will require learning the library well enough
to imagine ways to get the layout you desire. My intention is to provide
enough basic building blocks that just about anything can be built from
them, if they are well understood.


You are receiving this because you modified the open/close state.
Reply to this email directly, view it on GitHub
#58 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAsSbQQ20_1riJoj2__ZCGGUnjF_yAYOks5qFh0xgaJpZM4Im7jE
.

@jtdaugherty
Copy link
Owner

That would be fantastic! I would be happy to add it to the README.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants