Skip to content

Commit

Permalink
Add edit command option
Browse files Browse the repository at this point in the history
This commit adds an "edit" subcommand for Mod Manager that allows users to create or edit a configuration file for a game with their preferred text editor.
  • Loading branch information
Lucki committed Apr 5, 2024
1 parent 2c21354 commit d27307a
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 61 deletions.
16 changes: 7 additions & 9 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ Usage: mod-manager <COMMAND>
Commands:
activate Activate a mod by mounting the OverlayFS inplace
deactivate Deactivate an already activated mod by unmounting the OverlayFS
wrap Wrap an external command in between an activation and deactivation
edit Edit or create a configuration file for a game with $EDITOR
setup Setup and collect changes for a new mod by making changes to the game
wrap Wrap an external command in between an activation and deactivation
help Print this message or the help of the given subcommand(s)
Options:
Expand Down Expand Up @@ -97,21 +98,18 @@ Options:
-h, --help Print help
~~~
</details>
<details><summary>Wrap</summary>
<details><summary>Edit</summary>

~~~
Wrap an external command in between an activation and deactivation
Edit or create a configuration file for a game with $EDITOR
Usage: mod-manager wrap [OPTIONS] <GAME> -- [COMMAND]...
Usage: mod-manager edit <GAME>
Arguments:
<GAME> Identifier matching the config file
[COMMAND]... Command to wrap around to
<GAME> Identifier matching the config file. Can be a new identifier if PATH is also available
Options:
--set <SET> Override the "active_set" of the config file
--writable Mount with write access
-h, --help Print help
-h, --help Print help
~~~
</details>
<details><summary>Setup</summary>
Expand Down
60 changes: 37 additions & 23 deletions dist/_mod-manager
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ __mod-manager() {
commands=(
'activate:Activate a game or mod'
'deactivate:Deactivate a game or mod'
'wrap:Wrap a command around a game'
'edit:Edit or create a config file'
'setup:Set up a new game or mod'
'wrap:Wrap a command around a game'
'help:Print help message'
)

Expand All @@ -19,25 +20,28 @@ __mod-manager() {
'*:: :->args'

case $state in
command)
_describe 'command' commands
;;
args)
command)
_describe 'command' commands
;;
args)
case ${words[1]} in
activate)
__mod-manager-activate
activate)
__mod-manager-activate
;;
deactivate)
__mod-manager-deactivate
;;
deactivate)
__mod-manager-deactivate
edit)
__mod-manager-edit
;;
wrap)
__mod-manager-wrap
setup)
__mod-manager-setup
;;
setup)
__mod-manager-setup
wrap)
__mod-manager-wrap
;;
help)
_message "command options"
help)
_message "command options"
;;
esac
;;
Expand All @@ -56,16 +60,16 @@ __mod-manager-deactivate() {
_arguments \
{-h,--help}'[show help message and exit]' \
'1: :__mod-manager-game-identifier'

_message 'New game identifier'
}

