Fix issue printing where some rows could be offset. Added an option to display the current date and time.

This commit is contained in:
Antoine POPINEAU 2020-06-27 20:51:21 +02:00
parent ea9d911ce0
commit 0c4c127bae
No known key found for this signature in database
GPG Key ID: A78AC64694F84063
6 changed files with 110 additions and 23 deletions

69
Cargo.lock generated
View File

@ -1,5 +1,11 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
[[package]]
name = "bitflags"
version = "1.2.1"
@ -24,6 +30,17 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
[[package]]
name = "chrono"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2"
dependencies = [
"num-integer",
"num-traits",
"time",
]
[[package]]
name = "either"
version = "1.5.3"
@ -43,6 +60,7 @@ dependencies = [
name = "greetd-tui"
version = "0.1.0"
dependencies = [
"chrono",
"getopts",
"greetd_ipc",
"nix",
@ -96,6 +114,25 @@ dependencies = [
"void",
]
[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
dependencies = [
"autocfg",
"num-traits",
]
[[package]]
name = "num-traits"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
dependencies = [
"autocfg",
]
[[package]]
name = "numtoa"
version = "0.1.0"
@ -224,6 +261,16 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
dependencies = [
"libc",
"winapi",
]
[[package]]
name = "tui"
version = "0.9.5"
@ -262,3 +309,25 @@ name = "void"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

View File

@ -11,3 +11,4 @@ termion = "1.5.5"
tui = "0.9.5"
nix = "0.17.0"
textwrap = "0.12.0"
chrono = "0.4.11"

View File

@ -7,14 +7,15 @@ Graphical console greter for [greetd](https://git.sr.ht/~kennylevinsen/greetd).
## Usage
```
Usage: greetd-tui [options]
Usage: greetd-tui [OPTIONS]
Options:
-h, --help show this usage information
-c, --cmd COMMAND command to run
-i, --issue show the host's issue file
-g, --greeting GREETING
show custom text above login prompt
-h, --help show this usage information
-t, --time display the current date and time
```
## Configuration

View File

@ -56,6 +56,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut greeter = Greeter::default();
let mut opts = Options::new();
opts.optflag("h", "help", "show this usage information");
opts.optopt("c", "cmd", "command to run", "COMMAND");
opts.optflag("i", "issue", "show the host's issue file");
opts.optopt(
@ -64,7 +65,7 @@ fn main() -> Result<(), Box<dyn Error>> {
"show custom text above login prompt",
"GREETING",
);
opts.optflag("h", "help", "show this usage information");
opts.optflag("t", "time", "display the current date and time");
greeter.config = match opts.parse(&env::args().collect::<Vec<String>>()) {
Ok(matches) => Some(matches),
@ -116,8 +117,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}
fn print_usage(opts: Options) {
let brief = format!("Usage: {} [options]", "greetd-tui");
eprint!("{}", opts.usage(&brief));
eprint!("{}", opts.usage("Usage: greetd-tui [OPTIONS]"));
}
pub fn exit(status: AuthStatus, stream: &mut UnixStream) {

View File

@ -5,10 +5,11 @@ use std::{
io::{self, Write},
};
use chrono::prelude::*;
use termion::{cursor::Goto, raw::RawTerminal};
use tui::{
backend::TermionBackend,
layout::{Constraint, Direction, Layout},
layout::{Alignment, Constraint, Layout},
style::{Modifier, Style},
widgets::{Paragraph, Text},
Terminal,
@ -34,15 +35,24 @@ pub fn draw(
terminal.draw(|mut f| {
let size = f.size();
let chunks = Layout::default()
.constraints([Constraint::Min(1), Constraint::Length(1)].as_ref())
.constraints(
[
Constraint::Length(1),
Constraint::Min(1),
Constraint::Length(1),
]
.as_ref(),
)
.split(size);
let status = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(100)].as_ref())
.split(chunks[1]);
if greeter.config().opt_present("time") {
let time_text = [Text::raw(get_time())];
let time = Paragraph::new(time_text.iter()).alignment(Alignment::Center);
let t = [
f.render_widget(time, chunks[0]);
}
let status_text = [
Text::styled(
format!("ESC"),
Style::default().modifier(Modifier::REVERSED),
@ -54,9 +64,9 @@ pub fn draw(
greeter.command.clone().unwrap_or("-".to_string())
)),
];
let p = Paragraph::new(t.iter());
let status = Paragraph::new(status_text.iter());
f.render_widget(p, status[0]);
f.render_widget(status, chunks[2]);
cursor = self::prompt::draw(greeter, &mut f).ok();
})?;
@ -69,3 +79,7 @@ pub fn draw(
Ok(())
}
fn get_time() -> String {
Local::now().format("%b, %d %h %Y - %H:%M").to_string()
}

View File

@ -46,7 +46,7 @@ pub fn draw(
f.render_widget(block, container);
let (message, message_height) = get_message_height(&greeter.message, 1, 1);
let greeting_height = get_greeting_height(&greeter.greeting, 1, 0);
let (greeting, greeting_height) = get_greeting_height(&greeter.greeting, 1, 0);
let constraints = [
Constraint::Length(greeting_height), // Greeting
@ -70,11 +70,9 @@ pub fn draw(
let pos = chunks[USERNAME_INDEX];
if let Some(greeting) = &greeter.greeting {
if let Some(greeting) = &greeting {
let greeting_text = [Text::raw(greeting.trim_end())];
let greeting_label = Paragraph::new(greeting_text.iter())
.alignment(Alignment::Center)
.wrap(true);
let greeting_label = Paragraph::new(greeting_text.iter()).alignment(Alignment::Center);
f.render_widget(greeting_label, chunks[GREETING_INDEX]);
}
@ -164,7 +162,7 @@ pub fn draw(
fn get_height(greeter: &Greeter, message: &Option<String>) -> u16 {
let (_, message_height) = get_message_height(message, 2, 0);
let greeting_height = get_greeting_height(&greeter.greeting, 1, 0);
let (_, greeting_height) = get_greeting_height(&greeter.greeting, 1, 0);
let initial = match greeter.mode {
Mode::Username => 5,
Mode::Password => 7,
@ -173,14 +171,18 @@ fn get_height(greeter: &Greeter, message: &Option<String>) -> u16 {
initial + greeting_height + message_height
}
fn get_greeting_height(greeting: &Option<String>, padding: u16, fallback: u16) -> u16 {
fn get_greeting_height(
greeting: &Option<String>,
padding: u16,
fallback: u16,
) -> (Option<String>, u16) {
if let Some(greeting) = greeting {
let wrapped = textwrap::fill(greeting, WIDTH as usize - 4);
let height = wrapped.trim_end().matches('\n').count();
height as u16 + 1 + padding
(Some(wrapped), height as u16 + 1 + padding)
} else {
fallback
(None, fallback)
}
}