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

Question about background. #2

Closed
Chaos4U opened this issue Oct 29, 2022 · 6 comments
Closed

Question about background. #2

Chaos4U opened this issue Oct 29, 2022 · 6 comments

Comments

@Chaos4U
Copy link

Chaos4U commented Oct 29, 2022

@Kofituo , Hello i was trying to figure out how to set a background at exact position (like from (x1,y1) to (x10,y10) ) on screen, but i couldn't figure it out, so i thought about asking it here.

@Kofituo
Copy link
Owner

Kofituo commented Oct 30, 2022

Could you explain further? What background are you referring to?

@Chaos4U
Copy link
Author

Chaos4U commented Oct 31, 2022

SetBackgroundColor struct in crossterm.
I want to display something like this

  1. To create some autocomplete functionality.
    preview

  2. Or some mini window like this
    To create many stuff like file explorer, find file, etc.
    preview_two

@Kofituo
Copy link
Owner

Kofituo commented Nov 1, 2022

Okay I understand.

The colors are set within the String printed to the terminal. So you'd have to color them appropriately.

  1. You could create a string wrapper, similar to what I've in my tutorial.
  2. Create a push / push_str functions and then check if the new string being added falls with the specified range. You could simply do so by checking the len of the str being added.
  3. If it falls within your specified range, first add SetBackgroundColor(color) to your string. Next add the range of the string you want to color. Reset set the background color. And then add the remaining range.
  4. Alternatively, since you want to color a fixed range, a more convenient option maybe to modify your output string just before you write it to the terminal. In this method, you do not need to create a new type: Add SetBackgroundColor(color) before your start index, then reset background color after your end index. Since this method inserts randomly into a string, it might be slightly slower than the former method. However that should be a concern if only you experience lag. I don't think you'll though. Rust is really fast.

@Kofituo
Copy link
Owner

Kofituo commented Nov 1, 2022

However, depending on your program, the implementation might be slightly different. If you understand the principle I just gave, you'd be able to do it!

@Chaos4U
Copy link
Author

Chaos4U commented Nov 1, 2022

Oh so i should create a new struct that will override "EditorContents" struct?

@Kofituo
Copy link
Owner

Kofituo commented Nov 3, 2022

Not necessarily a new one. You could simply modify it to suit your needs.

@Kofituo Kofituo closed this as completed Nov 15, 2022
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