__mod-manager-wrap() {
_arguments -S \
__mod-manager-edit() {
_arguments \
{-h,--help}'[show help message and exit]' \
'--set=::Override the "active_set" of the config file:__mod-manager-set-identifier' \
'--writable[Mount with write access]' \
'1: :__mod-manager-game-identifier' \
'2: :__mod-manager-command-separator' \
'*:: :_complete' # FIXME: Doesn't work, should complete fresh here again.
'1: :__mod-manager-game-identifier'

_message "New game identifier"
}

__mod-manager-setup() {
Expand All @@ -77,6 +81,16 @@ __mod-manager-setup() {
'2: :__mod-manager-mod-identifier'
}

__mod-manager-wrap() {
_arguments -S \
{-h,--help}'[show help message and exit]' \
'--set=::Override the "active_set" of the config file:__mod-manager-set-identifier' \
'--writable[Mount with write access]' \
'1: :__mod-manager-game-identifier' \
'2: :__mod-manager-command-separator'
# '*:: :_complete' # FIXME: Doesn't work, should complete fresh here again.
}

__mod-manager-game-identifier() {
local XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
local available=()
Expand All @@ -85,7 +99,7 @@ __mod-manager-game-identifier() {
available+=("$(basename "$config")")
done

# shellcheck disable=2068
# shellcheck disable=SC2068
_values game ${available[@]%.toml}
}

Expand Down
86 changes: 57 additions & 29 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::{path::PathBuf, vec};
use clap::Parser;
use xdg::BaseDirectories;
Expand Down Expand Up @@ -42,22 +43,11 @@ enum Action {
game: Option<String>,
},

/// Wrap an external command in between an activation and deactivation
#[clap(name = "wrap")]
Wrap {
/// Identifier matching the config file.
/// Edit or create a configuration file for a game with $EDITOR
#[clap(name = "edit")]
Edit {
/// Identifier matching the config file. Can be a new identifier.
game: String,

/// Command to wrap around to.
command: Vec<String>,

/// Override the "active_set" of the config file.
#[clap(long = "set")]
set: Option<String>,

/// Mount with write access.
#[clap(long = "writable")]
writable: bool,
},

/// Setup and collect changes for a new mod by making changes to the game
Expand All @@ -78,6 +68,24 @@ enum Action {
#[clap(long = "set")]
set: Option<String>,
},

/// Wrap an external command in between an activation and deactivation
#[clap(name = "wrap")]
Wrap {
/// Identifier matching the config file.
game: String,

/// Command to wrap around to.
command: Vec<String>,

/// Override the "active_set" of the config file.
#[clap(long = "set")]
set: Option<String>,

/// Mount with write access.
#[clap(long = "writable")]
writable: bool,
},
}

fn main() {
Expand Down Expand Up @@ -163,16 +171,36 @@ fn main() {
}
}
},
Action::Wrap { game: game_id, command, set, writable } => {
if command.is_empty() {
panic!("Missing command for wrapping game");
}
Action::Edit { game } => {
let mut arguments: Vec<String> = vec![];

let game = Game::from_config(game_id, set).unwrap();
match game.wrap(ExternalCommand::new("wrap_command".to_string(), command, Some(true), None), writable) {
let editor = match env::var("EDITOR") {
Ok(value) => value,
Err(_) => "vi".to_owned(),
};

arguments.push(editor);
arguments.push(xdg_dirs
.place_config_file(format!("{}.toml", game))
.expect("Unable to place config file.")
.to_str()
.expect("Failed converting config path to string.")
.to_owned());

ExternalCommand::new("editor".to_owned(), arguments, Some(true), None)
.run()
.unwrap();
},
Action::Setup { game: game_id, mod_id, path: game_path, set } => {
let game = match game_path {
Some(game_path) => Game::new(game_id, game_path).unwrap(),
None => Game::from_config(game_id, set).unwrap(),
};

match game.setup(mod_id) {
Ok(()) => (),
Err(error) => {
println!("Failed wrapping game overlay '{}': {}", game.id, error);
println!("Failed setup game overlay '{}': {}", game.id, error);
match game.deactivate() {
Ok(()) => (),
Err(error) => {
Expand All @@ -182,16 +210,16 @@ fn main() {
}
}
},
Action::Setup { game: game_id, mod_id, path: game_path, set } => {
let game = match game_path {
Some(game_path) => Game::new(game_id, game_path).unwrap(),
None => Game::from_config(game_id, set).unwrap(),
};
Action::Wrap { game: game_id, command, set, writable } => {
if command.is_empty() {
panic!("Missing command for wrapping game");
}

match game.setup(mod_id) {
let game = Game::from_config(game_id, set).unwrap();
match game.wrap(ExternalCommand::new("wrap_command".to_string(), command, Some(true), None), writable) {
Ok(()) => (),
Err(error) => {
println!("Failed setup game overlay '{}': {}", game.id, error);
println!("Failed wrapping game overlay '{}': {}", game.id, error);
match game.deactivate() {
Ok(()) => (),
Err(error) => {
Expand Down

0 comments on commit d27307a

Please sign in to comment.