Skip to content

Commit 9278259

Browse files
authored
Add un and under inverses for &cd (#797)
* Add `un` and `under` inverses for `&cd` - `°&cd` will output the current directory. - `⍜&cd` will switch back to the original directory after calling `⍜`'s second function * Update documentation
1 parent d574206 commit 9278259

File tree

9 files changed

+37
-0
lines changed

9 files changed

+37
-0
lines changed

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ This version is not yet released. If you are reading this on the website, then t
4141
- [`&udpsml`](https://uiua.org/docs/&udpsml) to set the maximum message length
4242
- Rename `◿ modulus` to [`modulo ◿`](https://uiua.org/docs/modulo)
4343
- "Modulo" is the correct term for the operation this function conducts
44+
- Add [`un °`](https://uiua.org/docs/un) and [`under ⍜`](https://uiua.org/docs/under) capability to [`&cd`](https://uiua.org/docs/&cd)
45+
- [`un °`](https://uiua.org/docs/un)[`&cd`](https://uiua.org/docs/&cd) will output the current working directory
46+
- [`under ⍜`](https://uiua.org/docs/under)[`&cd`](https://uiua.org/docs/&cd) will return to the original directory afterward
4447
### Interpreter
4548
- The fomatter no longer truncates trailing decimal `0`s from number literals
4649
- Implement filled adjacent [`stencil ⧈`](https://uiua.org/docs/stencil)

parser/src/defs.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3686,6 +3686,9 @@ sys_op! {
36863686
/// [under][&runs] calls [&cl] on all 3 streams automatically.
36873687
(1(3), RunStream, Command, "&runs", "run command stream", Mutating),
36883688
/// Change the current directory
3689+
///
3690+
/// [un][&cd] will output the current working directory.
3691+
/// [under][&cd] will return to the original directory afterward.
36893692
(1(0), ChangeDirectory, Filesystem, "&cd", "change directory", Mutating),
36903693
/// Get the contents of the clipboard
36913694
///

site/src/primitive.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ fn all_uns() -> impl IntoView {
279279
{ inverse_row([ImageEncode], Optional, "Decodes bytes", None) }
280280
{ inverse_row([GifEncode], Optional, "Decodes bytes", None) }
281281
{ inverse_row([Sys(Clip)], No, "Set the clipboard", None) }
282+
{ inverse_row([Sys(ChangeDirectory)], No, "Get the current directory", None) }
282283
{ inverse_row([Sys(RawMode)], No, "Terminal raw state", None) }
283284
</table>
284285
}
@@ -359,6 +360,7 @@ fn all_unders() -> impl IntoView {
359360
{ inverse_row([Sys(TcpAccept)], Optional, view!("Calls "<Prim prim=Sys(Close)/>" on handle"), None) }
360361
{ inverse_row([Sys(FReadAllStr)], Optional, view!("Calls "<Prim prim=Sys(FWriteAll)/>), None) }
361362
{ inverse_row([Sys(FReadAllBytes)], Optional, view!("Calls "<Prim prim=Sys(FWriteAll)/>), None) }
363+
{ inverse_row([Sys(ChangeDirectory)], Optional, "Returns to original directory", None) }
362364
{ inverse_row([Sys(RawMode)], Optional, "Resets raw state", None) }
363365
</table>
364366
}

src/compile/invert/un.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ inverse!(PrimPat, input, _, Prim(prim, span), {
12671267
AudioEncode => ImplPrim(AudioDecode, span),
12681268
ImageEncode => ImplPrim(ImageDecode, span),
12691269
Sys(SysOp::Clip) => ImplPrim(UnClip, span),
1270+
Sys(SysOp::ChangeDirectory) => ImplPrim(UnChangeDirectory, span),
12701271
Sys(SysOp::RawMode) => ImplPrim(UnRawMode, span),
12711272
Hsv => ImplPrim(UnHsv, span),
12721273
Json => ImplPrim(UnJson, span),

src/compile/invert/under.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,15 @@ static UNDER_PATTERNS: &[&dyn UnderPattern] = &[
239239
&(Now, (Now, PushUnd(1)), (Now, PopUnd(1), Sub)),
240240
&MaybeVal(Store1Copy(Sys(SysOp::FOpen), Sys(SysOp::Close))),
241241
&MaybeVal(Store1Copy(Sys(SysOp::FCreate), Sys(SysOp::Close))),
242+
&MaybeVal((
243+
Sys(SysOp::ChangeDirectory),
244+
(
245+
ImplPrimitive::UnChangeDirectory,
246+
PushUnd(1),
247+
Sys(SysOp::ChangeDirectory),
248+
),
249+
(PopUnd(1), Sys(SysOp::ChangeDirectory)),
250+
)),
242251
&MaybeVal(Store1Copy(Sys(SysOp::TcpConnect), Sys(SysOp::Close))),
243252
&MaybeVal(Store1Copy(Sys(SysOp::TlsConnect), Sys(SysOp::Close))),
244253
&MaybeVal(Store1Copy(Sys(SysOp::TcpAccept), Sys(SysOp::Close))),

src/impl_prim.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ impl_primitive!(
166166
(1(2), GifDecode),
167167
(1(3), AudioDecode),
168168
(0, UnRawMode, Impure),
169+
(0, UnChangeDirectory, Impure),
169170
(1(0), UnClip, Mutating),
170171
// Unders
171172
(1, UndoFix),
@@ -358,6 +359,7 @@ impl fmt::Display for ImplPrimitive {
358359
GifDecode => write!(f, "{Un}{GifEncode}"),
359360
AudioDecode => write!(f, "{Un}{AudioEncode}"),
360361
UnRawMode => write!(f, "{Un}{}", Sys(SysOp::RawMode)),
362+
UnChangeDirectory => write!(f, "{Un}{}", Sys(SysOp::ChangeDirectory)),
361363
UnClip => write!(f, "{Un}{}", Sys(SysOp::Clip)),
362364
UndoUnBits => write!(f, "{Under}{Un}{Bits}"),
363365
AntiBase => write!(f, "{Anti}{Base}"),

src/run_prim.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,12 @@ impl ImplPrimitive {
718718
let raw_mode = env.rt.backend.get_raw_mode().map_err(|e| env.error(e))?;
719719
env.push(raw_mode);
720720
}
721+
ImplPrimitive::UnChangeDirectory => {
722+
let cur_dir = (env.rt.backend)
723+
.get_current_directory()
724+
.map_err(|e| env.error(e))?;
725+
env.push(cur_dir);
726+
}
721727
ImplPrimitive::UnClip => {
722728
let contents = env.pop(1)?.as_string(env, "Contents must be a string")?;
723729
(env.rt.backend)

src/sys/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,10 @@ pub trait SysBackend: Any + Send + Sync + 'static {
484484
fn change_directory(&self, path: &str) -> Result<(), String> {
485485
Err("Changing directories is not supported in this environment".into())
486486
}
487+
/// Get the current directory
488+
fn get_current_directory(&self) -> Result<String, String> {
489+
Err("Getting the current directory is not supported in this environment".into())
490+
}
487491
/// Capture an image from the webcam
488492
fn webcam_capture(&self, index: usize) -> Result<WebcamImage, String> {
489493
Err("Capturing from webcam is not supported in this environment".into())

src/sys/native.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,6 +1123,13 @@ impl SysBackend for NativeSys {
11231123
fn change_directory(&self, path: &str) -> Result<(), String> {
11241124
env::set_current_dir(path).map_err(|e| e.to_string())
11251125
}
1126+
fn get_current_directory(&self) -> Result<String, String> {
1127+
env::current_dir().map_err(|e| e.to_string()).and_then(|p| {
1128+
p.into_os_string()
1129+
.into_string()
1130+
.map_err(|_| "Path was not valid unicode".into())
1131+
})
1132+
}
11261133
#[cfg(feature = "webcam")]
11271134
fn webcam_capture(&self, index: usize) -> Result<crate::WebcamImage, String> {
11281135
let cam_channels = &NATIVE_SYS.cam_channels;

0 commit comments

Comments
 (0)