ksaitoの日記

日々試したことの覚え書き

GNU screenの日本語環境でncursesを使うと表示が乱れる

GNU screenの日本語環境でncursesを使うと表示が乱れます。

環境

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:        16.04
Codename:       xenial
$ screen -v
Screen version 4.03.01 (GNU) 28-Jun-15
$ echo $LANG
ja_JP.UTF-8
$ 

表示が乱れる

screenを起動してncursesのサンプルを動かすと表示が乱れます。

サンプルは、こちらのコードを使わせていただきました。

$ screen
$ ./curwin1
                            ┌─────────────────────┐
     │   Move the window   │
   │ with the arrow keys │
 │     or HOME/END     │
│                    │
│  Press 'q' to quit  │
└─────────────────────┘

GNU screenの設定

下記の設定を`.screenrcに追加します。

$ tail -1 ~/.screenrc 
cjkwidth off
$

screenを再起動すると、きれいに表示されるようになりました。

日本語表示/入力とも問題ないようです。今のところ副作用はないです。

$ screen
$ ./curwin1

                            ┌─────────────────────┐
                            │   Move the window   │
                            │ with the arrow keys │
                            │     or HOME/END     │
                            │                     │
                            │  Press 'q' to quit  │
                            └─────────────────────┘

dockerでgnu screenを使う

dockerコンテナでGNU screenを使えるようにしてみました。

環境

試した環境は、以下の通りです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.1 LTS
Release:    16.04
Codename:   denial
$ docker --version
Docker version 1.12.2, build bb80604
$ sudo docker run -it -d ubuntu:16.04 bash
$

screenのインストールと起動

コンテナに接続してscreenを起動するとttyがないので起動できません。

$ sudo docker exec -it ecstatic_williams bash
root@d82a18f67359:/# apt update && apt install -y screen
root@d82a18f67359:/# screen
Must be connected to a terminal.
root@d82a18f67359:/# tty
not a tty
root@d82a18f67359:/# 

ttyを設定

ngettyパッケージでgettyttyを設定します。

root@d82a18f67359:/# apt install -y ngetty
root@d82a18f67359:/# useradd -m test
root@d82a18f67359:/# passwd test
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
root@d82a18f67359:/# getty tty 

Ubuntu 16.04.1 LTS d82a18f67359 tty

d82a18f67359 login: test
Password: 
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-45-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

test@d82a18f67359:~$ tty
/dev/tty
test@d82a18f67359:~$ 

ttyが設定されたのでscreenも無事起動しました。

test@d82a18f67359:~$ screen
Screen version 4.03.01 (GNU) 28-Jun-15

Copyright (c) 2010 Juergen Weigert, Sadrul Habib Chowdhury
Copyright (c) 2008, 2009 Juergen Weigert, Michael Schroeder, Micah Cowan, Sadrul Habib Chowdhury
Copyright (c) 1993-2002, 2003, 2005, 2006, 2007 Juergen Weigert, Michael Schroeder
Copyright (c) 1987 Oliver Laumann

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either
version 3, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program (see the file COPYING); if not, see http://www.gnu.org/licenses/, or contact Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA.

Send bugreports, fixes, enhancements, t-shirts, money, beer & pizza to screen-devel@gnu.org


Capabilities:
+copy +remote-detach +power-detach +multi-attach +multi-user +font +color-256 +utf8 +rxvt +builtin-telnet

DockerのGUIをMacで使う

DockerコンテナのGUIアプリ(といってもxterm)をMacから使ってみました。

Quartz 2.7.11のインストール

QuartzからMacにダウンロードしてインストールします。

Quartzの設定

X11 - 環境設定でネットワークク・ライアントからの接続を許可をチェックしてX11を再起動する。 f:id:ksaito11:20161130211256p:plain

Quartzを起動するとxtermが起動するので下記のコマンドを実行してリモートからの接続を許可します。

$ xhost +

Dockerからの接続

コンテナにxtermをインストールして、下記のように実行します。

root@6497ba135950:/# useradd -m user
root@6497ba135950:/# su - user
user@cba17df706a0:~$ export DISPLAY=<MacのIPアドレス>:0
user@cba17df706a0:~$ xterm

これで、Dockerコンテナで実行したxtermがMac側に表示されます。

以上

既存のDebianパッケージの展開

既存のDebianパッケージを参考にするためにダウンロードして展開する方法です。

下記のコマンドで作成したディレクトリに既存パッケージの完成形が展開されます。

apt-get download mysql-server-5.7
mkdir mysql
dpkg-deb --raw-extract mysql-server-5.7_5.7.16-0ubuntu0.16.04.1_amd64.deb mysql

gitのrebaseでコミットを書き換える

git rebaseでコミットをきれいにする方法です。

使うタイミング

自分専用のトピックブランチで作業が完了して 元のブランチに戻す前 に行います。

練習用のリポジトリ作成

sampleというファイルに機能が3つインプリされているという想定のテスト用のリポジトリを作成します。

$ git init test
$ cd test
$ for i in `seq 1 3`; do printf "func%d\n\n" $i; done > sample
$ cat sample 
func1

func2

func3

$ git add sample; git commit -m "init"
[master (root-commit) c509fc6] init
 1 file changed, 6 insertions(+)
 create mode 100644 sample
$ 

トピックブランチを作って、3つの機能の全てに修正を加えてコミットしたと想定します。

$ git checkout -b topic
Switched to a new branch 'topic'
$ sed -ie 's/\(func.\)/\1+fix/' sample 
$ cat sample
func1+fix

func2+fix

func3+fix

$ git commit -a -m "fix"
[topic 77adf00] fix
 1 file changed, 3 insertions(+), 3 deletions(-)
$ 

この時点で、気がつけば、git reset HEADで戻すことも可能ですが、気が付いたときには、もう一つ機能が追加されていたとします。

$ echo func4 >> sample
$ git commit -am "これは、正しいコミットで修正したくない"
[topic e836500] これは、正しいコミットで修正したくない
 1 file changed, 1 insertion(+)
$ 

テスト用のリポジトリは、こんな感じになります。

$ git --no-pager log --pretty=oneline
e836500e25943c80e67fcbfdcfef10e0d8fc0040 これは、正しいコミットで修正したくない
77adf00234c23ceda93be668eef460a218944f5a fix
c509fc62f1a14a5d374c20203fecec670cf82a6f init
$ 

コミットを分割する

3つの機能の修正は、独立しているので、修正毎に3つのコミットにするべきだったと想定して、コミットを分割します。

対象のコミットを確認する。

git show-branchコマンドで、現在のtopicブランチに2つのコミットがありtopic^に修正したいコミットがあることがわかります。

$ git show-branch
! [master] init
 * [topic] これは、正しいコミットで修正したくない
--
 * [topic] これは、正しいコミットで修正したくない
 * [topic^] fix
+* [master] init
$ 

対象のコミットを修正

git rebase -iを使って対象のコミットを修正します。

対象は、topic^なので、その一つ前のmasterを指定します。

$ git rebase -i master

エディタが開いて対象のコミットが下記のように表示されます。

pickとなっている部分で[e]を選択してエディタを押します。

pick 77adf00 fix                                                                                                                                                                     
pick e836500 これは、正しいコミットで修正したくない                                                                                                                                  
                                                                                                                                                                                     
# Rebase c509fc6..e836500 onto c509fc6 (2 command(s))     

修正したいコミットのpickeditに変わったのを確認してエディタを修正します。

edit 77adf00 fix                                                                                                                                                                     
pick e836500 これは、正しいコミットで修正したくない                                                                                                                                  
                                                                                                                                                                                     
# Rebase c509fc6..e836500 onto c509fc6 (2 command(s))   

エディタが終了すると、こんなメッセージが表示されます。

Stopped at 77adf00234c23ceda93be668eef460a218944f5a... fix
You can amend the commit now, with

        git commit --amend 

Once you are satisfied with your changes, run

        git rebase --continue

$ 

git resetで修正を戻します。

$ git reset HEAD^
Unstaged changes after reset:
M       sample
$ 

修正内容を分割

git add -pを使って修正を分割します。

下記のように表示されるので、必要な修正まで[s]で分割していきます。

$ git add -p
diff --git a/sample b/sample
index bd073e0..b9dc832 100644
--- a/sample
+++ b/sample
@@ -1,6 +1,6 @@
-func1
+func1+fix
 
-func2
+func2+fix
 
-func3
+func3+fix
 
Stage this hunk [y,n,q,a,d,/,s,e,?]? 

[s]を押すと次のように、コミットが分割されます。

Stage this hunk [y,n,q,a,d,/,s,e,?]? s
Split into 3 hunks.
@@ -1,2 +1,2 @@
-func1
+func1+fix
 
Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? 

[y]を押して、分割されたコミットがステージされた状態にします。

[q]を押して一旦、終了します。

Stage this hunk [y,n,q,a,d,/,j,J,g,e,?]? y
@@ -2,3 +2,3 @@
 
-func2
+func2+fix
 
Stage this hunk [y,n,q,a,d,/,K,j,J,g,e,?]? q

$ 

git statusで確認すると下記のようになります。

$ git status
interactive rebase in progress; onto c509fc6
Last command done (1 command done):
   edit 77adf00 fix
Next command to do (1 remaining command):
   pick e836500 これは、正しいコミットで修正したくない
  (use "git rebase --edit-todo" to view and edit)
You are currently splitting a commit while rebasing branch 'topic' on 'c509fc6'.
  (Once your working directory is clean, run "git rebase --continue")

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   sample

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   sample

$ 

下記のように分割したコミットだけがステージされるので、これを、あと2回繰り返して、3つのコミットに分割します。

$ git --no-pager diff --staged
diff --git a/sample b/sample
index bd073e0..604342d 100644
--- a/sample
+++ b/sample
@@ -1,4 +1,4 @@
-func1
+func1+fix
 
 func2
 
$ git --no-pager diff
diff --git a/sample b/sample
index 604342d..b9dc832 100644
--- a/sample
+++ b/sample
@@ -1,6 +1,6 @@
 func1+fix
 
-func2
+func2+fix
 
-func3
+func3+fix
 
$ 

ログが分割されたことを確認します。

$ git --no-pager log --pretty=oneline
04d8da4ce4057583d6b917f0b1e481211fdb207c func3
e780e2cbd878d6d7bc2c9dd99cb1e0cefc9131dc func2
8263453e933119f5397b96267425c357731120e0 func1
c509fc62f1a14a5d374c20203fecec670cf82a6f init
$ 

git rebase --continueでログの修正を終了します。

$ git rebase --continue
Successfully rebased and updated refs/heads/topic.
$ 

これで、コミットが分割されました。

$ git show-branch
! [master] init
 * [topic] これは、正しいコミットで修正したくない
--
 * [topic] これは、正しいコミットで修正したくない
 * [topic^] func3
 * [topic~2] func2
 * [topic~3] func1
+* [master] init
$ 

以上

systemdで設定を上書きする方法

systemdで設定を上書きするには、/etc/systemd/systemに設定します。

systemctl edit <サービス>コマンドがあります。

$ sudo systemctl edit <サービス名>.service

エディタが起動されて追加設定を書き込めます。 設定先は、/etc/systemd/system/<サービス名>.service.d/override.confです。

$ ls -l /etc/systemd/system/<サービス名>.service.d/
合計 4
-rw-r--r-- 1 root root 27 11月  6 08:35 override.conf
$

今まで、手動でやってました。

既存のgitリポジトリを短時間でコピーする。

既存のgitリポジトリを短時間でコピーする方法です。

ローカルにクローン

ブランチを指定して最新コミットに限定してクローンします。

$ git clone --depth 1 -b <ブランチ名> <URL>

リモートにプッシュ

自分用のリポジトリにプッシュするとrejectされます。

$ git remote add origin <URL>
$ git push origin <ブランチ名>
...
 ! [remote rejected] <ブランチ名> -> <ブランチ名> (shallow update not allowed)
error: failed to push some refs to <URL>
$

部分的にクローンしたgitリポジトリ--unshallowオプションでpush可能なリポジトリに変換します。

再度、pushします。

$ git fetch --unshallow
$ git push origin <ブランチ名>
Counting objects: 227, done.
Compressing objects: 100% (97/97), done.
Writing objects: 100% (227/227), 66.44 KiB | 0 bytes/s, done.
Total 227 (delta 132), reused 214 (delta 129)  
To <URL>
 * [new branch]      <ブランチ名> -> <ブランチ名>
$