Skip to content

Commit a560b3c

Browse files
author
HoolaBoola
committed
Install script now tries to create autocomplete scripts in appropriate
places and add aliases etc. depending on the shell (Zsh, or Bash) For the script to work, for now, user needs to edit the CMD variable in the script to point to /target/debug/tmc after compiling the current version
1 parent fd3d86b commit a560b3c

4 files changed

Lines changed: 163 additions & 70 deletions

File tree

build.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ fn main() {
1515

1616
let mut app = build_cli();
1717
app.gen_completions("tmc", Shell::Bash, &outdir);
18-
app.gen_completions("tmc", Shell::Fish, &outdir);
1918
app.gen_completions("tmc", Shell::PowerShell, &outdir);
2019
app.gen_completions("tmc", Shell::Zsh, &outdir);
2120
}

scripts/install.sh

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
23
set -euo pipefail
34

45
echo "~ Installing TMC-CLI ~"
@@ -96,17 +97,67 @@ else
9697
PROFILEFILE=$HOME/.shrc
9798
echo "Defaulting to .shrc for environment variables, if this is incorrect, please copy these manually to correct file."
9899
fi
99-
# Removes old aliases
100+
# Removes old aliases and such
100101
sed -i '/alias tmc=/d' "$PROFILEFILE"
102+
sed -i "/export TMC_LANGS_CONFIG_DIR=/d" "$PROFILEFILE"
101103

102104
echo $PROFILEFILE
103105

106+
COMPLETIONS_PATH=$HOME/.local/share/tmc-autocomplete
107+
108+
109+
CMD=$PWD/$filename
110+
104111
# Saves new alias to .bashrc
105-
echo "alias tmc='$PWD/$filename'" >> "$PROFILEFILE"
112+
echo "alias tmc='$CMD'" >> "$PROFILEFILE"
106113
echo "export TMC_LANGS_CONFIG_DIR='$HOME/tmc-config'" >> "$PROFILEFILE"
107114

108115
echo ""
109116

117+
118+
119+
#
120+
#
121+
# Auto-complete scripts
122+
#
123+
#
124+
if [ "$SHELLNAME" = "bash" ]; then
125+
126+
127+
echo "Generating auto-complete scripts to $COMPLETIONS_PATH"
128+
echo ""
129+
echo ""
130+
131+
# removing possibly existing sourcing
132+
sed -i '/source/!b;/tmc-autocomplete/d' "$PROFILEFILE"
133+
134+
135+
# creating the completions directory, if it doesn't exist
136+
eval "mkdir -p $COMPLETIONS_PATH"
137+
138+
# calling the generate-completions subcommand to generate the completion script
139+
eval "$CMD generate-completions --bash > $COMPLETIONS_PATH/tmc.bash"
140+
141+
# adding the line to .bashrc so that bash knows where to look for
142+
echo "source $COMPLETIONS_PATH/tmc.bash" >> "$PROFILEFILE"
143+
144+
elif [ "$SHELLNAME" = "zsh" ]; then
145+
echo "Generating auto-complete scripts to $COMPLETIONS_PATH"
146+
echo ""
147+
echo ""
148+
149+
# removing possibly existing definitions
150+
sed -i "/compdef _tmc/d" "$PROFILEFILE"
151+
sed -i '/fpath/!b;/tmc-autocomplete/d' "$PROFILEFILE"
152+
153+
eval "mkdir -p $COMPLETIONS_PATH"
154+
eval "$CMD generate-completions --bash > $COMPLETIONS_PATH/_tmc"
155+
156+
echo "fpath=($COMPLETIONS_PATH/_tmc " '$fpath)' >> "$PROFILEFILE"
157+
158+
echo "compdef _tmc tmc" >> "$PROFILEFILE"
159+
fi
160+
110161
echo "Installation complete. Please restart the terminal."
111162
echo "After opening a new terminal, you can try using TMC-CLI from the command line with:"
112163
echo " 'tmc login'"

src/cli.rs

