<commit_before>/*
Package tview implements rich widgets for terminal based user interfaces. The
widgets provided with this package are useful for data exploration and data
entry.

Widgets

The package implements the following widgets:

  - TextView: A scrollable window that display multi-colored text. Text may also
    be highlighted.
  - Table: A scrollable display of tabular data. Table cells, rows, or columns
    may also be highlighted.
  - TreeView: A scrollable display for hierarchical data. Tree nodes can be
    highlighted, collapsed, expanded, and more.
  - List: A navigable text list with optional keyboard shortcuts.
  - InputField: One-line input fields to enter text.
  - DropDown: Drop-down selection fields.
  - Checkbox: Selectable checkbox for boolean values.
  - Button: Buttons which get activated when the user selects them.
  - Form: Forms composed of input fields, drop down selections, checkboxes, and
    buttons.
  - Modal: A centered window with a text message and one or more buttons.
  - Flex: A Flexbox based layout manager.
  - Pages: A page based layout manager.

The package also provides Application which is used to poll the event queue and
draw widgets on screen.

Hello World

The following is a very basic example showing a box with the title "Hello,
world!":

  package main

  import (
  	"github.com/rivo/tview"
  )

  func main() {
  	box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
  	if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
  		panic(err)
  	}
  }

First, we create a box primitive with a border and a title. Then we create an
application, set the box as its root primitive, and run the event loop. The
application exits when the application's Stop() function is called or when
Ctrl-C is pressed.

If we have a primitive which consumes key presses, we call the application's
SetFocus() function to redirect all key presses to that primitive. Most
primitives then offer ways to install handlers that allow you to react to any
actions performed on them.

More Demos

You will find more demos in the "demos" subdirectory. It also contains a
presentation (written using tview) which gives an overview of the different
widgets and how they can be used.

Colors

Throughout this package, colors are specified using the tcell.Color type.
Functions such as tcell.GetColor(), tcell.NewHexColor(), and tcell.NewRGBColor()
can be used to create colors from W3C color names or RGB values.

Almost all strings which are displayed can contain color tags. Color tags are
W3C color names or six hexadecimal digits following a hash tag, wrapped in
square brackets. Examples:

  This is a [red]warning[white]!
  The sky is [#8080ff]blue[#ffffff].

A color tag changes the color of the characters following that color tag. This
applies to almost everything from box titles, list text, form item labels, to
table cells. In a TextView, this functionality has to be switched on explicitly.
See the TextView documentation for more information.

Color tags may contain not just the foreground (text) color but also the
background color and additional flags. In fact, the full definition of a color
tag is as follows:

  [<foreground>:<background>:<flags>]

Each of the three fields can be left blank and trailing fields can be omitted.
(Empty square brackets "[]", however, are not considered color tags.) Colors
that are not specified will be left unchanged. A field with just a dash ("-")
means "reset to default".

You can specify the following flags (some flags may not be supported by your
terminal):

  l: blink
  b: bold
  d: dim
  r: reverse (switch foreground and background color)
  u: underline

Examples:

  [yellow]Yellow text
  [yellow:red]Yellow text on red background
  [:red]Red background, text color unchanged
  [yellow::u]Yellow text underlined
  [::bl]Bold, blinking text
  [::-]Colors unchanged, flags reset
  [-]Reset foreground color
  [-:-:-]Reset everything
  [:]No effect
  []Not a valid color tag, will print square brackets as they are

In the rare event that you want to display a string such as "[red]" or
"[#00ff1a]" without applying its effect, you need to put an opening square
bracket before the closing square bracket. Note that the text inside the
brackets will be matched less strictly than region or colors tags. I.e. any
character that may be used in color or region tags will be recognized. Examples:

  [red[]      will be output as [red]
  ["123"[]    will be output as ["123"]
  [#6aff00[[] will be output as [#6aff00[]
  [a#"[[[]    will be output as [a#"[[]
  []          will be output as [] (see color tags above)
  [[]         will be output as [[] (not an escaped tag)

You can use the Escape() function to insert brackets automatically where needed.

Styles

When primitives are instantiated, they are initialized with colors taken from
the global Styles variable. You may change this variable to adapt the look and
feel of the primitives to your preferred style.

Unicode Support

This package supports unicode characters including wide characters.

Concurrency

Many functions in this package are not thread-safe. For many applications, this
may not be an issue: If your code makes changes in response to key events, it
will execute in the main goroutine and thus will not cause any race conditions.

If you access your primitives from other goroutines, however, you will need to
synchronize execution. The easiest way to do this is to call
Application.QueueUpdate() or Application.QueueUpdateDraw() (see the function
documentation for details):

  go func() {
    app.QueueUpdateDraw(func() {
      table.SetCellSimple(0, 0, "Foo bar")
    })
  }()

One exception to this is the io.Writer interface implemented by TextView. You
can safely write to a TextView from any goroutine. See the TextView
documentation for details.

Type Hierarchy

All widgets listed above contain the Box type. All of Box's functions are
therefore available for all widgets, too.

All widgets also implement the Primitive interface. There is also the Focusable
interface which is used to override functions in subclassing types.

The tview package is based on https://github.com/gdamore/tcell. It uses types
and constants from that package (e.g. colors and keyboard values).

This package does not process mouse input (yet).
*/
package tview
<commit_msg>Added some clarification about QueueUpdate() and event callbacks.<commit_after>/*
Package tview implements rich widgets for terminal based user interfaces. The
widgets provided with this package are useful for data exploration and data
entry.

Widgets

The package implements the following widgets:

  - TextView: A scrollable window that display multi-colored text. Text may also
    be highlighted.
  - Table: A scrollable display of tabular data. Table cells, rows, or columns
    may also be highlighted.
  - TreeView: A scrollable display for hierarchical data. Tree nodes can be
    highlighted, collapsed, expanded, and more.
  - List: A navigable text list with optional keyboard shortcuts.
  - InputField: One-line input fields to enter text.
  - DropDown: Drop-down selection fields.
  - Checkbox: Selectable checkbox for boolean values.
  - Button: Buttons which get activated when the user selects them.
  - Form: Forms composed of input fields, drop down selections, checkboxes, and
    buttons.
  - Modal: A centered window with a text message and one or more buttons.
  - Flex: A Flexbox based layout manager.
  - Pages: A page based layout manager.

The package also provides Application which is used to poll the event queue and
draw widgets on screen.

Hello World

The following is a very basic example showing a box with the title "Hello,
world!":

  package main

  import (
  	"github.com/rivo/tview"
  )

  func main() {
  	box := tview.NewBox().SetBorder(true).SetTitle("Hello, world!")
  	if err := tview.NewApplication().SetRoot(box, true).Run(); err != nil {
  		panic(err)
  	}
  }

First, we create a box primitive with a border and a title. Then we create an
application, set the box as its root primitive, and run the event loop. The
application exits when the application's Stop() function is called or when
Ctrl-C is pressed.

If we have a primitive which consumes key presses, we call the application's
SetFocus() function to redirect all key presses to that primitive. Most
primitives then offer ways to install handlers that allow you to react to any
actions performed on them.

More Demos

You will find more demos in the "demos" subdirectory. It also contains a
presentation (written using tview) which gives an overview of the different
widgets and how they can be used.

Colors

Throughout this package, colors are specified using the tcell.Color type.
Functions such as tcell.GetColor(), tcell.NewHexColor(), and tcell.NewRGBColor()
can be used to create colors from W3C color names or RGB values.

Almost all strings which are displayed can contain color tags. Color tags are
W3C color names or six hexadecimal digits following a hash tag, wrapped in
square brackets. Examples:

  This is a [red]warning[white]!
  The sky is [#8080ff]blue[#ffffff].

A color tag changes the color of the characters following that color tag. This
applies to almost everything from box titles, list text, form item labels, to
table cells. In a TextView, this functionality has to be switched on explicitly.
See the TextView documentation for more information.

Color tags may contain not just the foreground (text) color but also the
background color and additional flags. In fact, the full definition of a color
tag is as follows:

  [<foreground>:<background>:<flags>]

Each of the three fields can be left blank and trailing fields can be omitted.
(Empty square brackets "[]", however, are not considered color tags.) Colors
that are not specified will be left unchanged. A field with just a dash ("-")
means "reset to default".

You can specify the following flags (some flags may not be supported by your
terminal):

  l: blink
  b: bold
  d: dim
  r: reverse (switch foreground and background color)
  u: underline

Examples:

  [yellow]Yellow text
  [yellow:red]Yellow text on red background
  [:red]Red background, text color unchanged
  [yellow::u]Yellow text underlined
  [::bl]Bold, blinking text
  [::-]Colors unchanged, flags reset
  [-]Reset foreground color
  [-:-:-]Reset everything
  [:]No effect
  []Not a valid color tag, will print square brackets as they are

In the rare event that you want to display a string such as "[red]" or
"[#00ff1a]" without applying its effect, you need to put an opening square
bracket before the closing square bracket. Note that the text inside the
brackets will be matched less strictly than region or colors tags. I.e. any
character that may be used in color or region tags will be recognized. Examples:

  [red[]      will be output as [red]
  ["123"[]    will be output as ["123"]
  [#6aff00[[] will be output as [#6aff00[]
  [a#"[[[]    will be output as [a#"[[]
  []          will be output as [] (see color tags above)
  [[]         will be output as [[] (not an escaped tag)

You can use the Escape() function to insert brackets automatically where needed.

Styles

When primitives are instantiated, they are initialized with colors taken from
the global Styles variable. You may change this variable to adapt the look and
feel of the primitives to your preferred style.

Unicode Support

This package supports unicode characters including wide characters.

Concurrency

Many functions in this package are not thread-safe. For many applications, this
may not be an issue: If your code makes changes in response to key events, it
will execute in the main goroutine and thus will not cause any race conditions.

If you access your primitives from other goroutines, however, you will need to
synchronize execution. The easiest way to do this is to call
Application.QueueUpdate() or Application.QueueUpdateDraw() (see the function
documentation for details):

  go func() {
    app.QueueUpdateDraw(func() {
      table.SetCellSimple(0, 0, "Foo bar")
    })
  }()

One exception to this is the io.Writer interface implemented by TextView. You
can safely write to a TextView from any goroutine. See the TextView
documentation for details.

You can also call Application.Draw() from any goroutine without having to wrap
it in QueueUpdate(). And, as mentioned above, key event callbacks are executed
in the main goroutine and thus should not use QueueUpdate() as that may lead to
deadlocks.

Type Hierarchy

All widgets listed above contain the Box type. All of Box's functions are
therefore available for all widgets, too.

All widgets also implement the Primitive interface. There is also the Focusable
interface which is used to override functions in subclassing types.

The tview package is based on https://github.com/gdamore/tcell. It uses types
and constants from that package (e.g. colors and keyboard values).

This package does not process mouse input (yet).
*/
package tview
<|endoftext|>


