├── .github ├── dependabot.yml └── workflows │ ├── release.yml │ └── test.yml ├── .gitignore ├── LICENSE ├── README.adoc ├── cli.go ├── cli_test.go ├── docs ├── demo.gif └── demo2.gif ├── go.mod ├── go.sum ├── log.go ├── main.go ├── slot.go ├── slot_test.go └── view.go /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # To get started with Dependabot version updates, you'll need to specify which 2 | # package ecosystems to update and where the package manifests are located. 3 | # Please see the documentation for all configuration options: 4 | # https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | --- 6 | version: 2 7 | updates: 8 | - package-ecosystem: "gomod" 9 | directory: "/" 10 | schedule: 11 | interval: "daily" 12 | assignees: 13 | - "jiro4989" 14 | - package-ecosystem: "github-actions" 15 | directory: "/" 16 | schedule: 17 | interval: "daily" 18 | assignees: 19 | - "jiro4989" 20 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: release 2 | 3 | on: 4 | push: 5 | tags: 6 | - 'v*.*.*' 7 | 8 | env: 9 | app: slotchmod 10 | goversion: '1.23' 11 | build_opts: '-ldflags="-s -w -extldflags \"-static\""' 12 | 13 | jobs: 14 | build-artifact: 15 | runs-on: ubuntu-latest 16 | strategy: 17 | matrix: 18 | os: [linux, windows, darwin] 19 | arch: [amd64] 20 | steps: 21 | - uses: actions/checkout@v4 22 | - uses: actions/setup-go@v5 23 | with: 24 | go-version: ${{ env.goversion }} 25 | - name: Build 26 | run: | 27 | go build ${{ env.build_opts }} 28 | if [[ $GOOS = windows ]]; then 29 | 7z a ${{ env.app }}-$GOOS-$GOARCH.zip ./${{ env.app }}.exe LICENSE README.* 30 | else 31 | tar czf ${{ env.app }}-$GOOS-$GOARCH.tar.gz ./${{ env.app }} LICENSE README.* 32 | fi 33 | env: 34 | GOOS: ${{ matrix.os }} 35 | GOARCH: ${{ matrix.arch }} 36 | 37 | - name: Upload artifact (windows) 38 | uses: actions/upload-artifact@v4 39 | with: 40 | name: artifact-${{ matrix.os }}-${{ matrix.arch }} 41 | path: ${{ env.app }}-${{ matrix.os }}-${{ matrix.arch }}.zip 42 | if: matrix.os == 'windows' 43 | 44 | - name: Upload artifact (unix) 45 | uses: actions/upload-artifact@v4 46 | with: 47 | name: artifact-${{ matrix.os }}-${{ matrix.arch }} 48 | path: ${{ env.app }}-${{ matrix.os }}-${{ matrix.arch }}.tar.gz 49 | if: matrix.os != 'windows' 50 | 51 | build-debian-packages: 52 | runs-on: ubuntu-latest 53 | steps: 54 | - uses: actions/checkout@v4 55 | - uses: actions/setup-go@v5 56 | with: 57 | go-version: ${{ env.goversion }} 58 | - run: go build ${{ env.build_opts }} 59 | - name: Create package 60 | run: | 61 | mkdir -p .debpkg/usr/bin 62 | cp -p ./${{ env.app }} .debpkg/usr/bin/ 63 | - uses: jiro4989/build-deb-action@v4 64 | with: 65 | package: ${{ env.app }} 66 | package_root: .debpkg 67 | maintainer: jiro4989 68 | version: ${{ github.ref }} 69 | arch: 'amd64' 70 | desc: '${{ env.app }} is command to convert from color text (ANSI or 256) to image.' 71 | - uses: actions/upload-artifact@v4 72 | with: 73 | name: artifact-deb 74 | path: | 75 | ./*.deb 76 | 77 | build-rpm-packages: 78 | runs-on: ubuntu-latest 79 | steps: 80 | - uses: actions/checkout@v4 81 | - uses: actions/setup-go@v5 82 | with: 83 | go-version: ${{ env.goversion }} 84 | - run: go build ${{ env.build_opts }} 85 | - name: Create package 86 | run: | 87 | mkdir -p .rpmpkg/usr/bin 88 | cp -p ./${{ env.app }} .rpmpkg/usr/bin/ 89 | - uses: jiro4989/build-rpm-action@v2 90 | with: 91 | summary: '${{ env.app }} is command to convert from color text (ANSI or 256) to image.' 92 | package: ${{ env.app }} 93 | package_root: .rpmpkg 94 | maintainer: jiro4989 95 | version: ${{ github.ref }} 96 | arch: 'x86_64' 97 | desc: '${{ env.app }} is command to convert from color text (ANSI or 256) to image.' 98 | - uses: actions/upload-artifact@v4 99 | with: 100 | name: artifact-rpm 101 | path: | 102 | ./*.rpm 103 | !./*-debuginfo-*.rpm 104 | 105 | create-release: 106 | runs-on: ubuntu-latest 107 | needs: 108 | - build-artifact 109 | - build-debian-packages 110 | - build-rpm-packages 111 | steps: 112 | - uses: actions/checkout@v4 113 | - name: Create Release 114 | id: create-release 115 | uses: actions/create-release@v1 116 | env: 117 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 118 | with: 119 | tag_name: ${{ github.ref }} 120 | release_name: ${{ github.ref }} 121 | body: ${{ github.ref }} 122 | draft: false 123 | prerelease: false 124 | 125 | - name: Write upload_url to file 126 | run: echo '${{ steps.create-release.outputs.upload_url }}' > upload_url.txt 127 | 128 | - uses: actions/upload-artifact@v4 129 | with: 130 | name: create-release 131 | path: upload_url.txt 132 | 133 | upload-release: 134 | runs-on: ubuntu-latest 135 | needs: create-release 136 | strategy: 137 | matrix: 138 | os: [linux, windows, darwin] 139 | arch: [amd64] 140 | include: 141 | - os: windows 142 | asset_content_type: application/zip 143 | - os: linux 144 | asset_content_type: application/gzip 145 | - os: darwin 146 | asset_content_type: application/gzip 147 | steps: 148 | - uses: actions/download-artifact@v4 149 | with: 150 | name: artifact-${{ matrix.os }}-${{ matrix.arch }} 151 | 152 | - uses: actions/download-artifact@v4 153 | with: 154 | name: create-release 155 | 156 | - id: vars 157 | run: | 158 | echo "::set-output name=upload_url::$(cat upload_url.txt)" 159 | echo "::set-output name=asset_name::$(ls ${{ env.app }}-${{ matrix.os }}-${{ matrix.arch }}.* | head -n 1)" 160 | 161 | - name: Upload Release Asset 162 | id: upload-release-asset 163 | uses: actions/upload-release-asset@v1 164 | env: 165 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 166 | with: 167 | upload_url: ${{ steps.vars.outputs.upload_url }} 168 | asset_path: ${{ steps.vars.outputs.asset_name }} 169 | asset_name: ${{ steps.vars.outputs.asset_name }} 170 | asset_content_type: ${{ matrix.asset_content_type }} 171 | 172 | upload-linux-package: 173 | runs-on: ubuntu-latest 174 | needs: create-release 175 | strategy: 176 | matrix: 177 | include: 178 | - pkg: deb 179 | asset_content_type: application/vnd.debian.binary-package 180 | - pkg: rpm 181 | asset_content_type: application/x-rpm 182 | steps: 183 | - uses: actions/download-artifact@v4 184 | with: 185 | name: artifact-${{ matrix.pkg }} 186 | 187 | - uses: actions/download-artifact@v4 188 | with: 189 | name: create-release 190 | 191 | - id: vars 192 | run: | 193 | echo "::set-output name=upload_url::$(cat upload_url.txt)" 194 | echo "::set-output name=asset_name::$(ls *.${{ matrix.pkg }} | head -n 1)" 195 | 196 | - name: Upload Release Asset 197 | id: upload-release-asset 198 | uses: actions/upload-release-asset@v1 199 | env: 200 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 201 | with: 202 | upload_url: ${{ steps.vars.outputs.upload_url }} 203 | asset_path: ${{ steps.vars.outputs.asset_name }} 204 | asset_name: ${{ steps.vars.outputs.asset_name }} 205 | asset_content_type: ${{ matrix.asset_content_type }} 206 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: test 2 | 3 | on: 4 | push: 5 | branches: 6 | - master 7 | paths: 8 | - '**.go' 9 | - '.github/**' 10 | pull_request: 11 | paths: 12 | - '**.go' 13 | - '.github/**' 14 | 15 | jobs: 16 | test: 17 | runs-on: ubuntu-latest 18 | strategy: 19 | matrix: 20 | go: ['1.20', '1.21', '1.x'] 21 | steps: 22 | - uses: actions/checkout@v4 23 | - uses: actions/setup-go@v5 24 | with: 25 | go-version: ${{ matrix.go }} 26 | - run: go build 27 | - run: go test -cover ./... 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | slotchmod -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 jiro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = slotchmod 2 | :sectnums: 3 | :toc: left 4 | 5 | `slotchmod` changes file permission with a slot. 6 | This is a useful tool when you want to mess up file access permissions. 7 | 8 | image:./docs/demo.gif[] 9 | 10 | image:./docs/demo2.gif[] 11 | 12 | == Usage 13 | 14 | [source,bash] 15 | ---- 16 | slotchmod [files...] 17 | ---- 18 | 19 | == Installation 20 | 21 | [source,bash] 22 | ---- 23 | go get github.com/jiro4989/slotchmod 24 | # or 25 | go install github.com/jiro4989/slotchmod@master 26 | ---- 27 | 28 | or download executables from https://github.com/jiro4989/slotchmod/releases[GitHub Releases]. 29 | 30 | == Key input 31 | 32 | [options="header"] 33 | |================= 34 | | Key | Description 35 | | Enter | Select slot 36 | | q, Ctrl-C, Ctrl-D | Quit 37 | |================= 38 | 39 | == Help 40 | 41 | [source,text] 42 | ---- 43 | slotchmod changes file permissions with a slot 44 | 45 | Usage: 46 | slotchmod [OPTIONS] [files...] 47 | 48 | Examples: 49 | slotchmod sample.txt 50 | 51 | Options: 52 | -level string 53 | slot difficulty. [easy|normal|hard] (default "normal") 54 | ---- 55 | -------------------------------------------------------------------------------- /cli.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "fmt" 6 | "os" 7 | ) 8 | 9 | type CmdArgs struct { 10 | Level string 11 | Style string 12 | Args []string 13 | } 14 | 15 | var ( 16 | styles = map[string]DrawStyle{ 17 | "simple": DrawStyleSimple, 18 | "big": DrawStyleBig, 19 | } 20 | ) 21 | 22 | func ParseArgs() (*CmdArgs, error) { 23 | opts := CmdArgs{} 24 | 25 | flag.Usage = flagHelpMessage 26 | flag.StringVar(&opts.Level, "level", "normal", "slot difficulty. [easy|normal|hard]") 27 | flag.StringVar(&opts.Style, "style", "simple", "style. [simple|big]") 28 | flag.Parse() 29 | opts.Args = flag.Args() 30 | 31 | if err := opts.Validate(); err != nil { 32 | return nil, err 33 | } 34 | 35 | return &opts, nil 36 | } 37 | 38 | func flagHelpMessage() { 39 | cmd := os.Args[0] 40 | fmt.Fprintln(os.Stderr, fmt.Sprintf("%s changes file permissions with a slot", cmd)) 41 | fmt.Fprintln(os.Stderr, "") 42 | fmt.Fprintln(os.Stderr, "Usage:") 43 | fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s [OPTIONS] [files...]", cmd)) 44 | fmt.Fprintln(os.Stderr, "") 45 | fmt.Fprintln(os.Stderr, "Examples:") 46 | fmt.Fprintln(os.Stderr, fmt.Sprintf(" %s sample.txt", cmd)) 47 | fmt.Fprintln(os.Stderr, "") 48 | fmt.Fprintln(os.Stderr, "Options:") 49 | 50 | flag.PrintDefaults() 51 | } 52 | 53 | func (c *CmdArgs) Validate() error { 54 | if len(c.Args) < 1 { 55 | return fmt.Errorf("Must need files") 56 | } 57 | 58 | if _, ok := slotIntervalTime[c.Level]; !ok { 59 | return fmt.Errorf("-level must be 'easy' or 'normal' or 'hard'.") 60 | } 61 | 62 | if _, ok := styles[c.Style]; !ok { 63 | return fmt.Errorf("-style must be 'simple' or 'big'.") 64 | } 65 | 66 | for _, file := range c.Args { 67 | _, err := os.Stat(file) 68 | if os.IsNotExist(err) { 69 | return fmt.Errorf("%s file doesn't exist.", file) 70 | } 71 | if os.IsPermission(err) { 72 | return fmt.Errorf("%s permission denied.", file) 73 | } 74 | } 75 | 76 | return nil 77 | } 78 | -------------------------------------------------------------------------------- /cli_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func TestCmdArgsValidate(t *testing.T) { 10 | tests := []struct { 11 | desc string 12 | args CmdArgs 13 | wantErr bool 14 | }{ 15 | { 16 | desc: "ok", 17 | args: CmdArgs{ 18 | Level: "normal", 19 | Style: "simple", 20 | Args: []string{"LICENSE"}, 21 | }, 22 | wantErr: false, 23 | }, 24 | { 25 | desc: "ok: level is hard", 26 | args: CmdArgs{ 27 | Level: "hard", 28 | Style: "simple", 29 | Args: []string{"LICENSE"}, 30 | }, 31 | wantErr: false, 32 | }, 33 | { 34 | desc: "ok: style is big", 35 | args: CmdArgs{ 36 | Level: "normal", 37 | Style: "big", 38 | Args: []string{"LICENSE"}, 39 | }, 40 | wantErr: false, 41 | }, 42 | { 43 | desc: "ng: arguments count is 0", 44 | args: CmdArgs{ 45 | Level: "normal", 46 | Style: "simple", 47 | Args: []string{}, 48 | }, 49 | wantErr: true, 50 | }, 51 | { 52 | desc: "ng: illegal level string", 53 | args: CmdArgs{ 54 | Level: "aiueo", 55 | Style: "simple", 56 | Args: []string{"LICENSE"}, 57 | }, 58 | wantErr: true, 59 | }, 60 | { 61 | desc: "ng: file doesn't exist", 62 | args: CmdArgs{ 63 | Level: "aiueo", 64 | Style: "simple", 65 | Args: []string{"LICENSE", "sushi.txt"}, 66 | }, 67 | wantErr: true, 68 | }, 69 | } 70 | 71 | for _, tt := range tests { 72 | t.Run(tt.desc, func(t *testing.T) { 73 | assert := assert.New(t) 74 | 75 | err := tt.args.Validate() 76 | if tt.wantErr { 77 | assert.Error(err) 78 | return 79 | } 80 | 81 | assert.NoError(err) 82 | }) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /docs/demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiro4989/slotchmod/f038f51b6cef61d2b3efb488896b50646dee0ff0/docs/demo.gif -------------------------------------------------------------------------------- /docs/demo2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jiro4989/slotchmod/f038f51b6cef61d2b3efb488896b50646dee0ff0/docs/demo2.gif -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/jiro4989/slotchmod 2 | 3 | go 1.20 4 | 5 | require ( 6 | github.com/nsf/termbox-go v1.1.1 7 | github.com/stretchr/testify v1.10.0 8 | ) 9 | 10 | require ( 11 | github.com/davecgh/go-spew v1.1.1 // indirect 12 | github.com/mattn/go-runewidth v0.0.9 // indirect 13 | github.com/pmezard/go-difflib v1.0.0 // indirect 14 | gopkg.in/yaml.v3 v3.0.1 // indirect 15 | ) 16 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 2 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0= 4 | github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= 5 | github.com/nsf/termbox-go v1.1.1 h1:nksUPLCb73Q++DwbYUBEglYBRPZyoXJdrj5L+TkjyZY= 6 | github.com/nsf/termbox-go v1.1.1/go.mod h1:T0cTdVuOwf7pHQNtfhnEbzHbcNyCEcVU4YPpouCbVxo= 7 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 8 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 9 | github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= 10 | github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= 11 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= 12 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 13 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 14 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 15 | -------------------------------------------------------------------------------- /log.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | ) 7 | 8 | func Err(err error) { 9 | fmt.Fprintln(os.Stderr, "[ERR] "+err.Error()) 10 | } 11 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "os" 6 | "strconv" 7 | "time" 8 | 9 | termbox "github.com/nsf/termbox-go" 10 | ) 11 | 12 | var ( 13 | slotIntervalTime = map[string]int{ 14 | "easy": 200, 15 | "normal": 100, 16 | "hard": 50, 17 | } 18 | ) 19 | 20 | func main() { 21 | args, err := ParseArgs() 22 | if err != nil { 23 | Err(err) 24 | os.Exit(1) 25 | } 26 | 27 | interval := slotIntervalTime[args.Level] 28 | style := styles[args.Style] 29 | slot := NewSlot(0, interval) 30 | 31 | if err := termbox.Init(); err != nil { 32 | panic(err) 33 | } 34 | defer termbox.Close() 35 | termbox.SetInputMode(termbox.InputEsc) 36 | termbox.Flush() 37 | 38 | go clock(slot, style) 39 | waitKeyInput(slot) 40 | termbox.Close() 41 | 42 | changeMode(slot, args.Args) 43 | } 44 | 45 | func clock(s *Slot, st DrawStyle) { 46 | sleepMilSec := time.Duration(s.IntervalTime()) * time.Millisecond 47 | 48 | for !s.IsFinished() { 49 | s.Switch() 50 | DrawSlot(s, st) 51 | time.Sleep(sleepMilSec) 52 | } 53 | } 54 | 55 | func waitKeyInput(s *Slot) { 56 | for { 57 | switch ev := termbox.PollEvent(); ev.Type { 58 | case termbox.EventKey: 59 | switch ev.Key { 60 | case termbox.KeyCtrlC, termbox.KeyCtrlD: 61 | return 62 | case termbox.KeyEnter: 63 | s.Select() 64 | } 65 | switch ev.Ch { 66 | case 'q': 67 | return 68 | } 69 | } 70 | if s.IsFinished() { 71 | return 72 | } 73 | } 74 | } 75 | 76 | func changeMode(s *Slot, files []string) { 77 | slots := s.Slots() 78 | for _, file := range files { 79 | perm := fmt.Sprintf("0%d%d%d", slots[0], slots[1], slots[2]) 80 | t := fmt.Sprintf("chmod %s %s", perm, file) 81 | fmt.Println(t) 82 | 83 | mode, err := strconv.ParseUint(perm, 8, 32) 84 | if err != nil { 85 | panic(err) 86 | } 87 | if err := os.Chmod(file, os.FileMode(mode)); err != nil { 88 | panic(err) 89 | } 90 | } 91 | if slots[0] == slots[1] && slots[1] == slots[2] { 92 | fmt.Println("BINGO🎉") 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /slot.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | crand "crypto/rand" 5 | "math" 6 | "math/big" 7 | "math/rand" 8 | ) 9 | 10 | const ( 11 | slotMinValue = 0 12 | slotMaxValue = 7 13 | ) 14 | 15 | type Slot struct { 16 | slots [3]int 17 | currentSlotIndex int 18 | isFinished bool 19 | intervalTime int 20 | } 21 | 22 | func NewSlot(seed int64, interval int) *Slot { 23 | if seed == 0 { 24 | i, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64)) 25 | seed = i.Int64() 26 | } 27 | rand.Seed(seed) 28 | 29 | s := Slot{} 30 | for i := 0; i < 3; i++ { 31 | slotValue := rand.Intn(slotMaxValue + 1) 32 | s.slots[i] = slotValue 33 | } 34 | s.intervalTime = interval 35 | 36 | return &s 37 | } 38 | 39 | func (s *Slot) Switch() { 40 | s.slots[s.currentSlotIndex] = s.NextValue() 41 | } 42 | 43 | func (s *Slot) Select() { 44 | if 2 <= s.currentSlotIndex { 45 | s.isFinished = true 46 | return 47 | } 48 | s.currentSlotIndex++ 49 | } 50 | 51 | func (s *Slot) IsFinished() bool { 52 | return s.isFinished 53 | } 54 | 55 | func (s *Slot) Slots() [3]int { 56 | return s.slots 57 | } 58 | 59 | func (s *Slot) PreviousValue() int { 60 | v := s.slots[s.currentSlotIndex] - 1 61 | if v < slotMinValue { 62 | v = slotMaxValue 63 | } 64 | return v 65 | } 66 | 67 | func (s *Slot) CurrentValue() int { 68 | return s.slots[s.currentSlotIndex] 69 | } 70 | 71 | func (s *Slot) CurrentSlotIndex() int { 72 | return s.currentSlotIndex 73 | } 74 | 75 | func (s *Slot) NextValue() int { 76 | v := s.slots[s.currentSlotIndex] + 1 77 | if slotMaxValue < v { 78 | v = slotMinValue 79 | } 80 | return v 81 | } 82 | 83 | func (s *Slot) IntervalTime() int { 84 | return s.intervalTime 85 | } 86 | -------------------------------------------------------------------------------- /slot_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | 6 | "github.com/stretchr/testify/assert" 7 | ) 8 | 9 | func TestSlotNewSlot(t *testing.T) { 10 | assert := assert.New(t) 11 | assert.NotNil(NewSlot(0, 100)) 12 | assert.NotNil(NewSlot(1, 100)) 13 | } 14 | 15 | func TestSlotPreviousValue(t *testing.T) { 16 | tests := []struct { 17 | desc string 18 | slot Slot 19 | want int 20 | }{ 21 | { 22 | desc: "0 -> 7", 23 | slot: Slot{ 24 | slots: [3]int{0, 6, 7}, 25 | currentSlotIndex: 0, 26 | }, 27 | want: 7, 28 | }, 29 | { 30 | desc: "6 -> 5", 31 | slot: Slot{ 32 | slots: [3]int{0, 6, 7}, 33 | currentSlotIndex: 1, 34 | }, 35 | want: 5, 36 | }, 37 | { 38 | desc: "7 -> 6", 39 | slot: Slot{ 40 | slots: [3]int{0, 6, 7}, 41 | currentSlotIndex: 2, 42 | }, 43 | want: 6, 44 | }, 45 | } 46 | 47 | for _, tt := range tests { 48 | t.Run(tt.desc, func(t *testing.T) { 49 | assert := assert.New(t) 50 | got := tt.slot.PreviousValue() 51 | assert.Equal(tt.want, got) 52 | }) 53 | } 54 | } 55 | 56 | func TestSlotCurrentValue(t *testing.T) { 57 | tests := []struct { 58 | desc string 59 | slot Slot 60 | want int 61 | }{ 62 | { 63 | desc: "0", 64 | slot: Slot{ 65 | slots: [3]int{0, 6, 7}, 66 | currentSlotIndex: 0, 67 | }, 68 | want: 0, 69 | }, 70 | { 71 | desc: "6", 72 | slot: Slot{ 73 | slots: [3]int{0, 6, 7}, 74 | currentSlotIndex: 1, 75 | }, 76 | want: 6, 77 | }, 78 | { 79 | desc: "7", 80 | slot: Slot{ 81 | slots: [3]int{0, 6, 7}, 82 | currentSlotIndex: 2, 83 | }, 84 | want: 7, 85 | }, 86 | } 87 | 88 | for _, tt := range tests { 89 | t.Run(tt.desc, func(t *testing.T) { 90 | assert := assert.New(t) 91 | got := tt.slot.CurrentValue() 92 | assert.Equal(tt.want, got) 93 | }) 94 | } 95 | } 96 | 97 | func TestSlotNextValue(t *testing.T) { 98 | tests := []struct { 99 | desc string 100 | slot Slot 101 | want int 102 | }{ 103 | { 104 | desc: "0 -> 1", 105 | slot: Slot{ 106 | slots: [3]int{0, 6, 7}, 107 | currentSlotIndex: 0, 108 | }, 109 | want: 1, 110 | }, 111 | { 112 | desc: "6 -> 7", 113 | slot: Slot{ 114 | slots: [3]int{0, 6, 7}, 115 | currentSlotIndex: 1, 116 | }, 117 | want: 7, 118 | }, 119 | { 120 | desc: "7 -> 0", 121 | slot: Slot{ 122 | slots: [3]int{0, 6, 7}, 123 | currentSlotIndex: 2, 124 | }, 125 | want: 0, 126 | }, 127 | } 128 | 129 | for _, tt := range tests { 130 | t.Run(tt.desc, func(t *testing.T) { 131 | assert := assert.New(t) 132 | got := tt.slot.NextValue() 133 | assert.Equal(tt.want, got) 134 | }) 135 | } 136 | } 137 | 138 | func TestSlotSelect(t *testing.T) { 139 | tests := []struct { 140 | desc string 141 | slot Slot 142 | wantIsFinished bool 143 | wantCurrentSlotIndex int 144 | }{ 145 | { 146 | desc: "turn ON isFinished flag when slot index is 2, and slot index is 2", 147 | slot: Slot{ 148 | slots: [3]int{0, 6, 7}, 149 | currentSlotIndex: 2, 150 | }, 151 | wantIsFinished: true, 152 | wantCurrentSlotIndex: 2, 153 | }, 154 | { 155 | desc: "turn OFF isFinished flag when slot index is 0 or 1, and increments slot index", 156 | slot: Slot{ 157 | slots: [3]int{0, 6, 7}, 158 | currentSlotIndex: 0, 159 | }, 160 | wantIsFinished: false, 161 | wantCurrentSlotIndex: 1, 162 | }, 163 | { 164 | desc: "turn OFF isFinished flag when slot index is 0 or 1, and increments slot index", 165 | slot: Slot{ 166 | slots: [3]int{0, 6, 7}, 167 | currentSlotIndex: 1, 168 | }, 169 | wantIsFinished: false, 170 | wantCurrentSlotIndex: 2, 171 | }, 172 | } 173 | 174 | for _, tt := range tests { 175 | t.Run(tt.desc, func(t *testing.T) { 176 | assert := assert.New(t) 177 | tt.slot.Select() 178 | assert.Equal(tt.wantIsFinished, tt.slot.isFinished) 179 | assert.Equal(tt.wantCurrentSlotIndex, tt.slot.currentSlotIndex) 180 | }) 181 | } 182 | } 183 | 184 | func TestSlotSwitch(t *testing.T) { 185 | tests := []struct { 186 | desc string 187 | slot Slot 188 | want [3]int 189 | }{ 190 | { 191 | desc: "increments slots[0]", 192 | slot: Slot{ 193 | slots: [3]int{0, 6, 7}, 194 | currentSlotIndex: 0, 195 | }, 196 | want: [3]int{1, 6, 7}, 197 | }, 198 | { 199 | desc: "increments slots[1]", 200 | slot: Slot{ 201 | slots: [3]int{0, 6, 7}, 202 | currentSlotIndex: 1, 203 | }, 204 | want: [3]int{0, 7, 7}, 205 | }, 206 | { 207 | desc: "increments slots[2], and set 0", 208 | slot: Slot{ 209 | slots: [3]int{0, 6, 7}, 210 | currentSlotIndex: 2, 211 | }, 212 | want: [3]int{0, 6, 0}, 213 | }, 214 | } 215 | 216 | for _, tt := range tests { 217 | t.Run(tt.desc, func(t *testing.T) { 218 | assert := assert.New(t) 219 | tt.slot.Switch() 220 | assert.Equal(tt.want, tt.slot.slots) 221 | }) 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /view.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | 6 | termbox "github.com/nsf/termbox-go" 7 | ) 8 | 9 | type DrawStyle int 10 | 11 | const ( 12 | DrawStyleSimple DrawStyle = iota 13 | DrawStyleBig 14 | ) 15 | 16 | var ( 17 | bigEmptyChmod = [6]string{ 18 | ` `, 19 | ` `, 20 | ` `, 21 | ` `, 22 | ` `, 23 | ` `, 24 | } 25 | bigChmod = [6]string{ 26 | ` _ _ `, 27 | ` ___| |__ _ __ ___ ___ __| |`, 28 | " / __| '_ \\| '_ ` _ \\ / _ \\ / _` |", 29 | `| (__| | | | | | | | | (_) | (_| |`, 30 | ` \___|_| |_|_| |_| |_|\___/ \__,_|`, 31 | ` `, 32 | } 33 | emptyNumber = [6]string{ 34 | ` `, 35 | ` `, 36 | ` `, 37 | ` `, 38 | ` `, 39 | ` `, 40 | } 41 | bigNumbers = [10][6]string{ 42 | { 43 | ` ___ `, 44 | ` / _ \ `, 45 | ` | | | | `, 46 | ` | |_| | `, 47 | ` \___/ `, 48 | ` `, 49 | }, 50 | { 51 | ` _ `, 52 | ` / | `, 53 | ` | | `, 54 | ` | | `, 55 | ` |_| `, 56 | ` `, 57 | }, 58 | { 59 | ` ____ `, 60 | ` |___ \ `, 61 | ` __) | `, 62 | ` / __/ `, 63 | ` |_____| `, 64 | ` `, 65 | }, 66 | { 67 | ` _____ `, 68 | ` |___ / `, 69 | ` |_ \ `, 70 | ` ___) | `, 71 | ` |____/ `, 72 | ` `, 73 | }, 74 | { 75 | ` _ _ `, 76 | `| || | `, 77 | `| || |_ `, 78 | `|__ _| `, 79 | ` |_| `, 80 | ` `, 81 | }, 82 | { 83 | ` ____ `, 84 | ` | ___| `, 85 | ` |___ \ `, 86 | ` ___) | `, 87 | ` |____/ `, 88 | ` `, 89 | }, 90 | { 91 | ` __ `, 92 | ` / /_ `, 93 | ` | '_ \ `, 94 | ` | (_) | `, 95 | ` \___/ `, 96 | ` `, 97 | }, 98 | { 99 | ` _____ `, 100 | ` |___ | `, 101 | ` / / `, 102 | ` / / `, 103 | ` /_/ `, 104 | ` `, 105 | }, 106 | { 107 | ` ___ `, 108 | ` ( _ ) `, 109 | ` / _ \ `, 110 | ` | (_) | `, 111 | ` \___/ `, 112 | ` `, 113 | }, 114 | { 115 | ` ___ `, 116 | ` / _ \ `, 117 | ` | (_) | `, 118 | ` \__, | `, 119 | ` /_/ `, 120 | ` `, 121 | }, 122 | } 123 | ) 124 | 125 | func DrawSlot(s *Slot, st DrawStyle) { 126 | termbox.Clear(termbox.ColorDefault, termbox.ColorDefault) 127 | 128 | idx := s.CurrentSlotIndex() 129 | pv := s.PreviousValue() 130 | nv := s.NextValue() 131 | slots := s.Slots() 132 | 133 | switch st { 134 | case DrawStyleSimple: 135 | drawSimple(slots, idx, pv, nv) 136 | case DrawStyleBig: 137 | drawBig(slots, idx, pv, nv) 138 | default: 139 | drawSimple(slots, idx, pv, nv) 140 | } 141 | 142 | termbox.Flush() 143 | } 144 | 145 | func drawSimple(slots [3]int, idx, pv, nv int) { 146 | p := [3]string{" ", " ", " "} 147 | p[idx] = fmt.Sprintf("%d", pv) 148 | 149 | n := [3]string{" ", " ", " "} 150 | n[idx] = fmt.Sprintf("%d", nv) 151 | 152 | rows := []string{ 153 | fmt.Sprintf(" %s %s %s", p[0], p[1], p[2]), 154 | fmt.Sprintf("chmod %d %d %d", slots[0], slots[1], slots[2]), 155 | fmt.Sprintf(" %s %s %s", n[0], n[1], n[2]), 156 | } 157 | 158 | for y, row := range rows { 159 | for x, r := range row { 160 | termbox.SetChar(x, y, r) 161 | } 162 | } 163 | } 164 | 165 | func drawBig(slots [3]int, idx, pv, nv int) { 166 | p := [3][6]string{emptyNumber, emptyNumber, emptyNumber} 167 | p[idx] = bigNumbers[pv] 168 | 169 | s := [3][6]string{ 170 | bigNumbers[slots[0]], 171 | bigNumbers[slots[1]], 172 | bigNumbers[slots[2]], 173 | } 174 | 175 | n := [3][6]string{emptyNumber, emptyNumber, emptyNumber} 176 | n[idx] = bigNumbers[nv] 177 | 178 | genRow := func(arr [3][6]string, pre [6]string) []string { 179 | max := len(arr[0]) 180 | var result []string 181 | for i := 0; i < max; i++ { 182 | row := fmt.Sprintf("%s %s %s %s", pre[i], arr[0][i], arr[1][i], arr[2][i]) 183 | result = append(result, row) 184 | } 185 | return result 186 | } 187 | 188 | var rows []string 189 | rows = append(rows, genRow(p, bigEmptyChmod)...) 190 | rows = append(rows, genRow(s, bigChmod)...) 191 | rows = append(rows, genRow(n, bigEmptyChmod)...) 192 | 193 | for y, row := range rows { 194 | for x, r := range row { 195 | termbox.SetChar(x, y, r) 196 | } 197 | } 198 | } 199 | --------------------------------------------------------------------------------