Show usage hint for scroll bar.

This commit is contained in:
shy 2021-04-18 06:21:56 +02:00
parent 35a8739f27
commit e9e1806aa2

View file

@ -41,7 +41,10 @@ impl Countdown {
} }
// Draw countdown. // Draw countdown.
pub fn draw<W: Write>(&self, stdout: &mut RawTerminal<W>) -> Result<(), std::io::Error> { pub fn draw<W: Write>(
&self,
stdout: &mut RawTerminal<W>
) -> Result<(), std::io::Error> {
if let Some(pos) = &self.position { if let Some(pos) = &self.position {
if self.value < 3600 { if self.value < 3600 {
// Show minutes and seconds. // Show minutes and seconds.
@ -72,8 +75,14 @@ impl Countdown {
Ok(()) Ok(())
} }
pub fn place(&mut self, layout: &Layout, alarm: &Alarm, offset: usize, index: usize) { // Compute position.
// Compute position. pub fn place(
&mut self,
layout: &Layout,
alarm: &Alarm,
offset: usize,
index: usize
) {
let mut col = layout.roster.col + 3 + UnicodeWidthStr::width(alarm.label.as_str()) as u16; let mut col = layout.roster.col + 3 + UnicodeWidthStr::width(alarm.label.as_str()) as u16;
let mut line = layout.roster.line + index as u16; let mut line = layout.roster.line + index as u16;
@ -114,13 +123,17 @@ impl Alarm {
pub struct AlarmRoster { pub struct AlarmRoster {
list: Vec<Alarm>, list: Vec<Alarm>,
offset: usize, offset: usize,
hints_shown: bool,
} }
impl AlarmRoster { impl AlarmRoster {
pub fn new() -> AlarmRoster { pub fn new() -> AlarmRoster {
AlarmRoster { AlarmRoster {
list: Vec::new(), list: Vec::new(),
// Scrolling offset.
offset: 0, offset: 0,
// Scrolling hint.
hints_shown: false,
} }
} }
@ -276,11 +289,10 @@ impl AlarmRoster {
layout: &mut Layout, layout: &mut Layout,
config: &Config, config: &Config,
) -> Result<(), std::io::Error> { ) -> Result<(), std::io::Error> {
// Match offset to layout. // Adjust offset in case something changed, e.g. the terminal size.
self.adjust_offset(&layout); self.adjust_offset(&layout);
for (i, alarm) in self.list.iter().skip(self.offset).enumerate() { for (i, alarm) in self.list.iter().skip(self.offset).enumerate() {
// Add 1 to compensate for the line "[...]".
let line = layout.roster.line + i as u16; let line = layout.roster.line + i as u16;
if self.offset > 0 && i == 0 { if self.offset > 0 && i == 0 {
@ -290,25 +302,23 @@ impl AlarmRoster {
"{}{}{}{}", "{}{}{}{}",
cursor::Goto(layout.roster.col, line), cursor::Goto(layout.roster.col, line),
style::Faint, style::Faint,
if config.fancy { if config.fancy { "╶╴▲╶╴" } else { "[ ^ ]" },
"╶╴▲╶╴"
} else {
"[ ^ ]"
},
style::Reset, style::Reset,
)?; )?;
continue; continue;
} else if i == layout.roster_height as usize { } else if i as u16 == layout.roster_height {
// Indicate hidden items at bottom. // Indicate hidden items at bottom.
write!( write!(
stdout, stdout,
"{}{}{}{}", "{}{}{}{}{}",
cursor::Goto(layout.roster.col, line), cursor::Goto(layout.roster.col, line),
style::Faint, style::Faint,
if config.fancy { if config.fancy { "╶╴▼╶╴" } else { "[ v ]" },
"╶╴▼╶╴" if !self.hints_shown {
self.hints_shown = true;
" [Page Up/Down]"
} else { } else {
"[ v ]" ""
}, },
style::Reset, style::Reset,
)?; )?;
@ -326,8 +336,8 @@ impl AlarmRoster {
style::Invert, style::Invert,
&alarm.label, &alarm.label,
style::NoInvert, style::NoInvert,
color::Fg(color::Reset),
style::Reset, style::Reset,
color::Fg(color::Reset),
)?; )?;
} }
false if config.fancy => { false if config.fancy => {
@ -349,8 +359,8 @@ impl AlarmRoster {
style::Bold, style::Bold,
style::Invert, style::Invert,
&alarm.label, &alarm.label,
color::Fg(color::Reset),
style::Reset, style::Reset,
color::Fg(color::Reset),
)?; )?;
} }
false => { false => {
@ -424,7 +434,11 @@ impl AlarmRoster {
} }
// Execute the command given on the command line. // Execute the command given on the command line.
pub fn exec_command(command: &Vec<String>, elapsed: u32, label: &String) -> Option<Child> { pub fn exec_command(
command: &Vec<String>,
elapsed: u32,
label: &String
) -> Option<Child> {
let time = if elapsed < 3600 { let time = if elapsed < 3600 {
format!("{:02}:{:02}", elapsed / 60, elapsed % 60) format!("{:02}:{:02}", elapsed / 60, elapsed % 60)
} else { } else {