wbkgd does not set background color - rust

I'm using Rust to write an ncurses app.
I'm trying to set the color of a subwin, however having no success. I'm not even sure the window is created in the first place, or it just doesn't want to set the color.
Here's a minimal example:
use ncurses::*;
fn main() {
setlocale(LcCategory::all, "");
initscr();
keypad(stdscr(), true);
start_color();
init_pair(1, COLOR_RED, COLOR_RED);
loop {
let user_input = get_wch();
match user_input.unwrap() {
WchResult::Char(ch) => {
match ch {
27 => break,
_ => {}
}
},
WchResult::KeyCode(code) => {
match code {
KEY_F5 => {
let ln = subwin(stdscr(), LINES(), 5, 0, 0);
wbkgd(ln, COLOR_PAIR(1));
refresh();
},
_ => {}
}
}
}
}
endwin();
}
As you can see, I initialized a color pair and invoked start_colors().
What could be the issue?

I think the problem might be that your not refreshing the sub-window. Try using wrefresh(ln) instead. Actually use both refresh and refresh(ln).

In this chunk
let ln = subwin(stdscr(), LINES(), 5, 0, 0);
wbkgd(ln, COLOR_PAIR(1));
refresh();
the refresh overwrites the result from the subwin. Also, you would get better results by ORing the COLOR_PAIR with a space (see this).
Addressing the comments:
let user_input = get_wch();
also does a refresh (overwriting the result from the subwin).

Related

Print updated value before it should update

Here's a short code
use std::{thread, time::{Duration}, sync::{Arc, Mutex}};
fn main() {
let num = Arc::new(Mutex::new(0u8));
let clone = num.clone();
thread::spawn(move || {
loop {
println!("{:?};", *num.lock().unwrap()); // always prints 0
thread::sleep(Duration::from_secs(1));
*num.lock().unwrap() = 0;
println!("{:?};", *num.lock().unwrap()); // always prints 0
}
});
listen(clone);
}
fn listen(num: Arc<Mutex<u8>>) {
rdev::listen(move |event| {
match event.event_type {
rdev::EventType::KeyPress(_) => {
*num.lock().unwrap() += 1;
},
_ => {},
}
}).unwrap();
}
All it should do is just counting how many times the users pressed any key on a keyboard. But this code is doesn't work.
I added 2 println! statements - before the value is updated and after that. And I assume to get a real value in the first statement and 0 in the second one. But for some reason both println! print a zero.
Why so and how can I avoid it?
The code does work if I don't reset value to a zero. But I have to do it.
It seems that you leave no time between the num read and write. So it writes the num value and immediatly read from it.
You probably want to add an extra delay statement:
loop {
println!("{:?};", *num.lock().unwrap());
thread::sleep(Duration::from_secs(1));
*num.lock().unwrap() = 0;
println!("{:?};", *num.lock().unwrap());
//this delay will allow the other thread to modify the num before the read happens.
thread::sleep(Duration::from_secs(1));
}

How to let Gtk CellRendererText display newlines during edit?

My Gtk app currently reads data from an external database/file, then add it to a Gtk Treeview. One of the columns allows newlines, which is correctly displayed in the Treeview when single-paragraph-mode is false.
However, when I double click to edit, the cell turns into a single line entry box where a carriage return symbol (like ⤶) replaces the newlines.
This makes it impossible to edit a cell with lots of newlines because they appear as one single line. Is it possible to render the newlines as actual newlines during editing? In other words, I want single-paragraph-mode=false to apply to the cell when it is being edited
I've read the documentation for CellRendererText and CellRenderer, but it is rather lacking. Things like max-width-chars apply to the cell whether editing or not.
This code demonstrates what I mean. (It uses gtk-rs but an answer using C is fine too):
use gtk::glib;
use gtk::prelude::*;
fn main() {
let application = gtk::Application::new(
Some("com.example.xyz"),
Default::default(),
);
application.connect_activate(build_ui);
application.run();
}
fn build_ui(application: &gtk::Application) {
let window = gtk::ApplicationWindow::new(application);
window.set_title(Some("App"));
window.set_default_size(800, 800);
let grid = gtk::Grid::builder()
.column_spacing(5)
.row_spacing(10)
.build();
window.set_child(Some(&grid));
let (treeview, _treestore) = build_treeview();
let sw = gtk::ScrolledWindow::builder()
.child(&treeview)
.vexpand(false)
.vexpand_set(false)
.max_content_height(10)
.build();
// column, row, width, height
grid.attach(&sw, 0, 0, 1, 1);
window.show();
}
fn build_treeview() -> (gtk::TreeView, gtk::TreeStore) {
let treestore = gtk::TreeStore::new(&[glib::Type::STRING].repeat(4));
let treeview = gtk::TreeView::builder()
.hexpand(true)
.vexpand(true)
.enable_grid_lines(gtk::TreeViewGridLines::Both)
.model(&treestore)
.build();
for (i, title) in ["Title", "Description", "Path1", "Path2"]
.iter()
.enumerate()
{
let cell_renderer = gtk::CellRendererText::builder()
.editable(true)
.xalign(0.0)
.single_paragraph_mode(false)
.build();
treeview.insert_column_with_attributes(
i.try_into().unwrap(),
title,
&cell_renderer,
&[(&"text", i.try_into().unwrap())],
);
let ts = treestore.clone();
cell_renderer.connect_edited(move |_renderer, row, text| {
ts.set_value(
&ts.iter(&row).unwrap(),
i.try_into().unwrap(),
&text.to_value()
);
});
}
treestore.insert_with_values(
None,
None,
&[
(0, &"one"),
(1, &"two\ntwo\ntwo\ntwo"),
(2, &"three"),
(3, &"four"),
],
);
(treeview, treestore)
}

Yew: Difficulty with nested callbacks

I'm attempting to do something that I feel is pretty basic: I have a pulldown, and I'd like the onchange event for that pulldown to cause the program to fetch some data from the backend based on the user's input. (And then, you know, give the user more options based on the first thing they picked. Really simple, and seems like I ought to have been able to find an easy way to do this.)
Full code for this minimal (failing) example is at: https://github.com/djmcmath/broken-yew
But the relevant bit, which doesn't behave correctly, is below:
The view function renders, delightfully, an iterated list. I pass in a callback, so it knows what to do on the "onchange" event.
The callback gets executed, which makes me very happy. But it isn't calling the Msg::GetData. This compiles, which is nice, but it doesn't work, which is less nice.
I've spent, I'm ashamed to admit, several weeks of my spare time fighting with this. I think it has something to do with scopes and lifetimes. I think that the way I'm making this compile -- by cloning the context and using "move" disconnects it from the actual context that I need to make this work. But every variation on the theme that I've been able to find in examples and references complains about scope or lifetimes.
Thanks in advance for the help.
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::GetData(value) => {
log::info!("Start 'fetch' with user-selected value: {}", value);
ctx.link().send_future(async {
match fetch_markdown("url_shortened_for_clarity").await {
Ok(md) => Msg::SetMarkdownFetchState(FetchState::Success(md)),
Err(err) => Msg::SetMarkdownFetchState(FetchState::Failed(err)),
}
});
false
},
Msg::SetMarkdownFetchState(fetch_state) => {
let mut wr = WebReturn { term_id: 0, dow: 0, dep_time_num: 0 };
match fetch_state {
FetchState::Success(s) => { wr = serde_json::from_str(&s).expect(&format!("Poorly formatted JSON! {}", s).to_string()); },
FetchState::Failed(f) => { log::info!("Fetch failed: {}", f); },
FetchState::NotFetching => {},
FetchState::Fetching => {}
};
log::info!("term_id (3) : {}, dep_time_num (12000) : {}, and dow (3) : {}", wr.term_id, wr.dep_time_num, wr.dow);
true
}
}
}
fn view(&self, ctx:&Context<Self>) -> Html {
let ctx_link = ctx.link().clone();
let my_callback: Callback<String> = Callback::from(move |value: String| {
let val_as_num = value.parse::<i32>().unwrap_or(0);
log::info!("Returned value: {}", val_as_num);
ctx_link.callback(|val_as_num: i32| Msg::GetData(val_as_num));
});
html! {
<div>
{ self.render_list(&self.props.term_list, my_callback) }
</div>
}
}
This line does not "call back" to your component, it creates a callback and then doesn't call it:
ctx_link.callback(|val_as_num: i32| Msg::GetData(val_as_num));
You need to instead call .send_message() in your callback or, better yet, create your original callback with .callback():
let my_callback = ctx_link.callback(|value: String| {
let val_as_num = value.parse::<i32>().unwrap_or(0);
log::info!("Returned value: {}", val_as_num);
Msg::GetData(val_as_num)
});

