@@ -11,6 +11,7 @@ use clap::{
11
11
} ;
12
12
use clap_complete:: Shell ;
13
13
14
+ use crate :: config:: new_toolchain_with_reason;
14
15
use crate :: {
15
16
cli:: {
16
17
common:: { self , PackageUpdate } ,
@@ -39,8 +40,9 @@ use crate::{
39
40
names:: {
40
41
custom_toolchain_name_parser, maybe_resolvable_toolchainame_parser,
41
42
partial_toolchain_desc_parser, resolvable_local_toolchainame_parser,
42
- resolvable_toolchainame_parser, CustomToolchainName , MaybeResolvableToolchainName ,
43
- ResolvableLocalToolchainName , ResolvableToolchainName , ToolchainName ,
43
+ resolvable_toolchainame_parser, CustomToolchainName , LocalToolchainName ,
44
+ MaybeResolvableToolchainName , ResolvableLocalToolchainName , ResolvableToolchainName ,
45
+ ToolchainName ,
44
46
} ,
45
47
toolchain:: Toolchain ,
46
48
} ,
@@ -1081,127 +1083,112 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1081
1083
1082
1084
let cwd = utils:: current_dir ( ) ?;
1083
1085
let installed_toolchains = cfg. list_toolchains ( ) ?;
1084
- // XXX: we may want a find_without_install capability for show.
1085
- let active_toolchain = cfg. find_or_install_active_toolchain ( & cwd) ;
1086
-
1087
- // active_toolchain will carry the reason we don't have one in its detail.
1088
- let active_targets = if let Ok ( ref at) = active_toolchain {
1089
- if let Ok ( distributable) = DistributableToolchain :: try_from ( & at. 0 ) {
1090
- let components = ( || {
1091
- let manifestation = distributable. get_manifestation ( ) ?;
1092
- let config = manifestation. read_config ( ) ?. unwrap_or_default ( ) ;
1093
- let manifest = distributable. get_manifest ( ) ?;
1094
- manifest. query_components ( distributable. desc ( ) , & config)
1095
- } ) ( ) ;
1096
-
1097
- match components {
1098
- Ok ( cs_vec) => cs_vec
1099
- . into_iter ( )
1100
- . filter ( |c| c. component . short_name_in_manifest ( ) == "rust-std" )
1101
- . filter ( |c| c. installed )
1102
- . collect ( ) ,
1103
- Err ( _) => vec ! [ ] ,
1104
- }
1086
+ let active_toolchain_and_reason: Option < ( ToolchainName , ActiveReason ) > =
1087
+ if let Ok ( Some ( ( LocalToolchainName :: Named ( toolchain_name) , reason) ) ) =
1088
+ cfg. find_active_toolchain ( & cwd)
1089
+ {
1090
+ Some ( ( toolchain_name, reason) )
1105
1091
} else {
1106
- // These three vec![] could perhaps be reduced with and_then on active_toolchain.
1107
- vec ! [ ]
1108
- }
1109
- } else {
1110
- vec ! [ ]
1111
- } ;
1092
+ None
1093
+ } ;
1094
+
1095
+ let ( active_toolchain_name, _active_reason) = active_toolchain_and_reason
1096
+ . as_ref ( )
1097
+ . map ( |atar| ( & atar. 0 , & atar. 1 ) )
1098
+ . unzip ( ) ;
1112
1099
1113
- let show_installed_toolchains = installed_toolchains. len ( ) > 1 ;
1114
- let show_active_targets = active_targets. len ( ) > 1 ;
1115
- let show_active_toolchain = true ;
1116
-
1117
- // Only need to display headers if we have multiple sections
1118
- let show_headers = [
1119
- show_installed_toolchains,
1120
- show_active_targets,
1121
- show_active_toolchain,
1122
- ]
1123
- . iter ( )
1124
- . filter ( |x| * * x)
1125
- . count ( )
1126
- > 1 ;
1127
-
1128
- if show_installed_toolchains {
1100
+ let active_targets: Vec < ComponentStatus > = active_toolchain_name
1101
+ . and_then ( |atn| match atn {
1102
+ ToolchainName :: Official ( desc) => DistributableToolchain :: new ( cfg, desc. clone ( ) ) . ok ( ) ,
1103
+ _ => None ,
1104
+ } )
1105
+ . and_then ( |distributable| {
1106
+ let manifestation = distributable. get_manifestation ( ) . ok ( ) ?;
1107
+ let config = manifestation. read_config ( ) . ok ( ) ?. unwrap_or_default ( ) ;
1108
+ let manifest = distributable. get_manifest ( ) . ok ( ) ?;
1109
+ manifest
1110
+ . query_components ( distributable. desc ( ) , & config)
1111
+ . ok ( )
1112
+ } )
1113
+ . map ( |cs_vec| {
1114
+ cs_vec
1115
+ . into_iter ( )
1116
+ . filter ( |c| c. component . short_name_in_manifest ( ) == "rust-std" )
1117
+ . filter ( |c| c. installed )
1118
+ . collect ( )
1119
+ } )
1120
+ . unwrap_or_default ( ) ;
1121
+
1122
+ // show installed toolchains
1123
+ {
1129
1124
let mut t = process ( ) . stdout ( ) . terminal ( ) ;
1130
1125
1131
- if show_headers {
1132
- print_header :: < Error > ( & mut t, "installed toolchains" ) ?;
1133
- }
1134
- let default_name = cfg
1135
- . get_default ( ) ?
1136
- . ok_or_else ( || anyhow ! ( "no default toolchain configured" ) ) ?;
1137
- for it in installed_toolchains {
1138
- if default_name == it {
1139
- writeln ! ( t. lock( ) , "{it} (default)" ) ?;
1140
- } else {
1141
- writeln ! ( t. lock( ) , "{it}" ) ?;
1142
- }
1126
+ print_header :: < Error > ( & mut t, "installed toolchains" ) ?;
1127
+
1128
+ let default_toolchain_name = cfg. get_default ( ) ?;
1129
+
1130
+ let last_index = installed_toolchains. len ( ) . wrapping_sub ( 1 ) ;
1131
+ for ( n, toolchain_name) in installed_toolchains. into_iter ( ) . enumerate ( ) {
1132
+ let is_default_toolchain = default_toolchain_name. as_ref ( ) == Some ( & toolchain_name) ;
1133
+ let is_active_toolchain = active_toolchain_name == Some ( & toolchain_name) ;
1134
+
1135
+ let status_str = match ( is_default_toolchain, is_active_toolchain) {
1136
+ ( true , true ) => " (default, active)" ,
1137
+ ( true , false ) => " (default)" ,
1138
+ ( false , true ) => " (active)" ,
1139
+ ( false , false ) => "" ,
1140
+ } ;
1141
+
1142
+ writeln ! ( t. lock( ) , "{toolchain_name}{status_str}" ) ?;
1143
+
1143
1144
if verbose {
1144
- let toolchain = Toolchain :: new ( cfg, it. into ( ) ) ?;
1145
- writeln ! ( process( ) . stdout( ) . lock( ) , "{}" , toolchain. rustc_version( ) ) ?;
1146
- // To make it easy to see what rustc that belongs to what
1147
- // toolchain we separate each pair with an extra newline
1148
- writeln ! ( process( ) . stdout( ) . lock( ) ) ?;
1145
+ let toolchain = Toolchain :: new ( cfg, toolchain_name. into ( ) ) ?;
1146
+ writeln ! ( process( ) . stdout( ) . lock( ) , " {}" , toolchain. rustc_version( ) ) ?;
1147
+ // To make it easy to see which rustc belongs to which
1148
+ // toolchain, we separate each pair with an extra newline.
1149
+ if n != last_index {
1150
+ writeln ! ( process( ) . stdout( ) . lock( ) ) ?;
1151
+ }
1149
1152
}
1150
1153
}
1151
- if show_headers {
1152
- writeln ! ( t. lock( ) ) ?
1153
- } ;
1154
1154
}
1155
1155
1156
- if show_active_targets {
1156
+ // show active toolchain
1157
+ {
1157
1158
let mut t = process ( ) . stdout ( ) . terminal ( ) ;
1158
1159
1159
- if show_headers {
1160
- print_header :: < Error > ( & mut t, "installed targets for active toolchain" ) ?;
1161
- }
1162
- for at in active_targets {
1163
- writeln ! (
1164
- t. lock( ) ,
1165
- "{}" ,
1166
- at. component
1167
- . target
1168
- . as_ref( )
1169
- . expect( "rust-std should have a target" )
1170
- ) ?;
1171
- }
1172
- if show_headers {
1173
- writeln ! ( t. lock( ) ) ?;
1174
- } ;
1175
- }
1176
-
1177
- if show_active_toolchain {
1178
- let mut t = process ( ) . stdout ( ) . terminal ( ) ;
1160
+ writeln ! ( t. lock( ) ) ?;
1179
1161
1180
- if show_headers {
1181
- print_header :: < Error > ( & mut t, "active toolchain" ) ?;
1182
- }
1162
+ print_header :: < Error > ( & mut t, "active toolchain" ) ?;
1183
1163
1184
- match active_toolchain {
1185
- Ok ( ( ref toolchain, ref reason) ) => {
1186
- writeln ! ( t. lock( ) , "{} ({})" , toolchain. name( ) , reason) ?;
1187
- writeln ! ( t. lock( ) , "{}" , toolchain. rustc_version( ) ) ?;
1188
- }
1189
- Err ( err) => {
1190
- let root_cause = err. root_cause ( ) ;
1191
- if let Some ( RustupError :: ToolchainNotSelected ) =
1192
- root_cause. downcast_ref :: < RustupError > ( )
1193
- {
1194
- writeln ! ( t. lock( ) , "no active toolchain" ) ?;
1195
- } else if let Some ( cause) = err. source ( ) {
1196
- writeln ! ( t. lock( ) , "(error: {err}, {cause})" ) ?;
1197
- } else {
1198
- writeln ! ( t. lock( ) , "(error: {err})" ) ?;
1164
+ match active_toolchain_and_reason {
1165
+ Some ( ( active_toolchain_name, active_reason) ) => {
1166
+ let active_toolchain = new_toolchain_with_reason (
1167
+ cfg,
1168
+ active_toolchain_name. clone ( ) . into ( ) ,
1169
+ & active_reason,
1170
+ ) ?;
1171
+ writeln ! ( t. lock( ) , "name: {}" , active_toolchain. name( ) ) ?;
1172
+ writeln ! ( t. lock( ) , "compiler: {}" , active_toolchain. rustc_version( ) ) ?;
1173
+ writeln ! ( t. lock( ) , "active because: {}" , active_reason. to_string( ) ) ?;
1174
+
1175
+ // show installed targets for the active toolchain
1176
+ writeln ! ( t. lock( ) , "installed targets:" ) ?;
1177
+
1178
+ for at in active_targets {
1179
+ writeln ! (
1180
+ t. lock( ) ,
1181
+ " {}" ,
1182
+ at. component
1183
+ . target
1184
+ . as_ref( )
1185
+ . expect( "rust-std should have a target" )
1186
+ ) ?;
1199
1187
}
1200
1188
}
1201
- }
1202
-
1203
- if show_headers {
1204
- writeln ! ( t. lock( ) ) ?
1189
+ None => {
1190
+ writeln ! ( t. lock( ) , "no active toolchain" ) ?;
1191
+ }
1205
1192
}
1206
1193
}
1207
1194
@@ -1210,9 +1197,11 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1210
1197
E : From < std:: io:: Error > ,
1211
1198
{
1212
1199
t. attr ( terminalsource:: Attr :: Bold ) ?;
1213
- writeln ! ( t. lock( ) , "{s}" ) ?;
1214
- writeln ! ( t. lock( ) , "{}" , "-" . repeat( s. len( ) ) ) ?;
1215
- writeln ! ( t. lock( ) ) ?;
1200
+ {
1201
+ let mut term_lock = t. lock ( ) ;
1202
+ writeln ! ( term_lock, "{s}" ) ?;
1203
+ writeln ! ( term_lock, "{}" , "-" . repeat( s. len( ) ) ) ?;
1204
+ } // drop the term_lock
1216
1205
t. reset ( ) ?;
1217
1206
Ok ( ( ) )
1218
1207
}
@@ -1224,27 +1213,27 @@ fn show(cfg: &Cfg, m: &ArgMatches) -> Result<utils::ExitCode> {
1224
1213
fn show_active_toolchain ( cfg : & Cfg , m : & ArgMatches ) -> Result < utils:: ExitCode > {
1225
1214
let verbose = m. get_flag ( "verbose" ) ;
1226
1215
let cwd = utils:: current_dir ( ) ?;
1227
- match cfg. find_or_install_active_toolchain ( & cwd) {
1228
- Err ( e) => {
1229
- let root_cause = e. root_cause ( ) ;
1230
- if let Some ( RustupError :: ToolchainNotSelected ) =
1231
- root_cause. downcast_ref :: < RustupError > ( )
1232
- {
1233
- } else {
1234
- return Err ( e) ;
1235
- }
1236
- }
1237
- Ok ( ( toolchain, reason) ) => {
1216
+ match cfg. find_active_toolchain ( & cwd) ? {
1217
+ Some ( ( toolchain_name, reason) ) => {
1218
+ let toolchain = new_toolchain_with_reason ( cfg, toolchain_name. clone ( ) . into ( ) , & reason) ?;
1238
1219
writeln ! (
1239
1220
process( ) . stdout( ) . lock( ) ,
1240
- "{} ({}) " ,
1221
+ "{}\n active because: {} " ,
1241
1222
toolchain. name( ) ,
1242
1223
reason
1243
1224
) ?;
1244
1225
if verbose {
1245
- writeln ! ( process( ) . stdout( ) . lock( ) , "{}" , toolchain. rustc_version( ) ) ?;
1226
+ writeln ! (
1227
+ process( ) . stdout( ) . lock( ) ,
1228
+ "compiler: {}" ,
1229
+ toolchain. rustc_version( )
1230
+ ) ?;
1246
1231
}
1247
1232
}
1233
+ None => writeln ! (
1234
+ process( ) . stdout( ) . lock( ) ,
1235
+ "no default toolchain is configured"
1236
+ ) ?,
1248
1237
}
1249
1238
Ok ( utils:: ExitCode ( 0 ) )
1250
1239
}
0 commit comments