-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
946ecc6
commit 8a25d88
Showing
5 changed files
with
180 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"path" | ||
"sort" | ||
"time" | ||
) | ||
|
||
type Nikki_date_t struct { | ||
Year int | ||
Month int | ||
Day int | ||
} | ||
|
||
type Nikki_t struct { | ||
Fname string | ||
Date Nikki_date_t | ||
Content string | ||
} | ||
|
||
func (a *App) Add_nikki_today() { | ||
if a.Setting_data.Nikki_dir == "" { | ||
return | ||
} | ||
|
||
timezone := time.FixedZone("Asia/Tokyo", 9*60*60) | ||
time_now := time.Now().In(timezone) | ||
f, err := os.OpenFile( | ||
path.Join( | ||
a.Setting_data.Nikki_dir, | ||
time_now.Format(TimeFormat_conv(a.Setting_data.Fname_format)), | ||
), | ||
os.O_WRONLY|os.O_CREATE, 0666, | ||
) | ||
if err != nil { | ||
} | ||
f.Close() | ||
} | ||
|
||
func (a *App) Parse_nikki_file(fname string) Nikki_t { | ||
content, err := ioutil.ReadFile(path.Join(a.Setting_data.Nikki_dir, fname)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
Fname_format_golang := TimeFormat_conv(a.Setting_data.Fname_format) | ||
|
||
t, err := time.Parse(Fname_format_golang, fname) | ||
|
||
return Nikki_t{ | ||
Fname: fname, Content: string(content), | ||
Date: Nikki_date_t{ | ||
Year: t.Year(), | ||
Month: int(t.Month()), | ||
Day: t.Day(), | ||
}, | ||
} | ||
} | ||
|
||
func (a *App) Write_nikki_file(v Nikki_t) { | ||
var err error | ||
var f *os.File | ||
f, err = os.Create(path.Join(a.Setting_data.Nikki_dir, v.Fname)) | ||
for err != nil { | ||
f, err = os.Create(path.Join(a.Setting_data.Nikki_dir, v.Fname)) | ||
time.Sleep(time.Millisecond * 200) | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write([]byte(v.Content)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (a *App) Set_nikki(v []Nikki_t) { | ||
for i, e := range v { | ||
if a.Nikki_data[i].Content != e.Content { | ||
a.Nikki_data[i].Content = e.Content | ||
a.Write_nikki_file(a.Nikki_data[i]) | ||
} | ||
} | ||
|
||
} | ||
|
||
func (a *App) Load_nikki() { | ||
var reslut []Nikki_t | ||
for _, e := range a.Get_dir_fnames(a.Setting_data.Nikki_dir) { | ||
reslut = append(reslut, a.Parse_nikki_file(e)) | ||
} | ||
|
||
sort.SliceStable(reslut, func(i, j int) bool { | ||
if reslut[i].Date.Year != reslut[j].Date.Year { | ||
return reslut[i].Date.Year > reslut[j].Date.Year | ||
} | ||
if reslut[i].Date.Month != reslut[j].Date.Month { | ||
return reslut[i].Date.Month > reslut[j].Date.Month | ||
} | ||
return reslut[i].Date.Day > reslut[j].Date.Day | ||
}) | ||
a.Nikki_data = reslut | ||
} | ||
|
||
func (a *App) Get_nikki() []Nikki_t { | ||
|
||
return a.Nikki_data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestParse_nikki_file(t *testing.T) { | ||
app := App{} | ||
app.Setting_data.Nikki_dir = "test-dir" | ||
app.Setting_data.Fname_format = "YYYY年MM月DD日.txt" | ||
|
||
assert.EqualExportedValues(t, | ||
Nikki_t{Fname: "2024年09月19日.txt", Date: Nikki_date_t{Year: 2024, Month: 9, Day: 19}, Content: "Test!"}, | ||
app.Parse_nikki_file("2024年09月19日.txt")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"time" | ||
) | ||
|
||
type Setting_t struct { | ||
Nikki_dir string | ||
Fname_format string | ||
} | ||
|
||
var default_Setting_data = Setting_t{Nikki_dir: "", Fname_format: "YYYY年MM月DD日.txt"} | ||
|
||
func (a *App) Get_setting() Setting_t { | ||
return a.Setting_data | ||
} | ||
|
||
func (a *App) Set_setting(v Setting_t) { | ||
if a.Setting_data != v { | ||
a.Setting_data = v | ||
a.Write_setting() | ||
} | ||
} | ||
|
||
func (a *App) Write_setting() { | ||
setting_text, err := json.Marshal(a.Setting_data) | ||
f, err := os.Create(a.setting_json_path) | ||
for err != nil { | ||
f, err = os.Create(a.setting_json_path) | ||
time.Sleep(time.Millisecond * 200) | ||
} | ||
defer f.Close() | ||
|
||
_, err = f.Write([]byte(setting_text)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (a *App) Load_setting() { | ||
|
||
setting_text, err := ioutil.ReadFile(a.setting_json_path) | ||
if err == nil { | ||
if err := json.Unmarshal(setting_text, &a.Setting_data); err != nil { | ||
a.Setting_data = default_Setting_data | ||
} | ||
} else { | ||
a.Setting_data = default_Setting_data | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters