mirror of
https://github.com/jbranchaud/til
synced 2026-03-05 23:48:46 +00:00
Compare commits
1 Commits
47104c335e
...
cf4ed73d4f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf4ed73d4f |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
|||||||
|
|
||||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1540 TILs and counting..._
|
_1537 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -283,7 +283,6 @@ _1540 TILs and counting..._
|
|||||||
- [Add Only Tracked Files From A Directory](git/add-only-tracked-files-from-a-directory.md)
|
- [Add Only Tracked Files From A Directory](git/add-only-tracked-files-from-a-directory.md)
|
||||||
- [Amend Author Of Previous Commit](git/amend-author-of-previous-commit.md)
|
- [Amend Author Of Previous Commit](git/amend-author-of-previous-commit.md)
|
||||||
- [Auto-Squash Those Fixup Commits](git/auto-squash-those-fixup-commits.md)
|
- [Auto-Squash Those Fixup Commits](git/auto-squash-those-fixup-commits.md)
|
||||||
- [Better Diffs With Delta](git/better-diffs-with-delta.md)
|
|
||||||
- [Caching Credentials](git/caching-credentials.md)
|
- [Caching Credentials](git/caching-credentials.md)
|
||||||
- [Change The Start Point Of A Branch](git/change-the-start-point-of-a-branch.md)
|
- [Change The Start Point Of A Branch](git/change-the-start-point-of-a-branch.md)
|
||||||
- [Check How A File Is Being Ignored](git/check-how-a-file-is-being-ignored.md)
|
- [Check How A File Is Being Ignored](git/check-how-a-file-is-being-ignored.md)
|
||||||
@@ -403,11 +402,9 @@ _1540 TILs and counting..._
|
|||||||
- [Access Go Docs Offline](go/access-go-docs-offline.md)
|
- [Access Go Docs Offline](go/access-go-docs-offline.md)
|
||||||
- [Add A Method To A Struct](go/add-a-method-to-a-struct.md)
|
- [Add A Method To A Struct](go/add-a-method-to-a-struct.md)
|
||||||
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
||||||
- [Check If Cobra Flag Was Set](go/check-if-cobra-flag-was-set.md)
|
|
||||||
- [Combine Two Slices](go/combine-two-slices.md)
|
- [Combine Two Slices](go/combine-two-slices.md)
|
||||||
- [Do Something N Times](go/do-something-n-times.md)
|
- [Do Something N Times](go/do-something-n-times.md)
|
||||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||||
- [Format Date And Time With Time Constants](go/format-date-and-time-with-time-constants.md)
|
|
||||||
- [Not So Random](go/not-so-random.md)
|
- [Not So Random](go/not-so-random.md)
|
||||||
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
||||||
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
# Better Diffs With Delta
|
|
||||||
|
|
||||||
A `git diff` from the command line is relatively bare bones. It shows you
|
|
||||||
removed lines and added lines that make up a changeset with the former text in
|
|
||||||
red and the later text in green. All other contextual text is in white. I've
|
|
||||||
found this to be good enough for most of the life of my git usage. I've been
|
|
||||||
missing out though.
|
|
||||||
|
|
||||||
By using [`delta`](https://github.com/dandavison/delta) as the pager and diff
|
|
||||||
filter for `git`, I get a bunch of nice visual improvements.
|
|
||||||
|
|
||||||
- Removals and additions are red and green shaded backgrounds
|
|
||||||
- Syntax highlighting for most languages
|
|
||||||
- Highlight specific part of a line that has changed
|
|
||||||
- Visual spacing and layout is clearer
|
|
||||||
|
|
||||||
To get all of this, all I had to do was install `delta`:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ brew install delta
|
|
||||||
```
|
|
||||||
|
|
||||||
And then add `delta` as both the _core_ pager and `diffFilter` in my global git
|
|
||||||
config file:
|
|
||||||
|
|
||||||
```
|
|
||||||
[core]
|
|
||||||
pager = delta
|
|
||||||
[interactive]
|
|
||||||
singleKey = true # unrelated, but nice to have
|
|
||||||
diffFilter = delta --color-only
|
|
||||||
```
|
|
||||||
|
|
||||||
It's also recommended that you use `zdiff3` for your merge conflict style,
|
|
||||||
which I already had:
|
|
||||||
|
|
||||||
```
|
|
||||||
[merge]
|
|
||||||
conflictstyle = zdiff3
|
|
||||||
```
|
|
||||||
|
|
||||||
Once you have ths all configred, try a `git diff` or `git add --patch` and see
|
|
||||||
how much more visual info you get.
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
# Check If Cobra Flag Was Set
|
|
||||||
|
|
||||||
When using [Cobra](https://github.com/spf13/cobra) to define a CLI, we can
|
|
||||||
specify a flag for a command like so:
|
|
||||||
|
|
||||||
```go
|
|
||||||
var Seed int64
|
|
||||||
myCmd.PersistentFlags().Int64VarP(&Seed, "seed", "", -1, "set a seed")
|
|
||||||
```
|
|
||||||
|
|
||||||
This `--seed` flag has a _default_ of `-1`. If the flag isn't specified, then
|
|
||||||
when we access that flag's value, we'll get `-1`.
|
|
||||||
|
|
||||||
But how do we differentiate between the _default_ `-1` and someone passing `-1`
|
|
||||||
to the `--seed` flag when running the program?
|
|
||||||
|
|
||||||
In the command definition, we can look at the flags and see, by name, if
|
|
||||||
specific ones were changed by user input rather than being the defaults.
|
|
||||||
|
|
||||||
```go
|
|
||||||
myCommand := &cobra.Command{
|
|
||||||
// coommand setup ...
|
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
|
||||||
if cmd.Flags().Changed("seed") {
|
|
||||||
seed, err := cmd.Flags().GetInt64("seed")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("Seed flag is missing from `cmdFlags()`")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Seed was set to %d\n", seed)
|
|
||||||
} else {
|
|
||||||
fmt.Println("Seed was not set")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
If we don't want to rely on the default and instead want to specify some other
|
|
||||||
behavior when the flag is not manually set by the user, we can detect that
|
|
||||||
scenario like this.
|
|
||||||
@@ -1,51 +0,0 @@
|
|||||||
# Format Date And Time With Time Constants
|
|
||||||
|
|
||||||
The Go [`time` package](https://pkg.go.dev/time) has a [`Format`
|
|
||||||
function](https://pkg.go.dev/time#Time.Format) for displaying the parts of a
|
|
||||||
date and time in standard and custom ways. It works a bit different than you
|
|
||||||
might be used to from other languages. Rather than using `strftime` identifiers
|
|
||||||
like in this string `"%B %d, %Y"`, there is a canonical date that is used as a
|
|
||||||
reference point.
|
|
||||||
|
|
||||||
That canonical date is from Janary 2nd, 2006. That was a Monday. It was at 5
|
|
||||||
seconds after 3:04PM. The Unix format of it looks like `"Mon Jan _2 15:04:05
|
|
||||||
MST 2006"`.
|
|
||||||
|
|
||||||
```
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
// This specific time pulled from `time.Format` docs
|
|
||||||
t, _ := time.Parse(time.UnixDate, "Wed Feb 25 11:06:39 PST 2015")
|
|
||||||
|
|
||||||
// Reference date and time:
|
|
||||||
// "Mon Jan _2 15:04:05 MST 2006"
|
|
||||||
|
|
||||||
strf1 := t.Format("|2006|02|01|03:04:05|Day: Mon|")
|
|
||||||
fmt.Println("strf1:", strf1)
|
|
||||||
// strf1: |2015|25|02|11:06:39|Day: Wed|
|
|
||||||
|
|
||||||
strf2 := t.Format(time.DateTime)
|
|
||||||
strf3 := t.Format(time.RubyDate)
|
|
||||||
strf4 := t.Format(time.Kitchen)
|
|
||||||
|
|
||||||
fmt.Println("DateTime:", strf2) // DateTime: 2015-02-25 11:06:39
|
|
||||||
fmt.Println("RubyDate:", strf3) // RubyDate: Wed Feb 25 11:06:39 +0000 2015
|
|
||||||
fmt.Println("Kitchen:", strf4) // Kitchen: 11:06AM
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Though there are a [variety of useful formatting
|
|
||||||
constants](https://pkg.go.dev/time#pkg-constants) already available like
|
|
||||||
`DateTime`, `RubyDate`, `Kitchen`, etc., we can also define our own formatting
|
|
||||||
string by using the reference values for each part of a date and time.
|
|
||||||
|
|
||||||
If you want to reference the year, whether as `YYYY` or `YY`, it is always
|
|
||||||
going to be a form of `2006`, so `2006` or `06` respectively. Even though the
|
|
||||||
above time variable is in February, our format strings will always need to use
|
|
||||||
one of `Jan`, `January`, `01` or `1`.
|
|
||||||
Reference in New Issue
Block a user