<commit_before>/*
Package vlc provides golang bindings for libVLC version 2.X/3.X/4.X.

Usage

Initialization
	// Initialize libVLC. Additional command line arguments can be passed in
	// to libVLC by specifying them in the Init function.
	if err := vlc.Init("--no-video", "--quiet"); err != nil {
		log.Fatal(err)
	}
	defer vlc.Release()

Player example
	// Create a new player.
	player, err := vlc.NewPlayer()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		player.Stop()
		player.Release()
	}()

	// Add a media file from path or from URL.
	// Set player media from path:
	// media, err := player.LoadMediaFromPath("localpath/test.mp4")
	// Set player media from URL:
	media, err := player.LoadMediaFromURL("http://stream-uk1.radioparadise.com/mp3-32")
	if err != nil {
		log.Fatal(err)
	}
	defer media.Release()

	// Start playing the media.
	err = player.Play()
	if err != nil {
		log.Fatal(err)
	}

	// Retrieve player event manager.
	manager, err := player.EventManager()
	if err != nil {
		log.Fatal(err)
	}

	// Register the media end reached event with the event manager.
	quit := make(chan struct{})
	eventCallback := func(event vlc.Event, userData interface{}) {
		close(quit)
	}

	eventID, err := manager.Attach(vlc.MediaPlayerEndReached, eventCallback, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer manager.Detach(eventID)

	<-quit

List player example
    // Create a new list player.
    player, err := vlc.NewListPlayer()
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        player.Stop()
        player.Release()
    }()

    // Create a new media list.
    list, err := vlc.NewMediaList()
    if err != nil {
        log.Fatal(err)
    }
    defer list.Release()

    err = list.AddMediaFromPath("localpath/example1.mp3")
    if err != nil {
        log.Fatal(err)
    }

    err = list.AddMediaFromURL("https://example.com/media.mp4")
    if err != nil {
        log.Fatal(err)
    }

    // Set player media list.
    err = player.SetMediaList(list)
    if err != nil {
        log.Fatal(err)
    }

    // Media files can be added to the list after the list has been added
    // to the player. The player will play these files as well.
    err = list.AddMediaFromPath("localpath/example2.mp3")
    if err != nil {
        log.Fatal(err)
    }

    // Retrieve player event manager.
    manager, err := player.EventManager()
    if err != nil {
        log.Fatal(err)
    }

    // Register the media end reached event with the event manager.
    quit := make(chan struct{})
    eventCallback := func(event vlc.Event, userData interface{}) {
        close(quit)
    }

    eventID, err := manager.Attach(vlc.MediaPlayerEndReached, eventCallback, nil)
    if err != nil {
        log.Fatal(err)
    }
    defer manager.Detach(eventID)

    <-quit

Handling multiple events example
	// Create a new player.
	player, err := vlc.NewPlayer()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		player.Stop()
		player.Release()
	}()

	// Add player media from path.
	media, err := player.LoadMediaFromPath("localpath/test.mp3")
	if err != nil {
		log.Fatal(err)
	}
	defer media.Release()

	// Start playing the media.
	err = player.Play()
	if err != nil {
		log.Fatal(err)
	}

	// Retrieve player event manager.
	manager, err := player.EventManager()
	if err != nil {
		log.Fatal(err)
	}

	// Create event handler.
	quit := make(chan struct{})
	eventCallback := func(event vlc.Event, userData interface{}) {
		switch event {
		case vlc.MediaPlayerEndReached:
			log.Println("Player end reached")
			close(quit)
		case vlc.MediaPlayerTimeChanged:
			media, err := player.Media()
			if err != nil {
				log.Println(err)
				break
			}

			stats, err := media.Stats()
			if err != nil {
				log.Println(err)
				break
			}
			log.Printf("%+v\n", stats)
		}
	}

	// Register events with the event manager.
	events := []vlc.Event{
		vlc.MediaPlayerTimeChanged,
		vlc.MediaPlayerEndReached,
	}

	var eventIDs []vlc.EventID
	for _, event := range events {
		eventID, err := manager.Attach(event, eventCallback, nil)
		if err != nil {
			log.Fatal(err)
		}

		eventIDs = append(eventIDs, eventID)
	}

	// De-register attached events.
	defer func() {
		for _, eventID := range eventIDs {
			manager.Detach(eventID)
		}
	}()

	<-quit
*/
package vlc
<commit_msg>Minor GoDoc examples improvement<commit_after>/*
Package vlc provides golang bindings for libVLC version 2.X/3.X/4.X.

Usage

Initialization
	// Initialize libVLC. Additional command line arguments can be passed in
	// to libVLC by specifying them in the Init function.
	if err := vlc.Init("--no-video", "--quiet"); err != nil {
		log.Fatal(err)
	}
	defer vlc.Release()

Player example
	// Create a new player.
	player, err := vlc.NewPlayer()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		player.Stop()
		player.Release()
	}()

	// Add a media file from path or from URL.
	// Set player media from path:
	// media, err := player.LoadMediaFromPath("localpath/test.mp4")
	// Set player media from URL:
	media, err := player.LoadMediaFromURL("http://stream-uk1.radioparadise.com/mp3-32")
	if err != nil {
		log.Fatal(err)
	}
	defer media.Release()

	// Start playing the media.
	if err = player.Play(); err != nil {
		log.Fatal(err)
	}

	// Retrieve player event manager.
	manager, err := player.EventManager()
	if err != nil {
		log.Fatal(err)
	}

	// Register the media end reached event with the event manager.
	quit := make(chan struct{})
	eventCallback := func(event vlc.Event, userData interface{}) {
		close(quit)
	}

	eventID, err := manager.Attach(vlc.MediaPlayerEndReached, eventCallback, nil)
	if err != nil {
		log.Fatal(err)
	}
	defer manager.Detach(eventID)

	<-quit

List player example
    // Create a new list player.
    player, err := vlc.NewListPlayer()
    if err != nil {
        log.Fatal(err)
    }
    defer func() {
        player.Stop()
        player.Release()
    }()

    // Create a new media list.
    list, err := vlc.NewMediaList()
    if err != nil {
        log.Fatal(err)
    }
    defer list.Release()

    err = list.AddMediaFromPath("localpath/example1.mp3")
    if err != nil {
        log.Fatal(err)
    }

    err = list.AddMediaFromURL("https://example.com/media.mp4")
    if err != nil {
        log.Fatal(err)
    }

	// Set player media list.
	if err = player.SetMediaList(list); err != nil {
		log.Fatal(err)
	}

	// Start playing the media list.
	if err = player.Play(); err != nil {
		log.Fatal(err)
	}

    // Media files can be added to the list after the list has been added
    // to the player. The player will play these files as well.
    err = list.AddMediaFromPath("localpath/example2.mp3")
    if err != nil {
        log.Fatal(err)
    }

    // Retrieve player event manager.
    manager, err := player.EventManager()
    if err != nil {
        log.Fatal(err)
    }

    // Register the media end reached event with the event manager.
    quit := make(chan struct{})
    eventCallback := func(event vlc.Event, userData interface{}) {
        close(quit)
    }

    eventID, err := manager.Attach(vlc.MediaPlayerEndReached, eventCallback, nil)
    if err != nil {
        log.Fatal(err)
    }
    defer manager.Detach(eventID)

    <-quit

Handling multiple events example
	// Create a new player.
	player, err := vlc.NewPlayer()
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		player.Stop()
		player.Release()
	}()

	// Add player media from path.
	media, err := player.LoadMediaFromPath("localpath/test.mp3")
	if err != nil {
		log.Fatal(err)
	}
	defer media.Release()

	// Start playing the media.
	if err = player.Play(); err != nil {
		log.Fatal(err)
	}

	// Retrieve player event manager.
	manager, err := player.EventManager()
	if err != nil {
		log.Fatal(err)
	}

	// Create event handler.
	quit := make(chan struct{})
	eventCallback := func(event vlc.Event, userData interface{}) {
		switch event {
		case vlc.MediaPlayerEndReached:
			log.Println("Player end reached")
			close(quit)
		case vlc.MediaPlayerTimeChanged:
			media, err := player.Media()
			if err != nil {
				log.Println(err)
				break
			}

			stats, err := media.Stats()
			if err != nil {
				log.Println(err)
				break
			}
			log.Printf("%+v\n", stats)
		}
	}

	// Register events with the event manager.
	events := []vlc.Event{
		vlc.MediaPlayerTimeChanged,
		vlc.MediaPlayerEndReached,
	}

	var eventIDs []vlc.EventID
	for _, event := range events {
		eventID, err := manager.Attach(event, eventCallback, nil)
		if err != nil {
			log.Fatal(err)
		}

		eventIDs = append(eventIDs, eventID)
	}

	// De-register attached events.
	defer func() {
		for _, eventID := range eventIDs {
			manager.Detach(eventID)
		}
	}()

	<-quit
*/
package vlc
<|endoftext|>


<commit_before>/*
Package forget provides an in-memory cache for arbitrary, binary data.

Caching

The cache identifies cached items by their key. Items to be stored are provided with their key, individually
selected expiration time (TTL), and binary data. Storing an item with the same key, overrides the TTL and the
previous one. A cached item can be retrieved or deleted with its key. As a special use case, it is possible to
store only keys, where the useful information is whether a key exists, and whether it has expired. If a new item
doesn't fit in the free space, the least recently used item is evicted (LRU).

Keyspaces

Keyspaces, when used (see NewCacheSpaces()), allow the optimization for the LRU eviction mechanism. Items that
are more expensive to produce but less frequently accessed than others, can be stored in different keyspaces.
When eviction is required, the cache tries to evict enough items from the same keyspace as the one currently
being filled. When using keyspaces, the same key can appear in different keyspaces pointing to different items.

Memory

The cache allocates all the used memory on start. To support parallel access, it splits the allocated memory
into segments. There are typically as many segments as the maximum of NumCPU() and GOMAXPROCS(). The maximum
size of a stored item is the total cache size divided by the number of segments.

The segments are split into chunks. One cached item can span over multiple chunks, but the chunks cannot be
shared between the items. This means that the cache is almost never fully utilized. The chunk size is an
initialization option, it typically should be a 'couple of times' smaller than the expected size of the 'bulk'
of the cached items.

The cache counts the size of the item keys in the used space, but there is some lookup metadata that is not
counted: ~24 bytes for each chunk and ~120 bytes for each item.

IO

The associated data of the keys can be accessed for read, seek and write through the standard Go interfaces. As
a shortcut, the data can be retrieved or set as a single byte slice, too. When using the IO interfaces, the item
data can be accessed concurrently, and reading from an item can be started before the write has finished. When
the reader reaches a point that was not yet filled by the writer, it blocks, and continues only when more data
was written, or returns with EOF once the write was finished.

While writing an item, chunks are continuously assigned to the item from the free range of allocated memory. If
there are no free chunks, the cache evicts enough of the least recently used items. The cache doesn't evict
those items that are currently being read by an unclosed reader. Similarly, when deleting an item or overwriting
one, while it has active readers associated with it, the item is only marked for delete, but the active readers
can finish reading from it.

Monitoring

The cache provides statistics about its internal state, including metrics like item count, effective and used
size, active readers and writers. When configured, it also provides change notifications. Depending on the
configured notification mask, it can send events about: cache hit/miss, evictions, allocation failures, etc.
*/
package forget
<commit_msg>fix docs<commit_after>/*
Package forget provides an in-memory cache for arbitrary, binary data.

Caching

The cache identifies items by their keys. It stores them with individual expiration time (TTL). The associated
content is stored in binary format.  Storing a new item with the same key, overrides the previous one A cached
item can be retrieved or deleted with its key. As a special use case, it is possible to store only keys, where
the useful information is whether a key exists or not. If a new item doesn't fit in the free space, the least
recently used item is evicted (LRU).

Keyspaces

Keyspaces, when used (see NewCacheSpaces()), allow some optimization of the LRU eviction mechanism. Items that
are more expensive to produce but less frequently accessed than others, can be stored in different keyspaces.
When eviction is required, the cache tries to evict enough items from the same keyspace as the one currently
being filled. When using keyspaces, the same key can appear in different keyspaces pointing to different items.

Memory

The cache allocates all the used memory on start. To support parallel access, it splits the allocated memory
into segments. There are typically as many segments as the maximum of NumCPU() and GOMAXPROCS(). The maximum
size of a stored item is the total cache size divided by the number of segments.

The segments are split into chunks. One cached item can span over multiple chunks, but the chunks cannot be
shared between the items. This means that the cache is almost never fully utilized. The chunk size is an
initialization option, it typically should be a 'couple of times' smaller than the expected size of the 'bulk'
of the cached items.

The cache counts the size of the item keys in the used space, but there is some lookup metadata that is not
counted: ~24 bytes for each chunk and ~120 bytes for each item.

IO

The associated data of the keys can be accessed for read, seek and write through the standard Go interfaces. As
a shortcut, the data can be retrieved or set as a single byte slice, too. When using the IO interfaces, the item
data can be accessed concurrently, and reading from an item can be started before the write has finished. When
the reader reaches a point that was not yet filled by the writer, it blocks, and continues only when more data
was written, or returns with EOF once the write was finished.

While writing an item, chunks are continuously assigned to the item from the free range of allocated memory. If
there are no free chunks, the cache evicts enough of the least recently used items. The cache doesn't evict
those items that are currently being read by an unclosed reader. Similarly, when deleting an item or overwriting
one, if it has active readers associated with it, the item is only marked for delete, but the active readers can
finish reading from it.

Monitoring

The cache provides statistics about its internal state, including metrics like item count, effective and used
size, active readers and writers. When configured, it also provides change notifications. Depending on the
configured notification mask, it can send events about: cache hit/miss, evictions, allocation failures, etc.
*/
package forget
<|endoftext|>


<commit_before>// Package json is a simple JSON encoder/decoder for gopher-lua.
//
// Documentation
//
// The following functions are exposed by the library:
//  decode(string): Decodes a JSON string. Returns nil and an error string if
//                  the string could not be decoded.
//  encode(value):  Encodes a value into a JSON string. Returns nil and an error
//                  string if the value could not be encoded.
//
// The following types are supported:
//
//  Lua      | JSON
//  ---------+-----
//  nil      | null
//  number   | number
//  string   | string
//  table    | object: when table is non-empty and has only string keys
//           | array:  when table is empty, or has only sequential numeric keys
//           |         starting from 1
//
// Attempting to encode any other Lua type will result in an error.
//
// Example
//
// Below is an example usage of the library:
//  import (
//      luajson "layeh.com/gopher-json"
//  )
//
//  L := lua.NewState()
//  luajson.Preload(s)
package json // import "layeh.com/gopher-json"
<commit_msg>free the import<commit_after>// Package json is a simple JSON encoder/decoder for gopher-lua.
//
// Documentation
//
// The following functions are exposed by the library:
//  decode(string): Decodes a JSON string. Returns nil and an error string if
//                  the string could not be decoded.
//  encode(value):  Encodes a value into a JSON string. Returns nil and an error
//                  string if the value could not be encoded.
//
// The following types are supported:
//
//  Lua      | JSON
//  ---------+-----
//  nil      | null
//  number   | number
//  string   | string
//  table    | object: when table is non-empty and has only string keys
//           | array:  when table is empty, or has only sequential numeric keys
//           |         starting from 1
//
// Attempting to encode any other Lua type will result in an error.
//
// Example
//
// Below is an example usage of the library:
//  import (
//      luajson "layeh.com/gopher-json"
//  )
//
//  L := lua.NewState()
//  luajson.Preload(s)
package json
<|endoftext|>


<commit_before>// Package kingpin provides command line interfaces like this:
//
//    $ chat server <ip>
//    $ chat [--debug] register [--name <name>] <nick>
//    $ chat post --channel|-c <channel> [--image <image>] [<text>]
//
// From code like this:
//
//    var (
//      chat  = kingpin.New("chat", "A command line chat application.")
//      debug = chat.Flag("debug", "enable debug mode").Default("false").Bool()
//
//      server   = chat.Command("server", "Server to connect to.")
//      serverIP = server.Arg("server", "server address").Required().IP()
//
//      register     = chat.Command("register", "Register a new user.")
//      registerName = register.Flag("name", "name of user").Required().String()
//      registerNick = register.Arg("nick", "nickname for user").Required().String()
//
//      post        = chat.Command("post", "Post a message to a channel.")
//      postChannel = post.Flag("channel", "channel to post to").Short('c').Required().String()
//      postImage   = post.Flag("image", "image to post").File()
//      postText    = post.Arg("text", "text to post").String()
//    )
//
//    func main() {
//      switch kingpin.Parse() {
//      case "register":
//        // Register user
//        println(*registerNick)
//
//      case "post":
//        // Post message
//        if *postImage != nil {
//        }
//        if *postText != "" {
//        }
//      }
//    }
package kingpin
<commit_msg>More docs.<commit_after>// Package kingpin provides command line interfaces like this:
//
//     $ chat
//     usage: chat [<flags>] <command> [<flags>] [<args> ...]
//
//     Flags:
//       --debug              enable debug mode
//       --help               Show help.
//       --server=127.0.0.1   server address
//
//     Commands:
//       help <command>
//         Show help for a command.
//
//       post [<flags>] <channel>
//         Post a message to a channel.
//
//       register <nick> <name>
//         Register a new user.
//
//     $ chat help post
//     usage: chat [<flags>] post [<flags>] <channel> [<text>]
//
//     Post a message to a channel.
//
//     Flags:
//       --image=IMAGE   image to post
//
//     Args:
//       <channel>   channel to post to
//       [<text>]    text to post
//     $ chat post --image=~/Downloads/owls.jpg pics
//
// From code like this:
//
//     package main
//
//     import "github.com/alecthomas/kingpin"
//
//     var (
//       debug    = kingpin.Flag("debug", "enable debug mode").Default("false").Bool()
//       serverIP = kingpin.Flag("server", "server address").Default("127.0.0.1").MetaVarFromDefault().IP()
//
//       register     = kingpin.Command("register", "Register a new user.")
//       registerNick = register.Arg("nick", "nickname for user").Required().String()
//       registerName = register.Arg("name", "name of user").Required().String()
//
//       post        = kingpin.Command("post", "Post a message to a channel.")
//       postImage   = post.Flag("image", "image to post").File()
//       postChannel = post.Arg("channel", "channel to post to").Required().String()
//       postText    = post.Arg("text", "text to post").String()
//     )
//
//     func main() {
//       switch kingpin.Parse() {
//       // Register user
//       case "register":
//         println(*registerNick)
//
//       // Post message
//       case "post":
//         if *postImage != nil {
//         }
//         if *postText != "" {
//         }
//       }
//     }
package kingpin
<|endoftext|>


<commit_before>/*
Command gonew generates new Go project directories.
Its produced project directories contain stub files and initialized
repositories (only git/github supported now). It can be used to create new
packages and commands.

The gonew configuration file is stored at ~/.gonewrc. It is generated the
first time you run gonew. Command line options can be used to override
some details of the configuration file.

Usage:

    gonew [options] TYPE NAME

Argument:

    TYPE
            The type of the new project ("pkg" and "cmd" supported).

    NAME
            The name of the new project/repo.

Options:

    -host=""
            Repository host if any (currently, "github" is the only
            supported host).

    -repo="git"
            Repository type (currently, "git" is the only supported
            repository type).

    -target=""
            Makefile target. The executable name in case the argument
            TYPE is "cmd", package name in case of "pkg". The default
            value based on the argument NAME.

    -help
            Print a usage message

Configuration:

The configuration for gonew is simple. The configuration can provide
default hosts, usernames, and repositories. However, it also contains the
setting of the {{name}} and {{email}} template variables.

The configuration file for gonew (~/.gonewrc) is generated on the spot if
one does not exist. So you do not need to worry about editing it for the
most part.

If you wish to write/edit the configuration file. An example configuration
file can be found at the path

    $GOROOT/src/pkg/github.com/bmatsuo/gonew/gonewrc.example
*/
package documentation
<commit_msg>Fix doc typo.<commit_after>/*
Command gonew generates new Go project directories.
Its produced project directories contain stub files and initialized
repositories (only git/github supported now). It can be used to create new
packages and commands.

The gonew configuration file is stored at ~/.gonewrc. It is generated the
first time you run gonew. Command line options can be used to override
some details of the configuration file.

Usage:

    gonew [options] TYPE NAME

Arguments:

    TYPE
            The type of the new project ("pkg" and "cmd" supported).

    NAME
            The name of the new project/repo.

Options:

    -host=""
            Repository host if any (currently, "github" is the only
            supported host).

    -repo="git"
            Repository type (currently, "git" is the only supported
            repository type).

    -target=""
            Makefile target. The executable name in case the argument
            TYPE is "cmd", package name in case of "pkg". The default
            value based on the argument NAME.

    -help
            Print a usage message

Configuration:

The configuration for gonew is simple. The configuration can provide
default hosts, usernames, and repositories. However, it also contains the
setting of the {{name}} and {{email}} template variables.

The configuration file for gonew (~/.gonewrc) is generated on the spot if
one does not exist. So you do not need to worry about editing it for the
most part.

If you wish to write/edit the configuration file. An example configuration
file can be found at the path

    $GOROOT/src/pkg/github.com/bmatsuo/gonew/gonewrc.example
*/
package documentation
<|endoftext|>


<commit_before>// Package jp offers a highly performant json selector in the style of the jq command line

package jq
<commit_msg>- fixing package docs<commit_after>// Package jp offers a highly performant json selector in the style of the jq command line
//
package jq
<|endoftext|>


<commit_before>// TODO: make `go generate` work on windows

//go:generate bash -c "ls -laR $GOPATH"

//go:generate rm -rf ./resources/provision/node_modules
//go:generate npm install ./resources/provision/ --prefix ./resources/provision --silent
// There's a handful of subdirectories that we don't need at runtime...
//go:generate rm -rf ./resources/provision/node_modules/aws-sdk/dist/
//go:generate rm -rf ./resources/provision/node_modules/aws-sdk/dist-xtools/
// Zip up the modules
//go:generate bash -c "pushd ./resources/provision; zip -qr ./node_modules.zip ./node_modules/"
//go:generate rm -rf ./resources/provision/node_modules

// Embed the custom service handlers
// TODO: Once AWS lambda supports golang as first class, move the
// NodeJS custom action helpers into golang
//go:generate go run $GOPATH/src/github.com/mjibson/esc/main.go -o ./CONSTANTS.go -private -pkg sparta ./resources
//go:generate go run ./resources/awsbinary/insertTags.go ./CONSTANTS !lambdabinary

// Create a secondary CONSTANTS_AWSBINARY.go file with empty content.  The next step will insert the
// build tags at the head of each file so that they are mutually exclusive, similar to the
// lambdabinaryshims.go file
//go:generate go run $GOPATH/src/github.com/mjibson/esc/main.go -o ./CONSTANTS_AWSBINARY.go -private -pkg sparta ./resources/awsbinary/README.md
//go:generate go run ./resources/awsbinary/insertTags.go ./CONSTANTS_AWSBINARY lambdabinary

// cleanup
//go:generate rm -f ./resources/provision/node_modules.zip

/*
Package sparta transforms a set of golang functions into an Amazon Lambda deployable unit.

The deployable archive includes

	 	1. NodeJS proxy logic
	 	2. A golang binary
	 	3. Dynamically generated CloudFormation template that supports create/update & delete operations.
	 	4. If specified, CloudFormation custom resources to automatically configure S3/SNS push registration
		5. If specified, API Gateway provisioning logic via custom resources to make the golang functions publicly accessible.

See the Main() docs for more information and examples
*/
package sparta
<commit_msg>Ensure `esc` is fetched<commit_after>// TODO: make `go generate` work on windows

//go:generate go get github.com/mjibson/esc

//go:generate rm -rf ./resources/provision/node_modules
//go:generate npm install ./resources/provision/ --prefix ./resources/provision --silent
// There's a handful of subdirectories that we don't need at runtime...
//go:generate rm -rf ./resources/provision/node_modules/aws-sdk/dist/
//go:generate rm -rf ./resources/provision/node_modules/aws-sdk/dist-xtools/
// Zip up the modules
//go:generate bash -c "pushd ./resources/provision; zip -qr ./node_modules.zip ./node_modules/"
//go:generate rm -rf ./resources/provision/node_modules

// Embed the custom service handlers
// TODO: Once AWS lambda supports golang as first class, move the
// NodeJS custom action helpers into golang
//go:generate go run $GOPATH/src/github.com/mjibson/esc/main.go -o ./CONSTANTS.go -private -pkg sparta ./resources
//go:generate go run ./resources/awsbinary/insertTags.go ./CONSTANTS !lambdabinary

// Create a secondary CONSTANTS_AWSBINARY.go file with empty content.  The next step will insert the
// build tags at the head of each file so that they are mutually exclusive, similar to the
// lambdabinaryshims.go file
//go:generate go run $GOPATH/src/github.com/mjibson/esc/main.go -o ./CONSTANTS_AWSBINARY.go -private -pkg sparta ./resources/awsbinary/README.md
//go:generate go run ./resources/awsbinary/insertTags.go ./CONSTANTS_AWSBINARY lambdabinary

// cleanup
//go:generate rm -f ./resources/provision/node_modules.zip

/*
Package sparta transforms a set of golang functions into an Amazon Lambda deployable unit.

The deployable archive includes

	 	1. NodeJS proxy logic
	 	2. A golang binary
	 	3. Dynamically generated CloudFormation template that supports create/update & delete operations.
	 	4. If specified, CloudFormation custom resources to automatically configure S3/SNS push registration
		5. If specified, API Gateway provisioning logic via custom resources to make the golang functions publicly accessible.

See the Main() docs for more information and examples
*/
package sparta
<|endoftext|>


<commit_before>/*
Package core is the perfect foundation for any web application.
It allows you to connect all and only the components you need in a flexible and efficient way.

A handlers (or *middlewares*) stack is used to pass data in line, from the first to the last handler.
So you can perform actions downstream, then filter and manipulate the response upstream.

No handlers are bundled in this package.

Getting started

"Hello, World!" example with request logging:

	package main

	import (
		"fmt"
		"log"
		"time"

		"github.com/volatile/core"
	)

	func main() {
		// Log
		core.Use(func(c *core.Context) {
			start := time.Now()
			c.Next()
			log.Printf(" %s  %s  %s", c.Request.Method, c.Request.URL, time.Since(start))
		})

		// Response
		core.Use(func(c *core.Context) {
			fmt.Fprint(c.ResponseWriter, "Hello, World!")
		})

		core.Run()
	}

After running, the application is reachable at "http://localhost:8080/".

Official handlers

In order of usability in you app:

- Log — Requests logging — https://github.com/volatile/log
- Compress — Responses compressing — https://github.com/volatile/compress
- CORS — Cross-Origin Resource Sharing support — https://github.com/volatile/cors
- Others are coming…

Official helpers

Helpers are just syntactic sugars to ease repetitive code and improve readability of you app.

- Route — Flexible routing helper — https://github.com/volatile/route
- Response — Readable response helper — https://github.com/volatile/response
- Others are coming…

Context

All handlers are functions that receive a context: func(*core.Context).
A Volatile context encapsulates the well known ["*http.Request"](http://golang.org/pkg/net/http/#Request) and ["http.ResponseWriter"](http://golang.org/pkg/net/http/#ResponseWriter), from the standard ["net/http"](http://golang.org/pkg/net/http/) package.

Next

Simply use the context's "Next()" method to go to the next handler.

	core.Use(func(c *core.Context) {
		c.Next()
	})

Pass data

To transmit data from a handler to another, the *coreContext has a Data field, which is a map[string]interface{}.

	// Set data
	core.Use(func(c *core.Context) {
		c.Data["id"] = 123
	})

	// Read data
	core.Use(func(c *core.Context) {
		println(c.Data["id"].(int))
	})

Response writer binding

If some of your handlers need to transform the request before sending it, they can't just use the same ResponseWriter all the stack long.
To do so, the Core provides a ResponseWriterBinder structure that has the same signature as an http.ResponseWriter, but that redirects the response upstream, to an io.Writer that will write the original http.ResponseWriter.

In other words, the ResponseWriterBinder has an output (the ResponseWriter used before setting the binder) and an input (an overwritten ResponseWriter used by the next handlers).
The Compress package (https://github.com/volatile/compress/blob/master/handler.go) is a good example.

If you need to do something just before writing the response body (like setting headers, as you can't do that after), use the BeforeWrite field.

	core.Use(func(c *core.Context) {
		// 1. Set the output
		gzw := gzip.NewWriter(c.ResponseWriter)
		defer gzw.Close()

		// 2. Set the binder
		rwb = ResponseWriterBinder{
			Writer:         gzw, // The binder output.
			ResponseWriter: c.ResponseWriter, // Keep the same Header() and WriteHeader() methods. Only the Write method change internally.
			BeforeWrite:    func(b []byte) {
				// Do something with b before writing the response body.
			},
		}

		// 3. Set the input
		c.ResponseWriter = rwb
	})

	core.Use(func(c *core.Context) {
		// The overwritten context's ResponseWriter is used in a transparent way.
		c.ResponseWriter.Write([]byte("Hello, World!"))
	})

Things to know

- When a handler writes the body of a response, it brakes the handlers chain so a c.Next() call has no effect.
- Remember that response headers must be set before the body is written. After that, trying to set a header has no effect.

Custom port

To let the server listen on a custom port, use the "-port [port]" parameter on launch.

Environments

Some handlers can have different behaviors depending on the environment the server is running.
By default, the Core suppose the server is launched in a development environment.
When running your application in a production environment, use the "-production" parameter on launch.

In your code, you have access to the core.Production flag to distinguish the environment.
*/
package core
<commit_msg>Fix GoDoc typo<commit_after>/*
Package core is the perfect foundation for any web application.
It allows you to connect all and only the components you need in a flexible and efficient way.

A handlers (or *middlewares*) stack is used to pass data in line, from the first to the last handler.
So you can perform actions downstream, then filter and manipulate the response upstream.

No handlers are bundled in this package.

Getting started

"Hello, World!" example with request logging:

	package main

	import (
		"fmt"
		"log"
		"time"

		"github.com/volatile/core"
	)

	func main() {
		// Log
		core.Use(func(c *core.Context) {
			start := time.Now()
			c.Next()
			log.Printf(" %s  %s  %s", c.Request.Method, c.Request.URL, time.Since(start))
		})

		// Response
		core.Use(func(c *core.Context) {
			fmt.Fprint(c.ResponseWriter, "Hello, World!")
		})

		core.Run()
	}

After running, the application is reachable at "http://localhost:8080/".

Official handlers

In order of usability in you app:

Log — Requests logging — https://github.com/volatile/log

Compress — Responses compressing — https://github.com/volatile/compress

CORS — Cross-Origin Resource Sharing support — https://github.com/volatile/cors

Others are coming…

Official helpers

Helpers are just syntactic sugars to ease repetitive code and improve readability of you app.

Route — Flexible routing helper — https://github.com/volatile/route

Response — Readable response helper — https://github.com/volatile/response

Others are coming…

Context

All handlers are functions that receive a context: func(*core.Context).
A Volatile context encapsulates the well known ["*http.Request"](http://golang.org/pkg/net/http/#Request) and ["http.ResponseWriter"](http://golang.org/pkg/net/http/#ResponseWriter), from the standard ["net/http"](http://golang.org/pkg/net/http/) package.

Next

Simply use the context's "Next()" method to go to the next handler.

	core.Use(func(c *core.Context) {
		c.Next()
	})

Pass data

To transmit data from a handler to another, the *coreContext has a Data field, which is a map[string]interface{}.

	// Set data
	core.Use(func(c *core.Context) {
		c.Data["id"] = 123
	})

	// Read data
	core.Use(func(c *core.Context) {
		println(c.Data["id"].(int))
	})

Response writer binding

If some of your handlers need to transform the request before sending it, they can't just use the same ResponseWriter all the stack long.
To do so, the Core provides a ResponseWriterBinder structure that has the same signature as an http.ResponseWriter, but that redirects the response upstream, to an io.Writer that will write the original http.ResponseWriter.

In other words, the ResponseWriterBinder has an output (the ResponseWriter used before setting the binder) and an input (an overwritten ResponseWriter used by the next handlers).
The Compress package (https://github.com/volatile/compress/blob/master/handler.go) is a good example.

If you need to do something just before writing the response body (like setting headers, as you can't do that after), use the BeforeWrite field.

	core.Use(func(c *core.Context) {
		// 1. Set the output
		gzw := gzip.NewWriter(c.ResponseWriter)
		defer gzw.Close()

		// 2. Set the binder
		rwb = ResponseWriterBinder{
			Writer:         gzw, // The binder output.
			ResponseWriter: c.ResponseWriter, // Keep the same Header() and WriteHeader() methods. Only the Write method change internally.
			BeforeWrite:    func(b []byte) {
				// Do something with b before writing the response body.
			},
		}

		// 3. Set the input
		c.ResponseWriter = rwb
	})

	core.Use(func(c *core.Context) {
		// The overwritten context's ResponseWriter is used in a transparent way.
		c.ResponseWriter.Write([]byte("Hello, World!"))
	})

Things to know

When a handler writes the body of a response, it brakes the handlers chain so a c.Next() call has no effect.

Remember that response headers must be set before the body is written. After that, trying to set a header has no effect.

Custom port

To let the server listen on a custom port, use the "-port [port]" parameter on launch.

Environments

Some handlers can have different behaviors depending on the environment the server is running.
By default, the Core suppose the server is launched in a development environment.
When running your application in a production environment, use the "-production" parameter on launch.

In your code, you have access to the core.Production flag to distinguish the environment.
*/
package core
<|endoftext|>


<commit_before>/*
Package vnc provides VNC client implementation.

This package implements The Remote Framebuffer Protocol as documented in
[RFC 6143](http://tools.ietf.org/html/rfc6143).

A basic VNC client can be created like this:

    // Establish TCP connection to VNC server.
    nc, err := net.Dial("tcp", "127.0.0.1:5900")
    if err != nil {
      log.Fatalf("Error connecting to VNC host. %v", err)
    }

    // Negotiate connection with the server.
    vcc := NewClientConfig("some_password")
    vc, err := Connect(context.Background(), nc, vcc)
    if err != nil {
      log.Fatalf("Error negotiating connection to VNC host. %v", err)
    }

    // Periodically request framebuffer updates.
    go func(){
      w, h := vc.FramebufferWidth(), vc.FramebufferHeight()
      for {
        if err := v.conn.FramebufferUpdateRequest(vnc.RFBTrue, 0, 0, w, h); err != nil {
          log.Printf("error requesting framebuffer update: %v", err)
        }
        time.Sleep(1*time.Second)
      }
    }()

    // Listen and handle server messages.
    go vc.ListenAndHandle()

    // Process messages coming in on the ServerMessage channel.
    for {
      msg := <-vcc.ServerMessageCh
      switch msg.Type() {
      case FramebufferUpdateMsg:
        log.Println("Received FramebufferUpdate message.")
      default:
        log.Printf("Received message type:%v msg:%v\n", msg.Type(), msg)
      }
    }

This example will connect to a VNC server running on the localhost. It will
periodically request updates from the server, and listen for and handle
incoming FramebufferUpdate messages coming from the server.
*/
package vnc
<commit_msg>Fixed #3. Incorporated user feedback to fix the example code.<commit_after>/*
Package vnc provides VNC client implementation.

This package implements The Remote Framebuffer Protocol as documented in
[RFC 6143](http://tools.ietf.org/html/rfc6143).

A basic VNC client can be created like this. Replace the IP on the net.Dial line
with something more appropriate for your setup.

  package main

  import (
    "context"
    "log"
    "net"
    "time"

    "github.com/kward/go-vnc"
    "github.com/kward/go-vnc/messages"
    "github.com/kward/go-vnc/rfbflags"
  )

  func main() {
    // Establish TCP connection to VNC server.
    nc, err := net.Dial("tcp", "127.0.0.1:5900")
    if err != nil {
      log.Fatalf("Error connecting to VNC host. %v", err)
    }

    // Negotiate connection with the server.
    vcc := vnc.NewClientConfig("some_password")
    vc, err := vnc.Connect(context.Background(), nc, vcc)
    if err != nil {
      log.Fatalf("Error negotiating connection to VNC host. %v", err)
    }

    // Periodically request framebuffer updates.
    go func() {
      w, h := vc.FramebufferWidth(), vc.FramebufferHeight()
      for {
        if err := vc.FramebufferUpdateRequest(rfbflags.RFBTrue, 0, 0, w, h); err != nil {
          log.Printf("error requesting framebuffer update: %v", err)
        }
        time.Sleep(1 * time.Second)
      }
    }()

    // Listen and handle server messages.
    go vc.ListenAndHandle()

    // Process messages coming in on the ServerMessage channel.
    for {
      msg := <-vcc.ServerMessageCh
      switch msg.Type() {
      case messages.FramebufferUpdate:
        log.Println("Received FramebufferUpdate message.")
      default:
        log.Printf("Received message type:%v msg:%v\n", msg.Type(), msg)
      }
    }
  }

This example will connect to a VNC server running on the localhost. It will
periodically request updates from the server, and listen for and handle
incoming FramebufferUpdate messages coming from the server.
*/
package vnc
<|endoftext|>


<commit_before>/*
Package merkleTree is a generic Merkle Tree implementation, for provably publishing lots
of data under one succinct tree root.

Install:

   go get github.com/keybase/go-merkle-tree

Design:

This package outputs a MerkleTree with two types of nodes: interior index
nodes, or iNodes, and exterior data nodes, of Leaf nodes. The inodes
consist of tables that map prefixes to child pointers.  The leafs map a full
hash to a "value".

This is best demonstrated with a simple example. Let's say you are storing
the key-value pair (`0123456789abcdef`, {"name" : "max"}) in the Merkle tree.
Let's say that the shape of the tree is to have 256 children per inode.
Then this key-value pair might be stored under the path

	at root     node: 01 → aabbccdd
	at aabbccdd node: 23 → eeff5588
	at eeff5588 node: 34 → 99331122
	at 99331122 node: 0123456789abcdef → {"name" : "max" }

Meaning at the root node, we take the first 256-bits of the needed
key to get a prefix `01`, and look that up in the node's pointer table
to get a child pointer, which is `aabbccdd`.  This is a hash of an
iNode, which we can fetch from storage, verify it matches the hash,
and then recursively apply the same algorithm to find the next
step in the path. The leaf node has a sparse table of long-hashes
(which are the keys) that map to the values actually stored in the
tree.

Implementation:

All nodes are encoded with msgpack before being hashed or written to
store. See `types.go` for the exactly layout of the msgpack objects.

Usage:

To construct a new Tree from scratch, you need to specify three parameters:

	- A Config, which specifies the shape of the Tree. That is,
	  how many children per interior Node, and how big leaves
	  can get before a new level of the tree is introduced. Also,
	  the hash function to use for hashing nodes into pointers.

	- A StorageEngine, which determines how to load and store tree Nodes
	  from storage, and how to load and store the root hash of the Merkle tree.

	- An array of KeyValuePairs, the things actually stored in the Merkle tree.

Example:

	type sha512Hasher struct{}

	func (s sha512Hasher) Hash(b []byte) Hash {
		tmp := sha512.Sum512(b)
		return Hash(tmp[:])
	}

	func main() {
		cfg := NewConfig(sha512hasher{}, 256, 512);

		eng := someFunctionToMakeAStorageEngine()
		tree := NewTree(eng, cfg)

		var objs []KeyValuePair
		objs := someFunctionToGetData()
		tree.Build(objs)
	}

*/
package merkleTree
<commit_msg>example now (hidden) in example_test.go<commit_after>/*
Package merkleTree is a generic Merkle Tree implementation, for provably publishing lots
of data under one succinct tree root.

Install:

   go get github.com/keybase/go-merkle-tree

Design:

This package outputs a MerkleTree with two types of nodes: interior index
nodes, or iNodes, and exterior data nodes, of Leaf nodes. The inodes
consist of tables that map prefixes to child pointers.  The leafs map a full
hash to a "value".

This is best demonstrated with a simple example. Let's say you are storing
the key-value pair (`0123456789abcdef`, {"name" : "max"}) in the Merkle tree.
Let's say that the shape of the tree is to have 256 children per inode.
Then this key-value pair might be stored under the path

	at root     node: 01 → aabbccdd
	at aabbccdd node: 23 → eeff5588
	at eeff5588 node: 34 → 99331122
	at 99331122 node: 0123456789abcdef → {"name" : "max" }

Meaning at the root node, we take the first 256-bits of the needed
key to get a prefix `01`, and look that up in the node's pointer table
to get a child pointer, which is `aabbccdd`.  This is a hash of an
iNode, which we can fetch from storage, verify it matches the hash,
and then recursively apply the same algorithm to find the next
step in the path. The leaf node has a sparse table of long-hashes
(which are the keys) that map to the values actually stored in the
tree.

Implementation:

All nodes are encoded with msgpack before being hashed or written to
store. See `types.go` for the exactly layout of the msgpack objects.

Usage:

To construct a new Tree from scratch, you need to specify three parameters:

	- A Config, which specifies the shape of the Tree. That is,
	  how many children per interior Node, and how big leaves
	  can get before a new level of the tree is introduced. Also,
	  the hash function to use for hashing nodes into pointers.

	- A StorageEngine, which determines how to load and store tree Nodes
	  from storage, and how to load and store the root hash of the Merkle tree.

	- An array of KeyValuePairs, the things actually stored in the Merkle tree.

*/
package merkleTree
<|endoftext|>


<commit_before>/*
Package stick is a go-language port of the Twig templating engine.

Twig is a powerful templating language that promotes separation of logic
from the view.

Stick executes Twig templates using an instance of Env. An Env contains all
the configured Functions, Filters, and Tests as well as a Loader to load
named templates from any source.

Obligatory "Hello, World!" example:

	env := stick.NewEnv(nil); // A nil loader means stick will simply execute
	                          // the string passed into env.Execute.
	// Templates receive a map of string to any value.
	p := map[string]stick.Value{"name": "World"}
	err := env.Execute("Hello, {{ name }}!", os.Stdout, )
	if err != nil { panic(err) }

In the previous example, notice that we passed in os.Stdout. Any io.Writer can be used.

Another example, using a FilesystemLoader and responding to an HTTP request:

	import "net/http"

	// ...

	fsRoot := os.Getwd() // Templates are loaded relative to this directory.
	env := stick.NewEnv(stick.NewFilesystemLoader(fsRoot))
	http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
		env.Execute("bar.html.twig", w, nil) // Loads "bar.html.twig" relative to fsRoot.
	})
	http.ListenAndServe(":80", nil)

Types and values

Any user value in Stick is represented by a stick.Value. There are three main types
in Stick when it comes to built-in operations: strings, numbers, and booleans. Of note,
numbers are represented by float64 as this matches regular Twig behavior most closely.

Stick makes no restriction on what is stored in a stick.Value, but some built-in
operators will try to coerce a value into a boolean, string, or number depending
on the operation.

Additionally, custom types that implement specific interfaces can be coerced. Stick
defines three interfaces: Stringer, Number, and Boolean. Each interface defines a single
method that should convert a custom type into the specified type.

	type myType struct {
		// ...
	}

	func (t *myType) String() string {
		return fmt.Sprintf("%v", t.someField)
	}

	func (t *myType) Number() float64 {
		return t.someFloatField
	}

	func (t *myType) Boolean() bool {
		return t.someValue != nil
	}

On a final note, there exists three functions to coerce any type into a string,
number, or boolean, respectively.

	// Coerce any value to a string
	v := stick.CoerceString(anything)

	// Coerce any value to a float64
	f := stick.CoerceNumber(anything)

	// Coerce any vale to a boolean
	b := stick.CoerceBool(anything)

Functions, filters, and tests

It is possible to define custom Filters, Functions, and boolean Tests available to
your Stick templates. Each user-defined type is simply a function with a specific
signature.

A Func represents a user-defined function.

	type Func func(e *Env, args ...Value) Value

Functions can be called anywhere expressions are allowed. Functions may take any number
of arguments.

	{% if form_valid(form) %}

A Filter is a user-defined filter.

	type Filter func(e *Env, val Value, args ...Value) Value

Filters receive a value and modify it in some way. Example of using a filter:

	{{ post|raw }}

Filters also accept zero or more arguments beyond the value to be filtered:

	{{ balance|number_format(2) }}

A Test represents a user-defined boolean test.

	type Test func(e *Env, val Value, args ...Value) bool

Tests are used to make some comparisons more expressive, for example:

	{% if users is empty %}

Tests also accept zero to any number of arguments, and Test names can contain
up to one space. Here, "divisible by" is an example of a two-word test that takes
a parameter:

	{% if loop.index is divisible by(3) %}

User-defined types are added to an Env after it is created. For example:

	env := stick.NewEnv(nil)
	env.Functions["form_valid"] = func(e *stick.Env, args ...stick.Value) stick.Value {
		// Do something useful..
		return true
	}
	env.Filters["number_format"] = func(e *stick.Env, val stick.Value, args ...stick.Value) stick.Value {
		v := stick.CoerceNumber(val)
		// Do some formatting.
		return fmt.Sprintf("%.2d", v)
	}
	env.Tests["empty"] = func(e *stick.Env, val stick.Value, args ...stick.Value) bool {
		// Probably not that useful.
		return stick.CoerceBool(val) == false
	}

For additional information on Twig, check http://twig.sensiolabs.org/
*/
package stick

// BUG(ts): Missing documentation on operators, tests, functions, filters, and much more.
<commit_msg>Doc formatting fixes.<commit_after>/*
Package stick is a go-language port of the Twig templating engine.

Twig is a powerful templating language that promotes separation of logic
from the view.

Stick executes Twig templates using an instance of Env. An Env contains all
the configured Functions, Filters, and Tests as well as a Loader to load
named templates from any source.

Obligatory "Hello, World!" example:

	env := stick.NewEnv(nil); // A nil loader means stick will simply execute
	                          // the string passed into env.Execute.

	// Templates receive a map of string to any value.
	p := map[string]stick.Value{"name": "World"}

	// Substitute os.Stdout with any io.Writer.
	env.Execute("Hello, {{ name }}!", os.Stdout, p)

Another example, using a FilesystemLoader and responding to an HTTP request:

	import "net/http"

	// ...

	fsRoot := os.Getwd() // Templates are loaded relative to this directory.
	env := stick.NewEnv(stick.NewFilesystemLoader(fsRoot))
	http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
		env.Execute("bar.html.twig", w, nil) // Loads "bar.html.twig" relative to fsRoot.
	})
	http.ListenAndServe(":80", nil)

Types and values

Any user value in Stick is represented by a stick.Value. There are three main types
in Stick when it comes to built-in operations: strings, numbers, and booleans. Of note,
numbers are represented by float64 as this matches regular Twig behavior most closely.

Stick makes no restriction on what is stored in a stick.Value, but some built-in
operators will try to coerce a value into a boolean, string, or number depending
on the operation.

Additionally, custom types that implement specific interfaces can be coerced. Stick
defines three interfaces: Stringer, Number, and Boolean. Each interface defines a single
method that should convert a custom type into the specified type.

	type myType struct {
		// ...
	}

	func (t *myType) String() string {
		return fmt.Sprintf("%v", t.someField)
	}

	func (t *myType) Number() float64 {
		return t.someFloatField
	}

	func (t *myType) Boolean() bool {
		return t.someValue != nil
	}

On a final note, there exists three functions to coerce any type into a string,
number, or boolean, respectively.

	// Coerce any value to a string
	v := stick.CoerceString(anything)

	// Coerce any value to a float64
	f := stick.CoerceNumber(anything)

	// Coerce any vale to a boolean
	b := stick.CoerceBool(anything)

User defined helpers

It is possible to define custom Filters, Functions, and boolean Tests available to
your Stick templates. Each user-defined type is simply a function with a specific
signature.

A Func represents a user-defined function.

	type Func func(e *Env, args ...Value) Value

Functions can be called anywhere expressions are allowed. Functions may take any number
of arguments.

	{% if form_valid(form) %}

A Filter is a user-defined filter.

	type Filter func(e *Env, val Value, args ...Value) Value

Filters receive a value and modify it in some way. Example of using a filter:

	{{ post|raw }}

Filters also accept zero or more arguments beyond the value to be filtered:

	{{ balance|number_format(2) }}

A Test represents a user-defined boolean test.

	type Test func(e *Env, val Value, args ...Value) bool

Tests are used to make some comparisons more expressive, for example:

	{% if users is empty %}

Tests also accept zero to any number of arguments, and Test names can contain
up to one space. Here, "divisible by" is an example of a two-word test that takes
a parameter:

	{% if loop.index is divisible by(3) %}

User-defined types are added to an Env after it is created. For example:

	env := stick.NewEnv(nil)
	env.Functions["form_valid"] = func(e *stick.Env, args ...stick.Value) stick.Value {
		// Do something useful..
		return true
	}
	env.Filters["number_format"] = func(e *stick.Env, val stick.Value, args ...stick.Value) stick.Value {
		v := stick.CoerceNumber(val)
		// Do some formatting.
		return fmt.Sprintf("%.2d", v)
	}
	env.Tests["empty"] = func(e *stick.Env, val stick.Value, args ...stick.Value) bool {
		// Probably not that useful.
		return stick.CoerceBool(val) == false
	}

For additional information on Twig, check http://twig.sensiolabs.org/
*/
package stick

// BUG(ts): Missing documentation on operators, tests, functions, filters, and much more.
<|endoftext|>


<commit_before>// Copyright 2014 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

// orm提供了一个相对统一的数据库操作，目前内置了对部分数据库的支持，
// 用户可以通过实现自定义来实现对特定数据库的支持。
//
// 支持的数据库：
//  1. sqlite3: github.com/mattn/go-sqlite3
//  2. mysql:   github.com/go-sql-driver/mysql
//  3. postgres:github.com/lib/pq
// 其它数据库，用户可以通过实现orm/core.Dialect接口，
// 然后调用orm/dialect.Register()注册来实现支持。
//
// 初始化：
//
// 默认情况下，orm包并不会加载任何数据库的实例。所以想要用哪个数据库，需要手动初始化：
//  import (
//      github.com/issue9/orm/dialect  // 加载dialect管理包
//      _ github.com/mattn/go-sqlite3  // 加载数据库驱动
//  )
//
//  // 向orm/dialect包注册dialect
//  dialect.Register("sqlite3", &dialect.Sqlite3{})
//
//  // 初始化一个Engine，表前缀为prefix_
//  db1 := orm.New("sqlite3", "./db1", "db1", "prefix_")
//
//  // 另一个Engine
//  db2 := orm.New("sqlite3", "./db2", "db2", "db2_")
//
// Model:
//  type User struct {
//      Id          int64      `orm:"name(id);ai;"`
//      FirstName   string     `orm:"name(first_name);index(index_name)"`
//      LastName    string     `orm:"name(first_name);index(index_name)"`
//  }
//
//  // 通过orm/core.Metaer接口，指定表的额外数据。若不需要，可不用实现该接口
//  func(u *User) Meta() string {
//      return "name(user);engine(innodb);charset(utf-8)"
//  }
// 通过struct tag可以直接将一个结构体定义为一个数据表结构，
// struct tag的语法结构，如上面代码所示，目前支持以下的struct tag：
//
//  name(fieldName): 将当前的字段映射到数据表中的fieldName字段。
//
//  len(l1, l2): 指定字段的长度，比如mysql中的int(5),varchar(255),double(1,2),
//  仅部分数据库支持，比如sqlite3不支持该属性。
//
//  nullable: 相当于定义表结构时的NULL，建议尽量少用该属性，若非用不可的话，
//  与之对应的Go属性必须声明为NullString之类的结构。
//
//  pk: 主键，支持联合主键，给多个字段加上pk的struct tag即可。
//
//  ai: 自增，若指定了自增列，则将自动取消其它的pk设置。无法指定起始值和步长。
//
//  unique(index_name): 唯一索引，支持联合索引，index_name为约束名，
//  会将index_name为一样的字段定义为一个联合索引。
//
//  index(index_name): 普通的关键字索引，同unique一样会将名称相同的索引定义为一个联合索引。
//
//  default(value): 指定默认值。相当于定义表结构时的DEFAULT。
//
//  fk(fk_name,refTable,refColName,updateRule,deleteRule):
//  定义物理外键，最少需要指定fk_name,refTabl,refColName三个值。
//  分别对应约束名，引用的表和引用的字段，updateRule,deleteRule，
//  在不指定的情况下，使用数据库的默认值。
//
// 关于core.Metaer接口。
//
// 在go不能将struct tag作用于结构体，所以为了指定一些表级别的属性，
// 只能通过接口的形式，然后在接口方法中返回一段类似于struct tag的字符串，
// 以达到相同的目的。
// 当然在go中receive区分值类型和指针类型，所以指定接口时，需要注意这个情况。
//
//
// 如何使用：
//
// Create:
// 可以通过Engine.Create()或是Tx.Create()创建一张表。
//  // 创建或是更新表
//  e.Create(&User{})
//  // 创建或是更新多个表
//  e.Create([]*User{&User{},&Email{}})
//
// Update:
//  // 将id为1的记录的FirstName更改为abc
//  e.Update(&User{Id:1,FirstName:"abc"})
//  e.Where("id=?", 1).Add("FirstName", "abc").Update()
//  e.Where("id=?").Columns("FirstName").Update("abc", 1)
//
// Delete:
//  // 删除id为1的记录
//  e.Delete(&User{Id:1})
//  e.Where("id=?").Delte(1)
//  e.Where("id=?", 1).Delete()
//
// Insert:
//  // 一次性插入一条数据
//  e.Insert(&User{Id:1,FirstName:"abc"})
//  // 一次性插入多条数据
//  e.Insert([]*User{&User{Id:1,FirstName:"abc"},&User{Id:1,FirstName:"abc"}})
//
// Select:
//  // 导出id=1的数据
//  m, err := e.Where("id=?", 1).FetchMap()
//  // 导出id<5的所有数据
//  m, err := e.Where("id<?", 1).FetchMaps(5)
//
//
// 事务：
//
// 默认的Engine是不支持事务的，若需要事务支持，则需要调用Engine.Begin()
// 返回事务对象Tx，当然并不是所有的数据库都支持事务操作的。
// Tx拥有与Engine相似的接口。
package orm

// 版本号
const Version = "0.2.2.141230"
<commit_msg>更新文档说明<commit_after>// Copyright 2014 by caixw, All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.

// orm提供了一个相对统一的数据库操作，目前内置了对部分数据库的支持，
// 用户可以通过实现自定义来实现对特定数据库的支持。
//
// 支持的数据库：
//  1. sqlite3: github.com/mattn/go-sqlite3
//  2. mysql:   github.com/go-sql-driver/mysql
//  3. postgres:github.com/lib/pq
// 其它数据库，用户可以通过实现orm/core.Dialect接口，
// 然后调用orm/dialect.Register()注册来实现支持。
//
// 初始化：
//
// 默认情况下，orm包并不会加载任何数据库的实例。所以想要用哪个数据库，需要手动初始化：
//  import (
//      github.com/issue9/orm/dialect  // 加载dialect管理包
//      _ github.com/mattn/go-sqlite3  // 加载数据库驱动
//  )
//
//  // 向orm/dialect包注册dialect
//  dialect.Register("sqlite3", &dialect.Sqlite3{})
//
//  // 初始化一个Engine，表前缀为prefix_
//  db1 := orm.New("sqlite3", "./db1", "db1", "prefix_")
//
//  // 另一个Engine
//  db2 := orm.New("sqlite3", "./db2", "db2", "db2_")
//
// Model:
//  type User struct {
//      Id          int64      `orm:"name(id);ai;"`
//      FirstName   string     `orm:"name(first_name);index(index_name)"`
//      LastName    string     `orm:"name(first_name);index(index_name)"`
//  }
//
//  // 通过orm/core.Metaer接口，指定表的额外数据。若不需要，可不用实现该接口
//  func(u *User) Meta() string {
//      return "name(user);engine(innodb);charset(utf-8)"
//  }
// 通过struct tag可以直接将一个结构体定义为一个数据表结构，
// struct tag的语法结构，如上面代码所示，目前支持以下的struct tag：
//
//  name(fieldName): 将当前的字段映射到数据表中的fieldName字段。
//
//  len(l1, l2): 指定字段的长度，比如mysql中的int(5),varchar(255),double(1,2),
//  仅部分数据库支持，比如sqlite3不支持该属性。
//
//  nullable: 相当于定义表结构时的NULL，建议尽量少用该属性，若非用不可的话，
//  与之对应的Go属性必须声明为NullString之类的结构。
//
//  pk: 主键，支持联合主键，给多个字段加上pk的struct tag即可。
//
//  ai: 自增，若指定了自增列，则将自动取消其它的pk设置。无法指定起始值和步长。
//
//  unique(index_name): 唯一索引，支持联合索引，index_name为约束名，
//  会将index_name为一样的字段定义为一个联合索引。
//
//  index(index_name): 普通的关键字索引，同unique一样会将名称相同的索引定义为一个联合索引。
//
//  default(value): 指定默认值。相当于定义表结构时的DEFAULT。
//
//  fk(fk_name,refTable,refColName,updateRule,deleteRule):
//  定义物理外键，最少需要指定fk_name,refTabl,refColName三个值。
//  分别对应约束名，引用的表和引用的字段，updateRule,deleteRule，
//  在不指定的情况下，使用数据库的默认值。
//
// 关于core.Metaer接口。
//
// 在go不能将struct tag作用于结构体，所以为了指定一些表级别的属性，
// 只能通过接口的形式，在接口方法中返回一段类似于struct tag的字符串，
// 以达到相同的目的。
//
// 在core.Metaer中除了可以指定name(table_name)和check(name,expr)两个属性之外，
// 还可指定一些自定义的属性，这些属性都将会被保存到Model.Meta中。
//
// 当然在go中receive区分值类型和指针类型，所以指定接口时，需要注意这个情况。
//
//
// 如何使用：
//
// Create:
// 可以通过Engine.Create()或是Tx.Create()创建一张表。
//  // 创建或是更新表
//  e.Create(&User{})
//  // 创建或是更新多个表
//  e.Create([]*User{&User{},&Email{}})
//
// Update:
//  // 将id为1的记录的FirstName更改为abc
//  e.Update(&User{Id:1,FirstName:"abc"})
//  e.Where("id=?", 1).Add("FirstName", "abc").Update()
//  e.Where("id=?").Columns("FirstName").Update("abc", 1)
//
// Delete:
//  // 删除id为1的记录
//  e.Delete(&User{Id:1})
//  e.Where("id=?").Delte(1)
//  e.Where("id=?", 1).Delete()
//
// Insert:
//  // 一次性插入一条数据
//  e.Insert(&User{Id:1,FirstName:"abc"})
//  // 一次性插入多条数据
//  e.Insert([]*User{&User{Id:1,FirstName:"abc"},&User{Id:1,FirstName:"abc"}})
//
// Select:
//  // 导出id=1的数据
//  m, err := e.Where("id=?", 1).FetchMap()
//  // 导出id<5的所有数据
//  m, err := e.Where("id<?", 1).FetchMaps(5)
//
//
// 事务：
//
// 默认的Engine是不支持事务的，若需要事务支持，则需要调用Engine.Begin()
// 返回事务对象Tx，当然并不是所有的数据库都支持事务操作的。
// Tx拥有与Engine相似的接口。
package orm

// 版本号
const Version = "0.2.2.141230"
<|endoftext|>


<commit_before>/*
Package dbus implements bindings to the DBus message bus system, as well as the
corresponding encoding format.

For the message bus API, you first need to connect to a bus (usually the Session
or System bus). Then, you can call methods with Call() and receive signals over
the channel returned by Signals(). Handling method calls is even easier; using
Export(), you can arrange DBus message calls to be directly translated to method
calls on a Go value.

Decoder and Encoder provide direct access to the DBus wire format. You usually
don't need to use them directly. While you may use them directly on the socket
as they accept the standard io interfaces, it is not advised to do so as this
would generate many small reads / writes that could limit performance.

Rules for encoding are as follows:

1. Any primitive Go type that has a direct equivalent in the wire format
is directly converted. This includes all fixed size integers
except for int8, as well as float64, bool and string.

2. Slices and maps are converted to arrays and dicts, respectively.

3. Most structs are converted to the expected DBus struct (all exported members
are marshalled as a DBus struct). The exceptions are all types and structs
defined in this package that have a custom wire format. These are ObjectPath,
Signature and Variant. Also, fields whose tag contains dbus:"-" will be skipped.

4. Trying to encode any other type (including int and uint!) will result
in a panic. This applies to all functions that call (*Encoder).Encode somewhere.

The rules for decoding are mostly just the reverse of the encoding rules,
except for the handling of variants. If a struct is wrapped in a variant,
its decoded value will be a slice of interfaces which contain the struct
fields in the correct order.

Because encoding and decoding of messages need special handling, they are also
implemented here.

*/
package dbus

// BUG(guelfey): Unix file descriptor passing is not implemented.

// BUG(guelfey): The implementation does not conform to the official
// specification in that most restrictions of the protocol (structure depth,
// maximum message size etc.) are not checked.

// BUG(guelfey): Emitting signals is not implemented yet.

// BUG(guelfey): This package needs new reflection features that are only
// availabe from the hg tip until Go 1.1 is released.
<commit_msg>Move bugs from documentation to GitHub<commit_after>/*
Package dbus implements bindings to the DBus message bus system, as well as the
corresponding encoding format.

For the message bus API, you first need to connect to a bus (usually the Session
or System bus). Then, you can call methods with Call() and receive signals over
the channel returned by Signals(). Handling method calls is even easier; using
Export(), you can arrange DBus message calls to be directly translated to method
calls on a Go value.

Decoder and Encoder provide direct access to the DBus wire format. You usually
don't need to use them directly. While you may use them directly on the socket
as they accept the standard io interfaces, it is not advised to do so as this
would generate many small reads / writes that could limit performance.

Rules for encoding are as follows:

1. Any primitive Go type that has a direct equivalent in the wire format
is directly converted. This includes all fixed size integers
except for int8, as well as float64, bool and string.

2. Slices and maps are converted to arrays and dicts, respectively.

3. Most structs are converted to the expected DBus struct (all exported members
are marshalled as a DBus struct). The exceptions are all types and structs
defined in this package that have a custom wire format. These are ObjectPath,
Signature and Variant. Also, fields whose tag contains dbus:"-" will be skipped.

4. Trying to encode any other type (including int and uint!) will result
in a panic. This applies to all functions that call (*Encoder).Encode somewhere.

The rules for decoding are mostly just the reverse of the encoding rules,
except for the handling of variants. If a struct is wrapped in a variant,
its decoded value will be a slice of interfaces which contain the struct
fields in the correct order.

Because encoding and decoding of messages need special handling, they are also
implemented here.

*/
package dbus

// BUG(guelfey): This package needs new reflection features that are only
// availabe from the hg tip until Go 1.1 is released.
<|endoftext|>


<commit_before>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"

	"github.com/flosch/pongo2"
)

var (
	Version   string
	GitCommit string

	output  = flag.String("o", "", "output file")
	version = flag.Bool("version", false, "print epp version")
)

func main() {
	flag.Parse()

	if *version {
		fmt.Fprintf(os.Stderr, "epp %s (%s)\n", Version, GitCommit)
		os.Exit(0)
	}

	if len(flag.Args()) == 0 {
		fmt.Fprintln(os.Stderr, "error: an input file is required")
		os.Exit(1)
	}

	fileContents, err := readInput(flag.Arg(0))
	if err != nil {
		fmt.Fprintf(os.Stderr, "IO error: %s\n", err)
		os.Exit(1)
	}

	out, err := parse(fileContents)
	if err != nil {
		fmt.Fprintf(os.Stderr, "templating error: %s\n", err)
		os.Exit(1)
	}

	if *output == "" {
		fmt.Printf(string(out))
		return
	}

	err = ioutil.WriteFile(*output, out, 0644)
	if err != nil {
		fmt.Fprintf(os.Stderr, "IO error: %s\n", err)
		os.Exit(1)
	}
}

func parse(input []byte) ([]byte, error) {
	tpl, err := pongo2.FromString(string(input))
	if err != nil {
		return nil, err
	}

	context := environToContext()
	out, err := tpl.ExecuteBytes(context)
	if err != nil {
		return nil, err
	}

	return out, nil
}

func readInput(input string) ([]byte, error) {
	if inputFile := flag.Arg(0); inputFile == "-" {
		return ioutil.ReadAll(os.Stdin)
	}

	return ioutil.ReadFile(input)
}

func environToContext() pongo2.Context {
	ctx := pongo2.Context{}

	for _, env := range os.Environ() {
		variable := strings.SplitN(env, "=", 2)
		key, value := variable[0], variable[1]

		ctx[key] = value
	}

	return ctx
}
<commit_msg>Refactor ExecuteBytes bit<commit_after>package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"os"
	"strings"

	"github.com/flosch/pongo2"
)

var (
	Version   string
	GitCommit string

	output  = flag.String("o", "", "output file")
	version = flag.Bool("version", false, "print epp version")
)

func main() {
	flag.Parse()

	if *version {
		fmt.Fprintf(os.Stderr, "epp %s (%s)\n", Version, GitCommit)
		os.Exit(0)
	}

	if len(flag.Args()) == 0 {
		fmt.Fprintln(os.Stderr, "error: an input file is required")
		os.Exit(1)
	}

	fileContents, err := readInput(flag.Arg(0))
	if err != nil {
		fmt.Fprintf(os.Stderr, "IO error: %s\n", err)
		os.Exit(1)
	}

	out, err := parse(fileContents)
	if err != nil {
		fmt.Fprintf(os.Stderr, "templating error: %s\n", err)
		os.Exit(1)
	}

	if *output == "" {
		fmt.Printf(string(out))
		return
	}

	err = ioutil.WriteFile(*output, out, 0644)
	if err != nil {
		fmt.Fprintf(os.Stderr, "IO error: %s\n", err)
		os.Exit(1)
	}
}

func parse(input []byte) ([]byte, error) {
	tpl, err := pongo2.FromString(string(input))
	if err != nil {
		return nil, err
	}

	context := environToContext()
	return tpl.ExecuteBytes(context)
}

func readInput(input string) ([]byte, error) {
	if inputFile := flag.Arg(0); inputFile == "-" {
		return ioutil.ReadAll(os.Stdin)
	}

	return ioutil.ReadFile(input)
}

func environToContext() pongo2.Context {
	ctx := pongo2.Context{}

	for _, env := range os.Environ() {
		variable := strings.SplitN(env, "=", 2)
		key, value := variable[0], variable[1]

		ctx[key] = value
	}

	return ctx
}
<|endoftext|>


<commit_before>package sdees

import (
	"fmt"
	"io/ioutil"
	"path"
	"strings"
	"time"
)

func Run() {
	// Some variables to be set later
	filterBranch := ""

	// Check if cloning needs to occur
	logger.Debug("Current remote: %s", Remote)
	measureTime := time.Now()
	fmt.Print("Fetching latest")
	if !exists(RemoteFolder) {
		logger.Debug("Remote folder does not exist: %s", RemoteFolder)
		err := Clone(RemoteFolder, Remote)
		if err != nil {
			logger.Warn("Problems cloning remote '%s': %s", Remote, err.Error())
		}
	} else {
		errFetch := Fetch(RemoteFolder)
		if errFetch == nil {
			fmt.Print("...done")
		} else {
			fmt.Println("...unable to fetch:")
			fmt.Println(errFetch.Error())
		}
	}
	fmt.Printf(" (%s)\n", time.Since(measureTime).String())

	// Prompt for passphrase
	Passphrase = PromptPassword(RemoteFolder)

	// List available documents to choose from
	availableFiles := ListFiles(RemoteFolder)
	if len(InputDocument) == 0 {
		var editDocument string
		fmt.Printf("\nCurrently available documents: ")
		logger.Debug("Last documents was %s", HashIDToString(CurrentDocument))
		for _, file := range availableFiles {
			fmt.Printf("\n- %s ", HashIDToString(file))
			if file == CurrentDocument {
				fmt.Print("(default) ")
			}
		}
		if len(CurrentDocument) == 0 {
			CurrentDocument = StringToHashID("notes.txt")
		}
		fmt.Printf("\n\nWhich document (press enter for '%s', or type name): ", HashIDToString(CurrentDocument))
		fmt.Scanln(&editDocument)
		if len(editDocument) == 0 && len(CurrentDocument) > 0 {
			// Pass
		} else if len(editDocument) == 0 && len(availableFiles) > 0 {
			CurrentDocument = availableFiles[0]
		} else if len(CurrentDocument) == 0 && len(editDocument) == 0 && len(availableFiles) == 0 {
			CurrentDocument = StringToHashID("notes.txt")
		} else if len(editDocument) > 0 {
			CurrentDocument = StringToHashID(editDocument)
		}
	} else {
		branchList, _ := ListBranches(RemoteFolder)
		for _, branch := range branchList {
			if branch == StringToHashID(InputDocument) {
				doc, _ := ListFileOfOne(RemoteFolder, branch)
				logger.Debug("You've entered a branch %s which is in document %s", branch, doc)
				InputDocument = doc
				filterBranch = branch
			}
		}
		CurrentDocument = InputDocument
	}
	logger.Debug("Current document: %s", CurrentDocument)
	// Save choice of current document
	SaveConfiguration(Editor, Remote, CurrentDocument)

	// Check if encryption is needed
	isNew := true
	for _, file := range availableFiles {
		if CurrentDocument == file {
			isNew = false
			break
		}
	}
	if !isNew && !All && !Summarize && !Export && !DeleteDocument && len(DeleteEntry) == 0 && len(filterBranch) == 0 && len(Search) == 0 {
		// Prompt for whether to load whole document
		var yesnoall string
		fmt.Print("\nLoad all entries (press enter for 'n')? (y/n) ")
		fmt.Scanln(&yesnoall)
		if yesnoall == "y" {
			All = true
		}
	}

	// Update the cache using the passphrase if needed
	cache, _, err := UpdateCache(RemoteFolder, CurrentDocument, false)
	if err != nil {
		logger.Error("Error updating cache: %s", err.Error())
		return
	}

	// Do deletions
	if DeleteDocument {
		GoDeleteDocument(cache)
		return
	} else if len(DeleteEntry) > 0 {
		GoDeleteEntry(cache)
		return
	}

	// Load fulltext
	texts := []string{}
	textsBranch := []string{}
	var branchHashes map[string]string
	if All || Export || Summarize || len(Search) > 0 || len(filterBranch) > 0 {
		texts, textsBranch, branchHashes = CombineEntries(cache)
		// Conduct the search
		if len(Search) > 0 {
			searchWords := GetWordsFromText(Search)
			textFoo := []string{}
			for i := range texts {
				for _, searchWord := range searchWords {
					if strings.Contains(strings.ToLower(texts[i]), strings.ToLower(searchWord)) {
						textFoo = append(textFoo, texts[i])
						break
					}
				}
			}
			texts = textFoo
		}
		if len(filterBranch) > 0 {
			for i, branch := range textsBranch {
				if branch == filterBranch {
					logger.Debug("Filtering out everything but branch %s", filterBranch)
					texts = []string{texts[i]}
					textsBranch = []string{textsBranch[i]}
				}
			}
		}
	}

	// Case-switch for what to do with fulltext
	if Export {
		fmt.Println("Exporting to " + HashIDToString(CurrentDocument))
		ioutil.WriteFile(HashIDToString(CurrentDocument), []byte(strings.Join(texts, "\n\n")+"\n"), 0644)
		return
	} else if Summarize {
		fmt.Println("\nSummary:")
		fmt.Println(SummarizeEntries(texts, textsBranch))
		return
	} else {
		if len(filterBranch) == 0 {
			texts = append(texts, HeadMatter(GetCurrentDate(), StringToHashID(MakeAlliteration())))
		} else {
			logger.Debug("Loaded entry '%s' on document '%s'\n", filterBranch, CurrentDocument)
			fmt.Printf("Loaded entry '%s' on document '%s'\n", HashIDToString(filterBranch), HashIDToString(CurrentDocument))
		}
		ioutil.WriteFile(path.Join(TempPath, "temp"), []byte(strings.Join(texts, "\n\n")+"\n"), 0644)
	}
	fulltext := WriteEntry()
	UpdateEntryFromText(fulltext, branchHashes)

	// Push new changes
	measureTime = time.Now()
	fmt.Print("Pushing changes")
	err = Push(RemoteFolder)
	if err == nil {
		fmt.Print("...done")
	} else {
		fmt.Print("...no internet, not pushing")
	}
	fmt.Printf(" (%s)\n", time.Since(measureTime).String())
}
<commit_msg>Forgot to change filename here<commit_after>package sdees

import (
	"fmt"
	"io/ioutil"
	"path"
	"strings"
	"time"
)

func Run() {
	// Some variables to be set later
	filterBranch := ""

	// Check if cloning needs to occur
	logger.Debug("Current remote: %s", Remote)
	measureTime := time.Now()
	fmt.Print("Fetching latest")
	if !exists(RemoteFolder) {
		logger.Debug("Remote folder does not exist: %s", RemoteFolder)
		err := Clone(RemoteFolder, Remote)
		if err != nil {
			logger.Warn("Problems cloning remote '%s': %s", Remote, err.Error())
		}
	} else {
		errFetch := Fetch(RemoteFolder)
		if errFetch == nil {
			fmt.Print("...done")
		} else {
			fmt.Println("...unable to fetch:")
			fmt.Println(errFetch.Error())
		}
	}
	fmt.Printf(" (%s)\n", time.Since(measureTime).String())

	// Prompt for passphrase
	Passphrase = PromptPassword(RemoteFolder)

	// List available documents to choose from
	availableFiles := ListFiles(RemoteFolder)
	if len(InputDocument) == 0 {
		var editDocument string
		fmt.Printf("\nCurrently available documents: ")
		logger.Debug("Last documents was %s", HashIDToString(CurrentDocument))
		for _, file := range availableFiles {
			fmt.Printf("\n- %s ", HashIDToString(file))
			if file == CurrentDocument {
				fmt.Print("(default) ")
			}
		}
		if len(CurrentDocument) == 0 {
			CurrentDocument = StringToHashID("notes.txt")
		}
		fmt.Printf("\n\nWhich document (press enter for '%s', or type name): ", HashIDToString(CurrentDocument))
		fmt.Scanln(&editDocument)
		if len(editDocument) == 0 && len(CurrentDocument) > 0 {
			// Pass
		} else if len(editDocument) == 0 && len(availableFiles) > 0 {
			CurrentDocument = availableFiles[0]
		} else if len(CurrentDocument) == 0 && len(editDocument) == 0 && len(availableFiles) == 0 {
			CurrentDocument = StringToHashID("notes.txt")
		} else if len(editDocument) > 0 {
			CurrentDocument = StringToHashID(editDocument)
		}
	} else {
		InputDocument = StringToHashID(InputDocument)
		branchList, _ := ListBranches(RemoteFolder)
		for _, branch := range branchList {
			if branch == InputDocument {
				doc, _ := ListFileOfOne(RemoteFolder, branch)
				logger.Debug("You've entered a branch %s which is in document %s", branch, doc)
				InputDocument = doc
				filterBranch = branch
			}
		}
		CurrentDocument = InputDocument
	}
	logger.Debug("Current document: %s", CurrentDocument)
	// Save choice of current document
	SaveConfiguration(Editor, Remote, CurrentDocument)

	// Check if encryption is needed
	isNew := true
	for _, file := range availableFiles {
		if CurrentDocument == file {
			isNew = false
			break
		}
	}
	if !isNew && !All && !Summarize && !Export && !DeleteDocument && len(DeleteEntry) == 0 && len(filterBranch) == 0 && len(Search) == 0 {
		// Prompt for whether to load whole document
		var yesnoall string
		fmt.Print("\nLoad all entries (press enter for 'n')? (y/n) ")
		fmt.Scanln(&yesnoall)
		if yesnoall == "y" {
			All = true
		}
	}

	// Update the cache using the passphrase if needed
	cache, _, err := UpdateCache(RemoteFolder, CurrentDocument, false)
	if err != nil {
		logger.Error("Error updating cache: %s", err.Error())
		return
	}

	// Do deletions
	if DeleteDocument {
		GoDeleteDocument(cache)
		return
	} else if len(DeleteEntry) > 0 {
		GoDeleteEntry(cache)
		return
	}

	// Load fulltext
	texts := []string{}
	textsBranch := []string{}
	var branchHashes map[string]string
	if All || Export || Summarize || len(Search) > 0 || len(filterBranch) > 0 {
		texts, textsBranch, branchHashes = CombineEntries(cache)
		// Conduct the search
		if len(Search) > 0 {
			searchWords := GetWordsFromText(Search)
			textFoo := []string{}
			for i := range texts {
				for _, searchWord := range searchWords {
					if strings.Contains(strings.ToLower(texts[i]), strings.ToLower(searchWord)) {
						textFoo = append(textFoo, texts[i])
						break
					}
				}
			}
			texts = textFoo
		}
		if len(filterBranch) > 0 {
			for i, branch := range textsBranch {
				if branch == filterBranch {
					logger.Debug("Filtering out everything but branch %s", filterBranch)
					texts = []string{texts[i]}
					textsBranch = []string{textsBranch[i]}
				}
			}
		}
	}

	// Case-switch for what to do with fulltext
	if Export {
		fmt.Println("Exporting to " + HashIDToString(CurrentDocument))
		ioutil.WriteFile(HashIDToString(CurrentDocument), []byte(strings.Join(texts, "\n\n")+"\n"), 0644)
		return
	} else if Summarize {
		fmt.Println("\nSummary:")
		fmt.Println(SummarizeEntries(texts, textsBranch))
		return
	} else {
		if len(filterBranch) == 0 {
			texts = append(texts, HeadMatter(GetCurrentDate(), StringToHashID(MakeAlliteration())))
		} else {
			logger.Debug("Loaded entry '%s' on document '%s'\n", filterBranch, CurrentDocument)
			fmt.Printf("Loaded entry '%s' on document '%s'\n", HashIDToString(filterBranch), HashIDToString(CurrentDocument))
		}
		ioutil.WriteFile(path.Join(TempPath, "temp"), []byte(strings.Join(texts, "\n\n")+"\n"), 0644)
	}
	fulltext := WriteEntry()
	UpdateEntryFromText(fulltext, branchHashes)

	// Push new changes
	measureTime = time.Now()
	fmt.Print("Pushing changes")
	err = Push(RemoteFolder)
	if err == nil {
		fmt.Print("...done")
	} else {
		fmt.Print("...no internet, not pushing")
	}
	fmt.Printf(" (%s)\n", time.Since(measureTime).String())
}
<|endoftext|>