Skip to content

Commit

Permalink
Add notebook configuration to set default notebook path
Browse files Browse the repository at this point in the history
  • Loading branch information
lsvmello committed Mar 30, 2023
1 parent a8d1db4 commit 378269d
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
5 changes: 5 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Each [notebook](notebook.md) contains a configuration file used to customize your experience with `zk`. This file is located at `.zk/config.toml` and uses the [TOML format](https://github.com/toml-lang/toml). It is composed of several optional sections:

* `[notebook]` sets the [notebook rules](config-notebook.md)
* `[note]` sets the [note creation rules](config-note.md)
* `[extra]` contains free [user variables](config-extra.md) which can be expanded in templates
* `[group]` defines [note groups](config-group.md) with custom rules
Expand All @@ -26,6 +27,10 @@ Notebook configuration files will inherit the settings defined in the global con
Here's an example of a complete configuration file:

```toml
# NOTEBOOK SETTINGS
[notebook]
dir = "~/notebook"

# NOTE SETTINGS
[note]

Expand Down
2 changes: 2 additions & 0 deletions docs/notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ To create a new notebook, simply run `zk init [<directory>]`.

Most `zk` commands are operating "Git-style" on the notebook containing the current working directory (or one of its parents). However, you can explicitly set which notebook to use with `--notebook-dir` or the `ZK_NOTEBOOK_DIR` environment variable. Setting `ZK_NOTEBOOK_DIR` in your shell configuration (e.g. `~/.profile`) can be used to define a default notebook which `zk` commands will use when the working directory is not in another notebook.

If the [default notebook](config-notebook.md) is set it will be used as `ZK_NOTEBOOK_DIR`, unless this environment variable is not already set.

## Anatomy of a notebook

Similarly to Git, a notebook is identified by the presence of a `.zk` directory at its root. This directory contains the only `zk`-specific files in your notebook:
Expand Down
17 changes: 17 additions & 0 deletions internal/cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"io"
"os"
"path/filepath"
"strings"

"github.com/mickael-menu/zk/internal/adapter/editor"
"github.com/mickael-menu/zk/internal/adapter/fs"
Expand Down Expand Up @@ -71,6 +72,22 @@ func NewContainer(version string) (*Container, error) {
}
}

// Set the default notebook if not already set
// might be overrided if --notebook-dir flag is present
if osutil.GetOptEnv("ZK_NOTEBOOK_DIR").IsNull() && !config.Notebook.Dir.IsNull() {
// Expand in case there are environment variables on the path
notebookDir := os.Expand(config.Notebook.Dir.Unwrap(), os.Getenv)
if strings.HasPrefix(notebookDir, "~") {
dirname, err := os.UserHomeDir()
if err != nil {
return nil, wrap(err)
}
notebookDir = filepath.Join(dirname, notebookDir[1:])
}
os.Setenv("ZK_NOTEBOOK_DIR", notebookDir)
// TODO: docs about environment variables and ~/
}

// Set the default shell if not already set
if osutil.GetOptEnv("ZK_SHELL").IsNull() && !config.Tool.Shell.IsEmpty() {
os.Setenv("ZK_SHELL", config.Tool.Shell.Unwrap())
Expand Down
52 changes: 36 additions & 16 deletions internal/core/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,23 @@ import (

// Config holds the user configuration.
type Config struct {
Note NoteConfig
Groups map[string]GroupConfig
Format FormatConfig
Tool ToolConfig
LSP LSPConfig
Filters map[string]string
Aliases map[string]string
Extra map[string]string
Notebook NotebookConfig
Note NoteConfig
Groups map[string]GroupConfig
Format FormatConfig
Tool ToolConfig
LSP LSPConfig
Filters map[string]string
Aliases map[string]string
Extra map[string]string
}

// NewDefaultConfig creates a new Config with the default settings.
func NewDefaultConfig() Config {
return Config{
Notebook: NotebookConfig{
Dir: opt.NullString,
},
Note: NoteConfig{
FilenameTemplate: "{{id}}",
Extension: "md",
Expand Down Expand Up @@ -192,6 +196,11 @@ const (
LSPDiagnosticHint LSPDiagnosticSeverity = 4
)

// NotebookConfig holds configuration about the default notebook
type NotebookConfig struct {
Dir opt.String
}

// NoteConfig holds the user configuration used when generating new notes.
type NoteConfig struct {
// Handlebars template used when generating a new filename.
Expand Down Expand Up @@ -280,6 +289,12 @@ func ParseConfig(content []byte, path string, parentConfig Config) (Config, erro
return config, wrap(err)
}

// Notebook
notebook := tomlConf.Notebook
if notebook.Dir != "" {
config.Notebook.Dir = opt.NewNotEmptyString(notebook.Dir)
}

// Note
note := tomlConf.Note
if note.Filename != "" {
Expand Down Expand Up @@ -472,14 +487,19 @@ func (c GroupConfig) merge(tomlConf tomlGroupConfig, name string) GroupConfig {

// tomlConfig holds the TOML representation of Config
type tomlConfig struct {
Note tomlNoteConfig
Groups map[string]tomlGroupConfig `toml:"group"`
Format tomlFormatConfig
Tool tomlToolConfig
LSP tomlLSPConfig
Extra map[string]string
Filters map[string]string `toml:"filter"`
Aliases map[string]string `toml:"alias"`
Notebook tomlNotebookConfig
Note tomlNoteConfig
Groups map[string]tomlGroupConfig `toml:"group"`
Format tomlFormatConfig
Tool tomlToolConfig
LSP tomlLSPConfig
Extra map[string]string
Filters map[string]string `toml:"filter"`
Aliases map[string]string `toml:"alias"`
}

type tomlNotebookConfig struct {
Dir string
}

type tomlNoteConfig struct {
Expand Down
9 changes: 9 additions & 0 deletions internal/core/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ func TestParseDefaultConfig(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, conf, Config{
Notebook: NotebookConfig{
Dir: opt.NullString,
},
Note: NoteConfig{
FilenameTemplate: "{{id}}",
Extension: "md",
Expand Down Expand Up @@ -66,6 +69,9 @@ func TestParseComplete(t *testing.T) {
conf, err := ParseConfig([]byte(`
# Comment
[notebook]
dir = "~/notebook"
[note]
filename = "{{id}}.note"
extension = "txt"
Expand Down Expand Up @@ -142,6 +148,9 @@ func TestParseComplete(t *testing.T) {

assert.Nil(t, err)
assert.Equal(t, conf, Config{
Notebook: NotebookConfig{
Dir: opt.NewString("~/notebook"),
},
Note: NoteConfig{
FilenameTemplate: "{{id}}.note",
Extension: "txt",
Expand Down

0 comments on commit 378269d

Please sign in to comment.