Lines changed: 84 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -10,99 +10,119 @@ pub fn build_cli() -> App<'static, 'static> {
1010
.subcommand(SubCommand::with_name("courses").about("List the available courses"))
1111
.subcommand(
1212
SubCommand::with_name("download")
13-
.about("Downloads course exercises")
14-
.arg(
15-
Arg::with_name("course")
16-
.short("c")
17-
.long("course")
18-
.value_name("course name")
19-
.required(false),
13+
.about("Downloads course exercises")
14+
.arg(
15+
Arg::with_name("course")
16+
.short("c")
17+
.long("course")
18+
.value_name("course name")
19+
.required(false),
2020
)
21-
.arg(
22-
Arg::with_name("currentdir")
23-
.short("d")
24-
.long("currentdir")
25-
.required(false),
21+
.arg(
22+
Arg::with_name("currentdir")
23+
.short("d")
24+
.long("currentdir")
25+
.required(false),
2626
),
27-
)
27+
)
2828
.subcommand(
2929
SubCommand::with_name("exercises")
30-
.about("List the exercises for a specific course")
31-
.arg(Arg::with_name("course").value_name("course").required(true)),
32-
)
30+
.about("List the exercises for a specific course")
31+
.arg(Arg::with_name("course").value_name("course").required(true)),
32+
)
3333
.subcommand(
3434
SubCommand::with_name("login")
35-
.about("Login to TMC server")
36-
.arg(
37-
Arg::with_name("non-interactive")
38-
.short("n")
39-
.help("Initiates the non-interactive mode.")
40-
.long("non-interactive"),
35+
.about("Login to TMC server")
36+
.arg(
37+
Arg::with_name("non-interactive")
38+
.short("n")
39+
.help("Initiates the non-interactive mode.")
40+
.long("non-interactive"),
4141
),
42-
)
42+
)
4343
.subcommand(SubCommand::with_name("logout").about("Logout from TMC server"))
4444
.subcommand(
4545
SubCommand::with_name("organization")
46-
.about("Change organization")
47-
.arg(
48-
Arg::with_name("non-interactive")
49-
.short("n")
50-
.help("Initiates the non-interactive mode.")
51-
.long("non-interactive"),
46+
.about("Change organization")
47+
.arg(
48+
Arg::with_name("non-interactive")
49+
.short("n")
50+
.help("Initiates the non-interactive mode.")
51+
.long("non-interactive"),
5252
),
53-
)
53+
)
5454
.subcommand(
5555
SubCommand::with_name("paste")
56-
.about("Submit exercise to TMC pastebin")
57-
.arg(
58-
Arg::with_name("exercise")
59-
.value_name("exercise")
60-
.required(false),
56+
.about("Submit exercise to TMC pastebin")
57+
.arg(
58+
Arg::with_name("exercise")
59+
.value_name("exercise")
60+
.required(false),
6161
),
62-
)
62+
)
6363
.subcommand(
6464
SubCommand::with_name("submit")
65-
.about("Submit exercises to TMC server")
66-
.arg(
67-
Arg::with_name("exercise")
68-
.value_name("exercise")
69-
.required(false),
65+
.about("Submit exercises to TMC server")
66+
.arg(
67+
Arg::with_name("exercise")
68+
.value_name("exercise")
69+
.required(false),
7070
),
71-
)
71+
)
7272
.subcommand(
7373
SubCommand::with_name("test")
74-
.about("Run local exercise tests")
75-
.arg(
76-
Arg::with_name("exercise")
77-
.value_name("exercise")
78-
.required(false),
74+
.about("Run local exercise tests")
75+
.arg(
76+
Arg::with_name("exercise")
77+
.value_name("exercise")
78+
.required(false),
7979
),
80-
)
80+
)
8181
.subcommand(
8282
SubCommand::with_name("fetchupdate")
83-
.setting(AppSettings::Hidden)
84-
.about("Finishes the autoupdater. Administator rights needed."),
85-
)
83+
.setting(AppSettings::Hidden)
84+
.about("Finishes the autoupdater. Administator rights needed."),
85+
)
8686
.subcommand(
8787
SubCommand::with_name("cleartemp")
88-
.setting(AppSettings::Hidden)
89-
.about("Removes tempfiles. Administator rights needed."),
90-
)
88+
.setting(AppSettings::Hidden)
89+
.about("Removes tempfiles. Administator rights needed."),
90+
)
9191
.subcommand(
9292
SubCommand::with_name("elevateddownload")
93-
.setting(AppSettings::Hidden)
94-
.about("Downloads course from the tempfile. Administator rights needed."),
95-
)
93+
.setting(AppSettings::Hidden)
94+
.about("Downloads course from the tempfile. Administator rights needed."),
95+
)
9696
.subcommand(SubCommand::with_name("update").about("Update exercises"))
9797
.arg(
9898
Arg::with_name("no-update")
99-
.short("d")
100-
.long("no-update")
101-
.help("Disable auto update temporarily"),
102-
)
99+
.short("d")
100+
.long("no-update")
101+
.help("Disable auto update temporarily"),
102+
)
103103
.arg(
104104
Arg::with_name("testmode")
105-
.long("testmode")
106-
.help("Only for internal testing, disables server connection"),
107-
)
105+
.long("testmode")
106+
.help("Only for internal testing, disables server connection"),
107+
)
108+
.subcommand(SubCommand::with_name("generate-completions")
109+
.usage("tmc generate_completions --[your shell] > /path/to/your/completions/folder")
110+
.about("Generate completion scripts for command line usage.")
111+
.setting(AppSettings::DisableVersion)
112+
.setting(AppSettings::Hidden)
113+
.setting(AppSettings::DeriveDisplayOrder)
114+
.arg(
115+
Arg::with_name("bash")
116+
.short("b")
117+
.long("bash"))
118+
.arg(
119+
Arg::with_name("zsh")
120+
.short("z")
121+
.long("zsh")
122+
)
123+
.arg(
124+
Arg::with_name("powershell")
125+
.short("p")
126+
.long("powershell"))
127+
)
108128
}

src/main.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
use clap::{ArgMatches, Shell};
12
use termcolor::{BufferWriter, ColorChoice};
23

3-
use std::io::{stdin, stdout};
4+
use std::{
5+
io,
6+
io::{stdin, stdout},
7+
};
48

59
pub mod io_module;
610
use io_module::IoProduction;
@@ -11,8 +15,12 @@ pub mod progress_reporting;
1115
mod updater;
1216

1317
fn main() {
14-
let matches = cli::build_cli().get_matches();
15-
18+
let cli = cli::build_cli();
19+
let matches = cli.get_matches();
20+
if matches.is_present("generate-completions") {
21+
generate_completions(&matches);
22+
return;
23+
}
1624
let mut stdout = stdout();
1725
let mut stdin = stdin();
1826

@@ -38,3 +46,18 @@ fn main() {
3846
}
3947
commands::handle(&matches, &mut io);
4048
}
49+
50+
fn generate_completions(matches: &ArgMatches) {
51+
let matches = matches.subcommand_matches("generate-completions").unwrap();
52+
let shell = if matches.is_present("bash") {
53+
Shell::Bash
54+
} else if matches.is_present("zsh") {
55+
Shell::Zsh
56+
} else if matches.is_present("powershell") {
57+
Shell::PowerShell
58+
} else {
59+
return;
60+
};
61+
62+
cli::build_cli().gen_completions_to("tmc", shell, &mut io::stdout());
63+
}

0 commit comments

Comments
 (0)