How to switch screens in a rust tui app (termion + tui-rs)

I have a tui app where a user is presented with some choices through a list. Once they navigate to the choice they want and hit enter I'd like to take them to the "next" screen.
It's more complicated than just clearning existing text and printing new one because I also need to replace keybindings and basically start a new tui-rs loop. More below.
Code for Screen 1:
pub fn draw_screen() -> Result<(), Box<dyn Error>> {
// Terminal initialization
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
let events = Events::new();
loop {
terminal.draw(|f| {
// user shown a list they can navigate through using arrow keys
});
match events.next()? {
Event::Input(input) => match input {
Key::Char('q') => {
break;
}
Key::Char('\n') => {
// this is where I need to "send" them to a new screen
}
Key::Down => {
// my_list won't exist on next screen
my_list.items.next();
}
Key::Up => {
my_list.items.previous();
}
_ => {}
},
_ => {}
}
}
Ok(())
}
As can be seen the keybindings at the bottom are specific to this screen. Eg on the next screen there's not going to be a my_list and instead there might be a my_another_list or my_box or nothing at all.
So if all I did was clear the text, I'd still be left inside the same loop with the same keybindings - doesn't work.
What's the right way to initiate a new loop with fresh keybindings?

Uncaught Error with Webassembly using Yew Framwork

I'm using Yew to program a theme switcher that by clicking cycles through different themes.
This is my update function. It gets the current theme which is stored in shared state, depending on what would come next inside theme_cycle the theme value inside the shared state gets set to it.
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::ChangeTheme => {
let theme_cycle: [&str; 3] = ["light", "dark", "rust"];
let current_theme = self.props.handle.state().theme.clone();
// eval next theme
let next_theme = match theme_cycle.iter().position(|x| x == &current_theme) {
None => theme_cycle[0].to_string(),
Some(i) => {
if i >= (current_theme.len() - 1) {
theme_cycle[0].to_string()
} else {
theme_cycle[i + 1].to_string()
}
},
};
// set next theme
self.props.handle.reduce(move |state| state.theme = next_theme.clone());
// store it inside localstorage
},
Msg::ToggleLangDropdown => self.show_dropdown = !self.show_dropdown,
};
true
}
But if the theme val inside shared state is "rust" and I click the button again that calls Msg::ChangeTheme, the theme should be set to "light" but instead my code panics and I get an "Uncaught Error: undefined" inside the Browser Console.
I've found a workaround; instead of using an array and accessing the values, I've tried to do the same task but just with iterators and made sure that the update function takes no ownership of any variable outside the function itself (I don't really know if that is really necessary though...)
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Msg::ChangeTheme => {
let theme_cycle = ["light".to_string(), "dark".to_string(), "rust".to_string()];
let current_theme = self.props.handle.state().theme.clone();
let indexof_current_theme = match theme_cycle.iter().position(|x| x.to_string() == current_theme) {
None => 0,
Some(x) => x.clone(),
};
let next_theme = match theme_cycle.iter().nth(indexof_current_theme + 1) {
None => theme_cycle.iter().nth(0).unwrap().clone(),
Some(x) => x.clone(),
};
self.props.handle.reduce(move |state| state.theme = next_theme.to_string());
},
Msg::ToggleLangDropdown => self.show_lang_dropdown = !self.show_lang_dropdown,
Msg::ToggleThemeDropdown => self.show_theme_dropdown = !self.show_theme_dropdown,
};
true
}
Still would be cool if anyone knows what I did wrong in my first attempt.

Resources