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

feature: show hostname in title #999

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
28 changes: 27 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ tui = "0.19.0"
typed-builder = "0.10.0"
unicode-segmentation = "1.10.0"
unicode-width = "0.1.10"
gethostname = "0.4.1"

[target.'cfg(unix)'.dependencies]
libc = "0.2.124"
Expand Down
2 changes: 2 additions & 0 deletions sample_configs/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
#mem_as_value = false
# Show tree mode by default in the processes widget.
#tree = false
# Set terminal name to hostname
#title_to_hostname = false
# Shows an indicator in table widgets tracking where in the list you are.
#show_table_scroll_position = false
# Show processes as their commands by default in the process widget.
Expand Down
10 changes: 8 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use bottom::{
use crossterm::{
event::{EnableBracketedPaste, EnableMouseCapture},
execute,
terminal::{enable_raw_mode, EnterAlternateScreen},
terminal::{enable_raw_mode, EnterAlternateScreen, SetTitle},
};
use tui::{backend::CrosstermBackend, Terminal};

Expand Down Expand Up @@ -123,8 +123,14 @@ fn main() -> Result<()> {
stdout_val,
EnterAlternateScreen,
EnableMouseCapture,
EnableBracketedPaste
EnableBracketedPaste,
)?;

let use_terminal_name = get_use_terminal_name(&matches, &config);
if use_terminal_name.0 {
execute!(stdout_val, SetTitle(use_terminal_name.1),)?;
}

enable_raw_mode()?;

let mut terminal = Terminal::new(CrosstermBackend::new(stdout_val))?;
Expand Down
7 changes: 7 additions & 0 deletions src/clap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,12 @@ use CPU (3) as the default instead.
.help("The amount in ms changed upon zooming.")
.long_help("The amount of time in milliseconds changed when zooming in/out. The minimum is 1s (1000), and defaults to 15s (15000).");

let title = Arg::new("title")
.long("title")
.takes_value(true)
.value_name("Title")
.help("Sets the title of the current terminal.");

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were just doing the hostname idea and putting the custom title part aside for now. If so, change this to match the config option, and not take a value.

let tree = Arg::new("tree")
.short('T')
.long("tree")
Expand Down Expand Up @@ -410,6 +416,7 @@ use CPU (3) as the default instead.
.arg(rate)
.arg(regex)
.arg(time_delta)
.arg(title)
.arg(tree)
.arg(network_use_bytes)
.arg(network_use_log)
Expand Down
22 changes: 22 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use crate::{
pub mod layout_options;

use anyhow::{Context, Result};
use gethostname::gethostname;

#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct Config {
Expand Down Expand Up @@ -92,6 +93,7 @@ pub struct ConfigFlags {
pub network_use_log: Option<bool>,
pub network_use_binary_prefix: Option<bool>,
pub enable_gpu_memory: Option<bool>,
pub title_to_hostname: Option<bool>,
ClementTsang marked this conversation as resolved.
Show resolved Hide resolved
#[serde(with = "humantime_serde")]
#[serde(default)]
pub retention: Option<Duration>,
Expand Down Expand Up @@ -732,6 +734,26 @@ pub fn get_app_use_regex(matches: &ArgMatches, config: &Config) -> bool {
false
}

pub fn get_use_terminal_name(matches: &ArgMatches, config: &Config) -> (bool, String) {
if matches.is_present("title") {
return if let Some(custom_name) = matches.value_of("title") {
(true, String::from(custom_name))
} else {
(false, String::from(""))
};
} else if let Some(flags) = &config.flags {
if let Some(title_to_hostname) = flags.title_to_hostname {
if title_to_hostname {
return match gethostname().into_string() {
Ok(hostname) => (title_to_hostname, format!("btm ({})", hostname)),
Err(_) => (false, String::from("")),
};
}
}
}
(false, String::from(""))
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some suggestions:

  • As mentioned above, remove the custom name part, as we're not doing that for now. Just use the hostname.
  • Why not just return an Option? e.g. if there is no change, return None.

fn get_hide_time(matches: &ArgMatches, config: &Config) -> bool {
if matches.is_present("hide_time") {
return true;
Expand Down