Skip to content

Commit

Permalink
Merge branch 'master' of github.com:apognu/tuigreet
Browse files Browse the repository at this point in the history
  • Loading branch information
dthelegend committed May 13, 2024
2 parents 9f3fb64 + f67e43f commit 8fbef86
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "tuigreet"
version = "0.9.0"
authors = ["Antoine POPINEAU <antoine[email protected]>"]
authors = ["Antoine POPINEAU <antoine@popineau.eu>"]
edition = "2018"
build = "build.rs"

Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ Please refer to the snippet below for the minimal `tuigreet` configuration:

Pre-built binaries of `tuigreet` for several architectures can be found in the [releases](https://github.com/apognu/tuigreet/releases) section of this repository. The [tip prerelease](https://github.com/apognu/tuigreet/releases/tag/tip) is continuously built and kept in sync with the `master` branch.

## Running the tests

Tests from the default features should run without any special consideration by running `cargo test`.

If you intend to run the whole test suite, you will need to perform some setup. One of our features uses NSS to list and filter existing users on the system, and in order not to rely on actual users being created on the host, we use [libnss_wrapper](https://cwrap.org/nss_wrapper.html) to mock responses from NSS. Without this, the tests would use the real user list from your system and probably fail because it cannot find the one it looks for.

After installing `libnss_wrapper` on your system (or compiling it to get the `.so`), you can run those specific tests as such:

```
$ export NSS_WRAPPER_PASSWD=contrib/fixtures/passwd
$ export NSS_WRAPPER_GROUP=contrib/fixtures/group
$ LD_PRELOAD=/path/to/libnss_wrapper.so cargo test --features nsswrapper nsswrapper_ # To run those tests specifically
$ LD_PRELOAD=/path/to/libnss_wrapper.so cargo test --all-features # To run the whole test suite
```

## Configuration

Edit `/etc/greetd/config.toml` and set the `command` setting to use `tuigreet`:
Expand Down
6 changes: 3 additions & 3 deletions src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,18 +213,18 @@ impl Greeter {
// If we should remember the last logged-in user.
if greeter.remember {
if let Some(username) = get_last_user_username() {
greeter.username = MaskedString::from(username.clone(), get_last_user_name());
greeter.username = MaskedString::from(username, get_last_user_name());

// If, on top of that, we should remember their last session.
if greeter.remember_user_session {
if let Ok(ref session_path) = get_last_user_session_path(&username) {
if let Ok(ref session_path) = get_last_user_session_path(greeter.username.get()) {
// Set the selected menu option and the session source.
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
}

// See if we have the last free-form command from the user.
if let Ok(command) = get_last_user_session(&username) {
if let Ok(command) = get_last_user_session(greeter.username.get()) {
greeter.session_source = SessionSource::Command(command);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub fn get_last_user_name() -> Option<String> {
}

pub fn write_last_username(username: &MaskedString) {
let _ = fs::write(LAST_USER_USERNAME, username.value.clone());
let _ = fs::write(LAST_USER_USERNAME, &username.value);

if let Some(ref name) = username.mask {
let _ = fs::write(LAST_USER_NAME, name);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn draw(greeter: &mut Greeter, f: &mut Frame) -> Result<(u16, u16), Box<dyn

let command_label_text = prompt_value(theme, Some(fl!("new_command")));
let command_label = Paragraph::new(command_label_text).style(theme.of(&[Themed::Prompt]));
let command_value_text = Span::from(greeter.buffer.clone());
let command_value_text = Span::from(&greeter.buffer);
let command_value = Paragraph::new(command_value_text).style(theme.of(&[Themed::Input]));

f.render_widget(command_label, chunks[0]);
Expand Down
4 changes: 2 additions & 2 deletions src/ui/common/menu.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::error::Error;
use std::{borrow::Cow, error::Error};

use tui::{
prelude::Rect,
Expand All @@ -18,7 +18,7 @@ use crate::{
use super::style::Themed;

pub trait MenuItem {
fn format(&self) -> String;
fn format(&self) -> Cow<'_, str>;
}

#[derive(Default)]
Expand Down
6 changes: 4 additions & 2 deletions src/ui/power.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use crate::{power::PowerOption, ui::common::menu::MenuItem};

#[derive(SmartDefault, Clone)]
Expand All @@ -8,7 +10,7 @@ pub struct Power {
}

impl MenuItem for Power {
fn format(&self) -> String {
self.label.clone()
fn format(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.label)
}
}
9 changes: 6 additions & 3 deletions src/ui/sessions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::path::{Path, PathBuf};
use std::{
borrow::Cow,
path::{Path, PathBuf},
};

use crate::Greeter;

Expand Down Expand Up @@ -86,8 +89,8 @@ pub struct Session {
}

impl MenuItem for Session {
fn format(&self) -> String {
self.name.clone()
fn format(&self) -> Cow<'_, str> {
Cow::Borrowed(&self.name)
}
}

Expand Down
8 changes: 5 additions & 3 deletions src/ui/users.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Cow;

use super::common::menu::MenuItem;

#[derive(Default, Clone)]
Expand All @@ -7,10 +9,10 @@ pub struct User {
}

impl MenuItem for User {
fn format(&self) -> String {
fn format(&self) -> Cow<'_, str> {
match &self.name {
Some(name) => format!("{name} ({})", self.username),
None => self.username.clone(),
Some(name) => Cow::Owned(format!("{name} ({})", self.username)),
None => Cow::Borrowed(&self.username),
}
}
}

0 comments on commit 8fbef86

Please sign in to comment.