fsw

package
v1.2.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2025 License: MIT Imports: 10 Imported by: 2

README

Pango FSWatch

This is a wrapper around https://github.com/fsnotify/fsnotify instead of only monitoring a top level folder, it allows you to monitor all folders underneath the folder you specify.

Example:

(error handling omitted to improve readability)

import (
	"fmt"
	github.com/askasoft/pango/fsw
)

// works exactly like fsnotify and implements the same API.
watcher, err := fsw.NewFileWatcher()

// watch recursive and recieve events with callback function
watcher.AddRecursive("watchdir", fsw.OpALL, func(path string, op fsw.Op) {
	fmt.Printf("%s %s\n", path, op)
})

// start watch go-routine
watcher.Start()

Documentation

Index

Constants

View Source
const (
	// OpNone none operation
	OpNone = Op(0)

	// OpCreate create operation
	OpCreate = fsnotify.Create

	// OpWrite write operation
	OpWrite = fsnotify.Write

	// OpRemove remove operation
	OpRemove = fsnotify.Remove

	// OpRename rename operation
	OpRename = fsnotify.Rename

	// OpChmod chmod operation
	OpChmod = fsnotify.Chmod

	// OpModifies modifies operations (OpCreate | OpWrite | OpRemove | OpRename)
	OpModifies = OpCreate | OpWrite | OpRemove | OpRename

	// OpALL all operations
	OpALL = Op(0xFFFFFFFF)
)

Variables

This section is empty.

Functions

func Add

func Add(path string, op Op, callback func(string, Op)) error

Add add a file to watch on specified operation op occurred

func AddRecursive

func AddRecursive(path string, op Op, cb func(string, Op)) error

AddRecursive add files and all sub-directories under the path to watch

func Close added in v1.2.3

func Close() error

Close stop file watching go-routine and removes all watches

func Remove

func Remove(path string) error

Remove stop watching the file

func RemoveRecursive

func RemoveRecursive(path string) error

RemoveRecursive stops watching the directory and all sub-directories.

func SetDefault added in v1.2.0

func SetDefault(fw *FileWatcher)

SetDefault set the default FileWatcher instance used by the package-level functions.

func Start

func Start() error

Start start file watching go-routine

func Stop

func Stop() error

Stop stop file watching go-routine

Types

type Callback added in v1.2.3

type Callback func(string, Op)

Callback callback function

type FileWatcher

type FileWatcher struct {
	Logger log.Logger
	// contains filtered or unexported fields
}

FileWatcher struct for file watching

func Default

func Default() *FileWatcher

Default returns the default FileWatcher instance used by the package-level functions.

func NewFileWatcher

func NewFileWatcher(delays ...time.Duration) *FileWatcher

NewFileWatcher create a FileWatcher with default 1sec delay. Some editor use create->rename to save file, This could raise 2 OpWrite events continuously, delay some time to prevent duplicated event.

func (*FileWatcher) Add

func (fw *FileWatcher) Add(path string, op Op, callback Callback) error

Add add a file to watch on specified operation op occurred

func (*FileWatcher) AddRecursive

func (fw *FileWatcher) AddRecursive(path string, op Op, cb Callback) error

AddRecursive add files and all sub-directories under the path to watch

func (*FileWatcher) Close added in v1.2.3

func (fw *FileWatcher) Close() error

Close stop file watching go-routine and removes all watches

func (*FileWatcher) Remove

func (fw *FileWatcher) Remove(path string) error

Remove stop watching the file

func (*FileWatcher) RemoveRecursive

func (fw *FileWatcher) RemoveRecursive(path string) error

RemoveRecursive stops watching the directory and all sub-directories.

func (*FileWatcher) Start

func (fw *FileWatcher) Start() error

Start start file watching go-routine

func (*FileWatcher) Stop

func (fw *FileWatcher) Stop() error

Stop stop file watching go-routine

type Op

type Op = fsnotify.Op

Op describes a set of file operations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL