about summary refs log tree commit diff stats
path: root/undo.lua
Commit message (Collapse)AuthorAgeFilesLines
* group all editor globalsKartik K. Agaram2022-07-121-20/+20
| | | | We're still accessing them through a global. But we'll change that next.
* stop saving the entire file when modifying drawingsKartik K. Agaram2022-06-101-4/+2
| | | | Now we just disallow that entirely.
* undo creating new drawingsKartik K. Agaram2022-06-101-0/+5
|
* crisp font renderingKartik K. Agaram2022-06-021-1/+0
|
* more efficient undo/redoKartik K. Agaram2022-06-021-2/+35
| | | | | Now the bottleneck shifts to applying undo/redo in large files. But things should be snappy if you don't use the sluggish feature.
* renameKartik K. Agaram2022-06-021-1/+1
|
* after much struggle, a brute-force undoKartik K. Agaram2022-06-021-0/+78
Incredibly inefficient, but I don't yet know how to efficiently encode undo mutations that can span multiple lines. There seems to be one bug related to creating new drawings; they're not spawning events and undoing past drawing creation has some weird artifacts. Redo seems to consistently work, though.
ight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
package apod

import (
	"encoding/json"
	"fmt"
	"regexp"

	"tildegit.org/andinus/cetus/request"
)

// APOD holds the response from the api. Not every field is returned
// in every request. Code & Msg should be filled only if the api
// returns an error, this behaviour was observed and shouldn't be
// trusted.
type APOD struct {
	Copyright      string `json:"copyright"`
	Date           string `json:"date"`
	Explanation    string `json:"explanation"`
	HDURL          string `json:"hdurl"`
	MediaType      string `json:"media_type"`
	ServiceVersion string `json:"service_version"`
	Title          string `json:"title"`
	URL            string `json:"url"`

	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

// UnmarshalJson will take body as input & unmarshal it to res.
func UnmarshalJson(res *APOD, body string) error {
	err := json.Unmarshal([]byte(body), res)
	if err != nil {
		err = fmt.Errorf("json.go: unmarshalling json failed\n%s",
			err.Error())
	}
	return err
}

// GetJson takes reqInfo as input and returns the body and an error.
func GetJson(reqInfo map[string]string) (string, error) {
	var body string
	var err error

	// This regexp is not perfect and does not guarantee that the
	// request will not fail because of wrong date, this will
	// eliminate many wrong dates though.
	re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])")
	if !re.MatchString(reqInfo["date"]) {
		err = fmt.Errorf("json.go: %s does not match format 'YYYY-MM-DD'",
			reqInfo["date"])
		return body, err
	}

	// reqInfo is map[string]string and params is built from it,
	// currently it takes apiKey and the date from reqInfo to
	// build param. If any new key/value is added to reqInfo then
	// it must be addded here too, it won't be sent as param
	// directly.
	params := make(map[string]string)
	params["api_key"] = reqInfo["apiKey"]
	params["date"] = reqInfo["date"]

	body, err = request.GetRes(reqInfo["api"], params)
	return body, err
}