Skip to content

Commit 629935f

Browse files
alex-s168kaikalii
andauthored
add file seek (&sb) (#718)
* add file seek (&sb) * add &sb to primitives.json * fmt * allow &sb to seek from file end * oops * change seek name to `&seek` --------- Co-authored-by: Kai Schmidt <[email protected]>
1 parent bcfa765 commit 629935f

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

parser/src/defs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3742,6 +3742,8 @@ sys_op! {
37423742
/// ex: &cl &w "Hello, world!" . &fc "file.txt"
37433743
/// : &fras "file.txt"
37443744
(2(0), Write, Stream, "&w", "write", Mutating),
3745+
/// Moves to a absolute position in the file under the stream.
3746+
(2(0), Seek, Stream, "&seek", "seek", Mutating),
37453747
/// Invoke a path with the system's default program
37463748
(1(1), Invoke, Command, "&invk", "invoke", Mutating),
37473749
/// Close a stream by its handle

site/primitives.json

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,12 @@
335335
"class": "Stream",
336336
"description": "Write an array to a stream"
337337
},
338+
"&sb": {
339+
"args": 2,
340+
"outputs": 0,
341+
"class": "Stream",
342+
"description": "Goes to the position in a stream"
343+
},
338344
"above": {
339345
"glyph": "",
340346
"outputs": 1,
@@ -1475,4 +1481,4 @@
14751481
"class": "Encoding",
14761482
"description": "Encode an array into XLSX bytes"
14771483
}
1478-
}
1484+
}

src/sys/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ pub trait SysBackend: Any + Send + Sync + 'static {
298298
fn write(&self, handle: Handle, contents: &[u8]) -> Result<(), String> {
299299
Err("Writing to streams is not supported in this environment".into())
300300
}
301+
/// Go to an absolute file position
302+
fn seek(&self, handle: Handle, offset: std::io::SeekFrom) -> Result<(), String> {
303+
Err("Seeking streams is not supported in this environment".into())
304+
}
301305
/// Create a file
302306
fn create_file(&self, path: &Path) -> Result<Handle, String> {
303307
Err("Creating files is not supported in this environment".into())
@@ -860,6 +864,17 @@ pub(crate) fn run_sys_op(op: &SysOp, env: &mut Uiua) -> UiuaResult {
860864
_ => (env.rt.backend.write(handle, &bytes)).map_err(|e| env.error(e))?,
861865
}
862866
}
867+
SysOp::Seek => {
868+
use std::io::SeekFrom;
869+
870+
let pos = env.pop(1)?.as_int(env, None)?;
871+
let pos = match pos {
872+
..0 => SeekFrom::End(pos as i64),
873+
0.. => SeekFrom::Start(pos as u64),
874+
};
875+
let handle = env.pop(2)?.as_handle(env, None)?;
876+
env.rt.backend.seek(handle, pos).map_err(|e| env.error(e))?;
877+
}
863878
SysOp::FReadAllStr => {
864879
let path = env.pop(1)?.as_string(env, "Path must be a string")?;
865880
let bytes = (env.rt.backend)

src/sys/native.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,14 @@ impl SysBackend for NativeSys {
608608
}
609609
}
610610
}
611+
fn seek(&self, handle: Handle, offset: std::io::SeekFrom) -> Result<(), String> {
612+
use std::io::Seek;
613+
614+
match NATIVE_SYS.get_stream(handle)? {
615+
SysStream::File(mut file) => file.seek(offset).map_err(|e| e.to_string()).map(|_| ()),
616+
_ => Err("Cannot seek this stream".into()),
617+
}
618+
}
611619
#[cfg(feature = "clipboard")]
612620
fn clipboard(&self) -> Result<String, String> {
613621
use arboard::*;

0 commit comments

Comments
 (0)