Simplify config file

This commit is contained in:
Marcin Kulik
2025-05-07 16:42:04 +02:00
parent ebc4b3b12b
commit 65cd787072
4 changed files with 26 additions and 92 deletions

View File

@@ -10,14 +10,13 @@ use crate::util;
impl cli::Play {
pub fn run(self, config: &Config) -> Result<()> {
let cmd_config = config.cmd_play();
let speed = self.speed.or(cmd_config.speed).unwrap_or(1.0);
let idle_time_limit = self.idle_time_limit.or(cmd_config.idle_time_limit);
let speed = self.speed.or(config.playback.speed).unwrap_or(1.0);
let idle_time_limit = self.idle_time_limit.or(config.playback.idle_time_limit);
status::info!("Replaying session from {}", self.filename);
let path = util::get_local_path(&self.filename)?;
let keys = get_key_bindings(&cmd_config)?;
let keys = get_key_bindings(&config.playback)?;
let ended = loop {
let recording = asciicast::open_from_path(&*path)?;
@@ -48,7 +47,7 @@ impl cli::Play {
}
}
fn get_key_bindings(config: &config::Play) -> Result<KeyBindings> {
fn get_key_bindings(config: &config::Playback) -> Result<KeyBindings> {
let mut keys = KeyBindings::default();
if let Some(key) = config.pause_key()? {

View File

@@ -33,17 +33,17 @@ use crate::tty::{DevTty, FixedSizeTty, NullTty, Tty};
use crate::util;
impl cli::Session {
pub fn run(mut self, config: &Config, cmd_config: &config::Session) -> Result<()> {
pub fn run(mut self, config: &Config) -> Result<()> {
locale::check_utf8_locale()?;
let runtime = Runtime::new()?;
let command = self.get_command(cmd_config);
let keys = get_key_bindings(cmd_config)?;
let command = self.get_command(&config.recording);
let keys = get_key_bindings(&config.recording)?;
let notifier = notifier::threaded(get_notifier(config));
let record_input = self.rec_input || cmd_config.rec_input;
let record_input = self.rec_input || config.recording.rec_input;
let term_type = self.get_term_type();
let term_version = self.get_term_version()?;
let env = capture_env(self.rec_env.take(), cmd_config);
let env = capture_env(self.rec_env.take(), &config.recording);
let file_writer = self
.output_file
@@ -51,7 +51,7 @@ impl cli::Session {
.map(|path| {
self.get_file_writer(
path,
cmd_config,
&config.recording,
term_type.clone(),
term_version.clone(),
&env,
@@ -181,7 +181,7 @@ impl cli::Session {
fn get_file_writer<N: Notifier + 'static>(
&self,
path: &str,
config: &config::Session,
config: &config::Recording,
term_type: Option<String>,
term_version: Option<String>,
env: &HashMap<String, String>,
@@ -292,7 +292,7 @@ impl cli::Session {
self.get_tty(false).map(|tty| tty.get_version())
}
fn get_command(&self, config: &config::Session) -> Option<String> {
fn get_command(&self, config: &config::Recording) -> Option<String> {
self.command.as_ref().cloned().or(config.command.clone())
}
@@ -301,7 +301,7 @@ impl cli::Session {
term_type: Option<String>,
term_version: Option<String>,
env: &HashMap<String, String>,
config: &config::Session,
config: &config::Recording,
) -> Metadata {
let idle_time_limit = self.idle_time_limit.or(config.idle_time_limit);
let command = self.get_command(config);
@@ -431,7 +431,7 @@ fn build_producer_url(
Ok(url)
}
fn get_key_bindings(config: &config::Session) -> Result<KeyBindings> {
fn get_key_bindings(config: &config::Recording) -> Result<KeyBindings> {
let mut keys = KeyBindings::default();
if let Some(key) = config.prefix_key()? {
@@ -449,7 +449,7 @@ fn get_key_bindings(config: &config::Session) -> Result<KeyBindings> {
Ok(keys)
}
fn capture_env(var_names: Option<String>, config: &config::Session) -> HashMap<String, String> {
fn capture_env(var_names: Option<String>, config: &config::Recording) -> HashMap<String, String> {
let var_names = var_names
.or(config.rec_env.clone())
.unwrap_or(String::from("SHELL"));

View File

@@ -18,7 +18,8 @@ pub type Key = Option<Vec<u8>>;
#[allow(unused)]
pub struct Config {
server: Server,
cmd: Cmd,
pub recording: Recording,
pub playback: Playback,
pub notifications: Notifications,
}
@@ -28,30 +29,9 @@ pub struct Server {
url: Option<String>,
}
#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Cmd {
rec: Rec,
play: Play,
stream: Stream,
session: Session,
}
#[derive(Debug, Clone, Deserialize, Default)]
#[allow(unused)]
pub struct Session {
pub command: Option<String>,
pub rec_input: bool,
pub rec_env: Option<String>,
pub idle_time_limit: Option<f64>,
pub prefix_key: Option<String>,
pub pause_key: Option<String>,
pub add_marker_key: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Default)]
#[allow(unused)]
pub struct Rec {
pub struct Recording {
pub command: Option<String>,
pub rec_input: bool,
pub rec_env: Option<String>,
@@ -63,7 +43,7 @@ pub struct Rec {
#[derive(Debug, Clone, Deserialize)]
#[allow(unused)]
pub struct Play {
pub struct Playback {
pub speed: Option<f64>,
pub idle_time_limit: Option<f64>,
pub pause_key: Option<String>,
@@ -71,17 +51,6 @@ pub struct Play {
pub next_marker_key: Option<String>,
}
#[derive(Debug, Clone, Deserialize, Default)]
#[allow(unused)]
pub struct Stream {
pub command: Option<String>,
pub rec_input: bool,
pub rec_env: Option<String>,
pub prefix_key: Option<String>,
pub pause_key: Option<String>,
pub add_marker_key: Option<String>,
}
#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct Notifications {
@@ -93,10 +62,8 @@ impl Config {
pub fn new(server_url: Option<String>) -> Result<Self> {
let mut config = config::Config::builder()
.set_default("server.url", None::<Option<String>>)?
.set_default("cmd.rec.rec_input", false)?
.set_default("cmd.play.speed", None::<Option<f64>>)?
.set_default("cmd.stream.rec_input", false)?
.set_default("cmd.session.rec_input", false)?
.set_default("playback.speed", None::<Option<f64>>)?
.set_default("recording.rec_input", false)?
.set_default("notifications.enabled", true)?
.add_source(File::with_name("/etc/asciinema/config.toml").required(false))
.add_source(File::with_name(&user_defaults_path()?.to_string_lossy()).required(false))
@@ -142,41 +109,9 @@ impl Config {
Ok(id)
}
}
pub fn cmd_rec(&self) -> Session {
Session {
command: self.cmd.rec.command.clone(),
rec_input: self.cmd.rec.rec_input,
rec_env: self.cmd.rec.rec_env.clone(),
idle_time_limit: self.cmd.rec.idle_time_limit,
prefix_key: self.cmd.rec.prefix_key.clone(),
pause_key: self.cmd.rec.pause_key.clone(),
add_marker_key: self.cmd.rec.add_marker_key.clone(),
}
}
pub fn cmd_stream(&self) -> Session {
Session {
command: self.cmd.stream.command.clone(),
rec_input: self.cmd.stream.rec_input,
rec_env: self.cmd.stream.rec_env.clone(),
idle_time_limit: None,
prefix_key: self.cmd.stream.prefix_key.clone(),
pause_key: self.cmd.stream.pause_key.clone(),
add_marker_key: self.cmd.stream.add_marker_key.clone(),
}
}
pub fn cmd_session(&self) -> Session {
self.cmd.session.clone()
}
pub fn cmd_play(&self) -> Play {
self.cmd.play.clone()
}
}
impl Session {
impl Recording {
pub fn prefix_key(&self) -> Result<Option<Key>> {
self.prefix_key.as_ref().map(parse_key).transpose()
}
@@ -190,7 +125,7 @@ impl Session {
}
}
impl Play {
impl Playback {
pub fn pause_key(&self) -> Result<Option<Key>> {
self.pause_key.as_ref().map(parse_key).transpose()
}

View File

@@ -54,7 +54,7 @@ fn main() -> anyhow::Result<()> {
log_file: None,
};
cmd.run(&config, &config.cmd_rec())
cmd.run(&config)
}
Commands::Stream(stream) => {
@@ -75,10 +75,10 @@ fn main() -> anyhow::Result<()> {
log_file: stream.log_file,
};
cmd.run(&config, &config.cmd_stream())
cmd.run(&config)
}
Commands::Session(cmd) => cmd.run(&config, &config.cmd_session()),
Commands::Session(cmd) => cmd.run(&config),
Commands::Play(cmd) => cmd.run(&config),
Commands::Cat(cmd) => cmd.run(&config),
Commands::Convert(cmd) => cmd.run(&config),