Skip to content

Commit ea2d92d

Browse files
authored
os: document os.ls (using readdir), add example (#20622)
1 parent 300b139 commit ea2d92d

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

doc/docs.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7295,14 +7295,29 @@ Make sure that in command you use a path to a V's file,
72957295
in that case you need to modify content of a folder (add new file, for example),
72967296
because changes in *message.v* will have no effect.
72977297
7298-
Functions that you want to be reloaded must have `[live]` attribute
7298+
Functions that you want to be reloaded must have `@[live]` attribute
72997299
before their definition.
73007300
73017301
Right now it's not possible to modify types while the program is running.
73027302
73037303
More examples, including a graphical application:
73047304
[github.com/vlang/v/tree/master/examples/hot_reload](https://github.com/vlang/v/tree/master/examples/hot_reload).
73057305
7306+
#### About keeping states in hot reloading functions with v -live run
7307+
V's hot code reloading relies on marking the functions that you want to reload with `@[live]`,
7308+
then compiling a shared library of these `@[live]` functions, and then
7309+
your v program loads that shared library at runtime.
7310+
7311+
V (with the -live option) starts a new thread, that monitors the source files for changes,
7312+
and when it detects modifications, it recompiles the shared library, and reloads it at runtime,
7313+
so that new calls to those @[live] functions will be made to the newly loaded library.
7314+
7315+
It keeps all the accumulated state (from locals outside the @[live] functions,
7316+
from heap variables and from globals), allowing to tweak the code in the merged functions quickly.
7317+
7318+
When there are more substantial changes (to data structures, or to functions that were not marked),
7319+
you will have to restart the running app manually.
7320+
73067321
### Cross-platform shell scripts in V
73077322
73087323
V can be used as an alternative to Bash to write deployment scripts, build scripts, etc.

examples/readdir.v

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
// Example usage of os.ls (in the form of top-level statements)
4+
println('readdir example using os.ls')
5+
6+
entries := os.ls(os.home_dir()) or { [] }
7+
8+
for entry in entries {
9+
if os.is_dir(os.join_path(os.home_dir(), entry)) {
10+
println('dir: ${entry}')
11+
} else {
12+
println('file: ${entry}')
13+
}
14+
}

vlib/os/os.v

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ pub fn join_path_single(base string, elem string) string {
608608
}
609609

610610
// walk_ext returns a recursive list of all files in `path` ending with `ext`.
611+
// For listing only one level deep, see: `os.ls`
611612
pub fn walk_ext(path string, ext string) []string {
612613
mut res := []string{}
613614
impl_walk_ext(path, ext, mut res)
@@ -637,6 +638,7 @@ fn impl_walk_ext(path string, ext string, mut out []string) {
637638
// When a file is encountered, it will call the callback `f` with current file as argument.
638639
// Note: walk can be called even for deeply nested folders,
639640
// since it does not recurse, but processes them iteratively.
641+
// For listing only one level deep, see: `os.ls`
640642
pub fn walk(path string, f fn (string)) {
641643
if path.len == 0 {
642644
return
@@ -674,6 +676,7 @@ pub type FnWalkContextCB = fn (voidptr, string)
674676
// and the path to the file in its second parameter.
675677
// Note: walk_with_context can be called even for deeply nested folders,
676678
// since it does not recurse, but processes them iteratively.
679+
// For listing only one level deep, see: `os.ls`
677680
pub fn walk_with_context(path string, context voidptr, fcb FnWalkContextCB) {
678681
if path.len == 0 {
679682
return

vlib/os/os_nix.c.v

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,19 @@ fn init_os_args(argc int, argv &&u8) []string {
282282
return args_
283283
}
284284

285+
// ls returns ![]string of the files and dirs in the given `path` ( os.ls uses C.readdir ). Symbolic links are returned to be files. For recursive list see os.walk functions.
286+
// See also: `os.walk`, `os.walk_ext`, `os.is_dir`, `os.is_file`
287+
// Example: https://github.com/vlang/v/blob/master/examples/readdir.v
288+
// ```
289+
// entries := os.ls(os.home_dir()) or { [] }
290+
// for entry in entries {
291+
// if os.is_dir(os.join_path(os.home_dir(), entry)) {
292+
// println('dir: $entry')
293+
// } else {
294+
// println('file: $entry')
295+
// }
296+
// }
297+
// ```
285298
pub fn ls(path string) ![]string {
286299
if path.len == 0 {
287300
return error('ls() expects a folder, not an empty string')

0 commit comments

Comments
 (0)